Saturday, 15 June 2013

MIXED DML Exception from APEX Batch class


Batch class execution fail when you are updating set up(Eg. User) and non-setup(Eg. Custom Sobject) object in same context in apex.

There is a solution in batch class for this:

In batch class we have public void finish() method , we can utilize this method to get new apex context. First either setup object or non setup object update add in execute() method And later add other object update in void finish() method.

Also to pass the variable from execute method to finish method , declare the variable name as global. Also implement the Database.Stateful interface in the same batch class to maintain the variable value.
 
Hope this will help some one who stuck in writing apex code where setup and non-setup object update is required.

Wednesday, 2 January 2013


System.SObjectException: SObject row was retrieved via SOQL without querying the requested field:

This kind of exception that happens when we are trying to access one object property that actually we haven't got through an SOQL query.

Let me explain.

If an Object Lead has Id,Name,Email with it. And if we are writing the below query to retrieve its Id, Name from the object.

Lead Ld;

Ld = [select Id,Name from lead];

if you then access Email property from the Ld instance , sales force will throw the error     System.SObjectException: SObject row was retrieved via SOQL without querying the requested field:      

 To avoid retrieve all the required [here it's email] field in your SOQL query

 

Tuesday, 1 January 2013

 Adding data to sets in apex, A small mistake that made me disappointed for a complete day.

In sets we have a built in function to add data to that sets

Eg: Set<String> Str;

Str.add('Hello');

But be careful that we should create  one instance otherwise salesforce won't allow you to insert data to this sets.

Also salesforce will not generate any compile time waring or error message while saving the code.

The right code for the above example is below

Set<String> Str =  new Set<String>();
Str.add('Hello');

Even though it's a blunder sometimes we might forget about this.