+ Start a Discussion

Cron job with Apex



I am trying to create a application that does data analytics of a Sales Force CRM user's contacts and leads. Basically I want to pull of the contacts and leads from the Salesforce CRM and send it via REST API to my server to do some analysis on the data. After the analysis is complete, I want to send the data back to the Salesforce CRM.


I heard that because Salesforce platform is a multi-tenant environment, there are limitations on how much CPU I can use at any one time. Is a cron job that sends the information over time the best approach? And also can anyone give me some pointers as to what code I need to write to achieve this? I am fairly new to the Salesforce platform but have a good understand of Apex.





There are two ways you can employ CRON in connection with your org: 


1. Create a batchable or schedulable class and schedule it using the native force.com subset of CRON capabilities.

2. Run a CRON on a local machine that connects to your org via the API. 


But of course both will be subject to limits. In the first instance, Apex governor limits and asynchronous Apex execution limits. 


In the second case, API usage limits. 


For the first use case, you will create an Apex class that implements one of two interfaces, either Batchable, or Schedulable. If you will be working with moderate data volumes (querying up to 50k records and making changes to no more than 10k records) just do a schedulable class. If you expect more than that, use Batchable. These days you can directly schedule a Batchable class, but you can also invoke a batch job from a scheduled class. This is all very well documented in the Apex code guide.


For the second, using free tools, such as Informatica, Jitterbit, and others make scheduling easy. You can also use Apex DataLoader+CLIq to quickly create batch jobs and then schedule them. 


The one thing to consider in the first case is the Apex callout limits. Since you have a max of 2 minutes to get things done on a callout, you might bump into that if you are doing this with a lot of data, which would then push you to the second use-case, where the entire process is orchestrated externally using a SOAP API integration. Why SOAP? Quite simply because you can't commit a change to more than one record at a time with our REST API. 


Nick - There are multiple aspects to integrating with Salesforce. For your specific needs you would need to investigate various integration API's provided by Salesforce. The issue of governor limits will need to be investigated if you are calling Apex classes or doing tasks that will invoke code. Please have a look at Apex Data loader for asynchronous data extracts or SOAP API or REST API for getting data out. The key will be understanding the data volumes you are dealing with.