Tuesday, March 22, 2011

Serialize Batch apex in Salesforce

In salesforce we cannot call a batch apex class from another batch apex class because batch apex is a future call.

To make the batch process serialize we have to start the another batch when first batch is finished. By using salesforce Email services we can make batch process serializable. Create a class which implements Messaging.InboundEmailHandler.

global class EmailServiceHandler implements Messaging.InboundEmailHandler 
{
        global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,Messaging.InboundEnvelope envelope) 
        {
  Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
  return result;
 }
}

Now create a class to send an email to salesforce email services email address.

public class EmailHandler
{
    public static void sendEmail(string body)
    {
       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
       String[] toAddresses = new String[] {'myhandler@74m9vxsizy32oug74yh6rhj7e.in.salesforce.com'};
       mail.setToAddresses(toAddresses);
       mail.setSubject('Test Batch' );
       mail.setPlainTextBody(body);
       Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
    
}

When first batch apex is finished. We will have to call sendEmail method of EmailHandler class in finish method of batch class.

EmailHandler.sendEmail('start second batch');

In the EmailServiceHandler class we can check the email body.
global class EmailServiceHandler implements Messaging.InboundEmailHandler 
{
        global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,Messaging.InboundEnvelope envelope) 
        {
    if (email.plaintextbody == 'start second batch')
           {
              // create the instance of second batch class
              SecondBatchClass obj = new SecondBatchClass();
              Database.executeBatch(obj);
           } 
           Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
    return result;
 }
}


This will make your batch process serialize.

6 comments:

  1. The limit of active query cursors per user increased from 5 to 50. All Apex code has this higher limit except for the batch Apex
    start method, which has a limit of 5 open cursors per user. The remaining batch methods have the higher limit of 50 cursors.
    With this higher limit, SOQL queries in your code are less likely to reach the cursor limit and cause errors resulting from
    having cursors released by the system. Query cursors are created when SOQL queries execute and make use of inner queries
    or generate large result sets.

    salesforce training in Chennai

    ReplyDelete
  2. It has been simply incredibly generous with you to provide openly what exactly many individuals would’ve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.
    Python Online certification training
    python Training institute in Chennai
    Python training institute in Bangalore

    ReplyDelete
  3. Useful information.I am actual blessed to read this article.thanks for giving us this advantageous information.I acknowledge this post.and I would like bookmark this post.Thanks
    AWS Training in Bangalore
    AWS training in sholinganallur
    AWS training in Tambaram
    AWS training in Velachery

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.

    AWS training in Chennai

    AWS Online Training in Chennai

    AWS training in Bangalore

    AWS training in Hyderabad

    AWS training in Coimbatore

    AWS training

    AWS online training

    ReplyDelete
  6. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
    salesforce training in chennai

    software testing training in chennai

    robotic process automation rpa training in chennai

    blockchain training in chennai

    devops training in chennai

    ReplyDelete