condional rendering with a redirect - salesforce

Here is the set up. I have a standard page layout with custom buttons.
When the user clicks a button, i want to check a value in the extension class. If the value is null or 0- i want a pages message to appear, otherwise i want to redirect them to another VF page.
the way that i'm attempting to do this is put a VF section on the page layout, and have it conditionally rendered using an action:support method, but I can't get it to work..
HALP!

You do not need to add a VF page to the layout.
Try to use the Salesforce AJAX Toolkit for your custom button:
1) First create a WebService to calculate your value:
global with sharing class yourController{
WebService static Integer calculateValue(Integer i) {
Integer result = i + 5;
return result;
}
}
2) Then create a custom button for your object: Display Type -
Detail Page Button Behavior - Execute JavaScript Content Source - onClick JavaScript
3) Then add this code to the button:
// Loading the ajax toolkit data
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
// Making a call to the webservice method
var value = sforce.apex.execute("yourController","calculateValue", {i:5});
// If the value is 0 or blank
if(value == 0 || value == ''){
// Pop up a message
alert('Your message text here');
else{
// Otherwise redirecting user to another page
window.location = "/apex/YourCustomPage";
}
4) Now save your button and do not forget to add it to the layout page :)

If the method called from the button returns a PageReference, you can create a PageReference object and use the .setRedirect( True) method to do a redirect. You're probably returning Null right now, which indicates "no action".
You can set the target of the redirect when you create the PageReference, or use the new ApexPages.StandardController() method to select one of the controllers for a given object type, or just Page.vfPageName to reference another Visualforce page.
When your method returns that PageReference, the browser will redirect to the new URL.

Related

Call a link from APEX

Assume a URL link (containing a merge field on the Account object) exist on the Account object with the API Name link_1, and I have a VF Page with a standrdcontroller on Account, is there a way to call link_1 from the controller or when the page loads, the goal here is to invoke that link programtically once the controller and action are completed.
And what do you mean by invoke? Redirect user to that linked page? Pull that page's content and somehow display? Get page's content, save to PDF and put in Attachment in Salesforce for example?
Actions that can be bound in Visualforce are supposed to return PageReference. Most of the time you return null or mark the method as void, you want to stay on current VF page (maybe there's something else you want the user to do, maybe there were errors and you want to display them on same page so user has chance to fix them). But for you it could be
public PageReference doSomething(){
// Your action code here
// ...
PageReference pr = new PageReference(acc.Link_1__c);
return pr;
}

Refresh data on change in scope variable

I'm using AngularJS for an external web project.
I'm stuck with the update interval of the page as the JSON data I get is from the html div from inside the page, JSON data is being sent from policy.
I'm using
$scope.json_data = angular.element(document).find('#idofthejsondiv').html();
The div gets different JSON data for every 'x' seconds.
How can I update the page on the change in the divs value?
try this:
$scope.$watch (
function () {
return angular.element(document).find('#idofthejsondiv').html();
},
function (newValue) {
console.log (newValue);
// do stuff with the new value...
}
);
but it has to be mentioned that this doesn't seem like a appropriate structure of your app. the data in the div has to be changed somewhere somehow by some service. the better approach would be to intercept this service whenever it puts the changes to the div and do whatever you want with that new content

Adobe CQ EXTJS component data post to servlet

I got a EXTJS CQ component with two text fields and and button.
When the "save" button clicked, the dialog data has to be submitted to custom sling servlet.
Custom sling servlet will call a osgi service and finally saves data to crx using jcr api.
Question : How to post the dialog data to servlet ?
I am new to CQ, Thanks for any help!
-Sri
I'm assuming when you say "save" you are referring to some custom button and not the "OK" button that saves the dialog data to the node.
Add a handler to the save button. The handler function must retrieve the dialog object, loop over all the fields in it and post the values to your custom servlet. The handler should be something like this
function(button,event){
//fetch dialog using the save button
var dialog = button.findParentByType('dialog');
var params = {}; //parameters to post
var textfields = dialog.findByType('textfield'); //returns all textfields in the dialog
for(i=0;i<textfields.length;i++){
params[textfields[i].name] = textfields[i].getValue(); //add the value to params with name same as the name you have provided to the textfield
}
$.post( "path to your servlet" , params ); // you can also use CQ.shared.HTTP of cq's ext js package to do the post
}
In case all you want to do is post the form data on clicking the "OK" button that comes by default, set the formurl property of the dialog to the path of your custom servlet. In this case if the values aren't stored back as properties with appropriate names on the corresponding node like a dialog normally does, the dialog will not be able to load the values when the component is re-edited.

CakePHP 1.3 and TCPDF: how to return to controller after printing PDF

I'm using CakePHP 1.3 and TCPDF to print a PDF file using print_pdf.ctp. I'm having trouble redirecting the page to the referrer after this print. The commented section of the controller action is not reached.. How do I manage this?
Note that print_pdf() is linked to a button. So, the user loads a page, presses the "Print" button linked to print_pdf action, and a PDF file is printed when this button is clicked. The reason I'd like to redirect is that the print_pdf action updates some data and the page where the button is located needs to be refreshed to reflect this update.
Controller:
// This action is linked to a button
function print_pdf() {
...
$this->layout = 'pdf'; //this will use the pdf.ctp layout
$this->render();
// Line below are not executed
...
$this->redirect($this->referer());
}
I'm still not entirely sure I understand what you're trying to do. You are trying to redirect to another page to save some information, and then immediately display a PDF to the person?
Why not just run whatever save your doing from inside the PDF function:
// This action is linked to a button
function print_pdf() {
...
//$this->redirect($this->referer());
//Put whatever you were going to redirect to before the render. Instead of redirecting just run the thing as part of this function.
...
$this->layout = 'pdf'; //this will use the pdf.ctp layout
// Line below are not executed
$this->render();
}
$this->render(); should escape the controller and go straight to the view. You can't put anything after it. If you want to make changes to the page with the button, you can do that before the render.

Hide Email on Lead Page Layout in Salesforce

I am looking at hiding Email on Leads Page Layout.
I am including it in a section already with a visualforce page but PageLayout doesn't detect that and still requires it to be on the layout.
Is there a way to remove it from the PageLayout?
I have read that you can edit a picklist but didn't find any solid examples of doing that. I also read about Field Level Accessibility but I don't think that's the way to go since everyone is part of the same role and I am only excluding the detail based off if you're the owner. I have also tried javascript but since my visualforce page is loaded in an iframe I can't access the parent document of the iframe to be able to hide or remove the value from email while viewing not as the owner.
Any thoughts would be appreciated.
Try hiding the field with JavaScript stored in a Custom Button. In the On-Click JavaScript for the button, you can include code that will be run on page load by jQuery that hides the Email field. You can then hide the actual button as well. Very much a hack approach, but it should work.
Here's some code to put in the On-Click JavaScript button (modified from Daniel Llewellyn's blog post and mgsmith's force2b blog post). Hopefully this should get you jump-started. I didn't include any code on how to hide the Email field, but you said you were already trying it through your Visualforce page, so I figured I'd leave that part to you. Cheers!
var interval;
//The main function that does all the work
function appendScript() {
// Include core jQuery library by injecting it into the DOM
var script= document.createElement('script');
script.type= 'text/javascript';
script.src= 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';
head.appendChild(script);
// It takes a second to load, so put in a delay
// (we don't want to try and reference the script
// before it is actually loaded, so we store the interval
// in a global variable, and set up an interval.
// this interval dealio. This will keep running
// until jQuery has been found to be loaded and then
//clears the interval so it doesn't keep running.
interval=self.setInterval(function(){
//Check to see if jQuery has loaded
if(jQuery) {
//if jQuery has loaded, clear the interval
window.clearInterval(interval);
// Hide the Email field
// Hide the custom button
var btnName = 'buttonName';
try{
var buttons = parent.document.getElementsByName(btnName);
for (var i=0; i < buttons.length; i++) {
buttons[i].className="btnDisabled ";
buttons[i].disabled=true;
buttons[i].type='hidden';
}
} catch(e) {
// var ee = e.message || 0; alert('Error: \n\n'+e+'\n'+ee);
}
}
}, 300);
}
appendScript();

Resources