Salesforce Queueable Apex and Future annotations

Introduced for some time now particularly in Winter 15. Queueable Apex addresses some short coming of the @future methods. It’s an offspring of the marriage of @future methods and Batch Apex.

Queueable Apex allows you to submit jobs for asynchronous processing similar to future methods with the following additional benefits:

  • Non-primitive types – you can pass SObjects and custom apex types
  • Monitoring – you can monitor the job from the Apex jobs page in Setup or query the AsyncApexJob record
  • Chaining Jobs – create sequential jobs

How to use Queueable Apex:

  • Create a class that implements the Queueable interface
  • If you are going to make callouts you need to extend your implementation to Datatabase.AllowCallouts
  • To allow multiple primitive types create a contructor and call the public void execute method.
  • To execute the class, call it from your logic
  • There you can chain jobs by calling another Queueable class on the execute method.

Things to note: This counts to the your limit of 250 limit for asynchronous process per 24 hours.

Demystifying JSON Parsing In Apex

You got back a JSON string either as a response from a REST service, passed through from your visual force page via remote action or passed from your lighting component to your apex controller, now how do you parse this information? For simple JSON structures Salesforce can automatically parse them for you. There are tools online that would generate apex classes from the JSON string that would assist in parsing the JSON input.

It is good to get back to the basics and understand a little bit how the JSON string is structured and how the parsing is done.

What is JSON? JSON is the acronym of Javascript Object Notation and is a lightweight format that is used for data exchange between servers and modern applicationsIt is based on a subset of JavaScript language (the way objects are built in JavaScript).

Read the rest of this entry »

Reference the External ID on Lookup

This post shows you how you can reference another object through it’s external id. This is useful if you do not know the Salesforce ID but know the external ID.

Example: You have an external field on the Account object with an API named myExternal_Id__c. You have an account record named “Burlington Textile of America” and let’s give the external ID a value of “X123”.

 

Issue is you want to create a contact record but do not know the account record id but know the external Id.

Check this snippet of apex code to accomplish this.

Account acc = new Account(myExternal_Id__c = ‘X123’);

Contact con = new Contact();

con.Account = acc;

con.LastName = ‘Pau’;

insert con;

The same concept is applicable for JSON files

{

“Account” : {“myExternal_Id__c” : “X123”},

“LastName” : “Pau”

}

 

Master-Detail: Field is not writable error on Apex

I came upon this error when I assigning the custom object to the master standard object.

Invoice__c inv = new Invoice__c();
inv.Account__c = accountId;
insert inv;

Then I get this error.

Field is not writable : Invoice__c.Account__c

The fix was actually easy. I just rewrote it to assign the master id on instantiating the child object.

Invoice__c inv = new Invoice__c(Account__c = accountId);
insert inv;

Hope somebody finds this useful.