Wednesday, October 19, 2016

Visualforce Component for Record Status Progress Bar

Sometimes we have to display current status of record through progress bar on record detail page. For example user can directly see the status of current opportunity or case by looking into detail page of record.




For this purpose we generally create a VF page and embed it in record page layout.

I have created a generic visualforce component which can be used to display record status bar. We just have to pass the object API name and field API name to component and it will generate status bar for you.

Upload below image image in static resource and name it as "progressbar".


Just create a new VF page and include this component in it. Below is code for visualforce Component and visualforce component Controller.


Suppose we need to display progress bar for opportunity stage, then use below code.

<apex:page standardController="Opportunity">
<c:StatusProgressBar objectAPIName="Opportunity"  fieldAPIName="stagename"/>
</apex:page>

Note:
  • You need to specify the objectAPIName and fieldAPIName for which you want to display progress bar.
  • Add this VF page to page layout to display progress bar.

Opportunity Record Detail Page



Now suppose you want to display progress bar for case record then create a VF page and use below code:

<apex:page standardController="Case">
<c:StatusProgressBar objectAPIName="Case" fieldAPIName="Status"/>
</apex:page>

Here we are displaying the progress bar for status field present in case. You can display status bar for any picklist field (standard or custom).



Friday, October 14, 2016

Batch Apex Job Progress Bar

We can display the progress of Batch Apex job on UI to user. Apex allows you to query the job status by query AsyncApexJob  object and getting all details about running job like status, JobItemsProcessed, NumberOfErrors, TotalJobItems etc. By using apex, we can manipulate the result and display the progress bar for batch job.

Suppose you already have Apex Batch class written. Here for example "RecordTypeAccessFinder" is batch class is already present.

Below is VF page and Apex class Code:



Monday, October 3, 2016

Asynchronous Apex- Apex Scheduler, Batch Apex & Future Methods Implementation Tricks

Salesforce provide different options to run asynchronous jobs which can run in background whenever salesforce resources will be available.

Apex Scheduler is one of the options through which you can schedule a job to run after certain interval of time or to run at particular time everyday, every week, every month etc.

Batch Apex allows you to process large volume of data. Normal apex class will hit governor limits if we process large volume of data. Batch Apex split the records in different batched and you will get new governor limits for each batch.

Future methods runs asynchronously which can be used to perform some operation in background. When you are executing particular code and you want to perform some other operation and don't want to wait for it to get completed, then you can use future method to perform that operation and you can continue executing your code. You can consider that future methods starts new thread or execution.

Now we are going to cover different scenarios related to future methods and apex schedulers and batch apex.


  • Is it possible to call future method from apex scheduler or not?
         Yes

Below is sample code which I tested for this scenario. After scheduling the scheduler, I checked the debug logs and it was displaying logs for future handler  and debug statement present in future method was present in logs.

//Scheduled Apex 
public class DemoScheduler1 implements Schedulable{
    public void execute(SchedulableContext sc){
        system.debug('*******Going to call future method ');
        DemoAsynchronousTest.futureMethodCallFromScheduler();
    }
}
//apex class containing future method
public class DemoAsynchronousTest{
    @future
    public static void futureMethodCallFromScheduler(){
        system.debug('******futureMethodCallFromScheduler get called');
    }
    
}

  • Is it possible to do Synchronous Web service callouts from scheduled apex?
          No.

Synchronous Web service callouts are not supported from scheduled Apex. To be able to make callouts, make an asynchronous callout by placing the callout in a method annotated with @future(callout=true) and call this method from scheduled Apex. However, if your scheduled Apex executes a batch job, callouts are supported from the batch class.

  • Can we call scheduler from future method?
Yes

  • Can we call future method from batch apex (from execute method)?
No

  • Is there is any way through which we can call future method from batch apex?
As we know that a webservice can be called from batch class and webservice can call @future method. So in your batch class call webservice and which can call your @future method.         
Also you can call future method from finish method in batch class.

  • What all different things which we need to consider while using future methods?
  1. Methods with the future annotation cannot be used in Visualforce controllers in either getMethodName or setMethodName methods, nor in the constructor.
  2. You cannot call a method annotated with future from a method that also has the future annotation. Nor can you call a trigger from an annotated method that calls another annotated method.
  3. Future methods cannot be called from batch class.

  • Can we modify the scheduler class or classes referenced by this scheduler class if any scheduled job is pending?
          No

If there are one or more active scheduled jobs for an Apex class, you cannot update the class or any classes referenced by this class through the Salesforce user interface. However, you can enable deployments to update the class with active scheduled jobs by using the Metadata API.

  • What we can do if we have to deploy the scheduler class or classes referenced by this scheduler class if any scheduled job is pending for that scheduler class in target org?
By default, changes to Apex code that have Apex jobs pending or in progress can’t be deployed. To deploy these changes, do one of the following.
  • Cancel Apex jobs before deploying changes to Apex code. Reschedule the jobs after the              deployment.
  • Enable deployments with Apex jobs in the Salesforce user interface in the Deployment Settings page.