Thursday, March 19, 2015

Configure Redirect URLs after Record Saved in Salesforce

In general, after a record is saved in Salesforce, it will redirect to Detail Screen or list of records of its corresponding Object.

There are many cases where we want to override this functionality. For example, after a new Account created, we want user to create Tasks to it, so once the New Account is saved, page should redirect to New Task screen with Current Account ID details.

To accomplish this, we in general need to create a Custom New Form for Account and in that form, we override default save action and once default save action is accomplished, we redirect using PageReference

We also save one more easy way to accomplish this on default forms, adding details through URL parameters.

By default, Salesforce provided 3 URL parameters,  'retURL', 'saveURL', 'cancelURL'.

We can make use of these parameters to redirect the screen after default button actions.

lets understand with below simple example.

https://demo.salesforce.com/a08/e?retURL=%2Fa08%2Fo&saveURL=/apex/google&cancelURL=/apex/yahoo

The above URL is actually a new form for a custom object.

https://demo.salesforce.com/a08/e

But we also provided other parameters along with URL.

saveURL=/apex/google, this parameter is used for redirection after the record saved. So, once a record is saved, it redirected to a custom page 'google', also the newly created record Id is sent as a parameter by default.

https://demo.salesforce.com/apex/google?newid=a08o0000006rTSR

cancelURL=/apex/yahoo, this parameter is used for redirection if user clicked on default Cancel button . This redirects to a custom page 'yahoo' if user cancelled the form.

Tuesday, March 17, 2015

Mathematical Operations & Number formatting in Visualforce Page

In a Visualforce page, if we want to do some basic mathematical Operations and display on the screen, eg: display percentage along with the existing value in a Table, we can use apex:outputtext

Sample code to implement it is as below, here we are displaying it in a PageBlockTable, display marks for each subject in one column and  Percentage (Calculated) for each subject in another column

 <apex:pageBlockTable value="{!score}" var="item">  
      <apex:column value="{!item.subject}"/>   
      <apex:column value="{!item.marks}" dir="RTL">  
           <apex:facet name="footer" >  
                {!total}  
           </apex:facet>  
      </apex:column>       
      <apex:column >  
           <apex:outputText value="{!(item.marks / total) * 100}%" />  
      </apex:column>  
 </apex:pageBlockTable>   

In above example, we already have a variable 'total' with sum of marks of all subjects.

In some scenarios, we  might need to format the text when we display on screen like, Currency notation / Date in a specific format etc.. apex:outputtext also has this functionality.

Sample code to implement it is as below, here we send input to outputtext as params, which are used with number notations {0} as in C, C++

 <apex:outputText value="{0, number, 000,000.00}$">  
   <apex:param value="{!AnnualRevenue}" />  
 </apex:outputText>  
  <apex:outputText value="The formatted time right now is:   
       {0,date,yyyy.MM.dd G 'at' HH:mm:ss z}">  
   <apex:param value="{!NOW()}" />  
 </apex:outputText>