Saturday, February 23, 2019

Asynchronous Apex

Asynchronous Processing Basics

1. What is a key benefits of asynchronous processing?
A. Higher governor and execution limits
B. Unlimited number of external callouts
C. Override organization-wide sharing defaults
D. Enabling turbo mode for transactional processing
2. Batch Apex is typically the best type of asynchronous processing when you want to:
A. Make a callout to a web service when a user updates a record.
B. Schedule a task to run on a weekly basis.
C. Update all records in your org.
D. Send a rickroll email to a contact

Use Future Methods

Create an Apex class that uses the @future annotation to update Account records.
Create an Apex class with a method using the @future annotation that accepts a List of Account IDs and updates a custom field on the Account object with the number of contacts associated to the Account. Write unit tests that achieve 100% code coverage for the class.
 public class AccountProcessor {  
   @future  
   public static void countContacts(Set<Id> conId)  
   {  
     List<Account> accList = [SELECT ID, Name, Number_Of_Contacts__c, (SELECT ID From Contacts)FROM Account WHERE ID IN :conId];  
     for(Account acc : accList)  
     {  
       List<Contact> conList = acc.Contacts;  
       acc.Number_Of_Contacts__c = conList.Size();  
     }  
     update accList;  
   }  
 }  
Test Class :-
 @isTest  
 public class AccountProcessorTest {  
      public static testMethod void createAccount()  
   {  
     Account acc = New Account();  
     acc.Name = 'Test-Account';  
     Insert acc;  
     Contact con = New Contact();  
     con.FirstName = 'Test-FirstName-Contact';  
     con.LastName = 'Test-LastName-Contact';  
     con.AccountId = acc.Id;  
     Insert con;  
     Set<Id> accId = new Set<Id>();  
     accId.add(acc.Id);  
     Test.startTest();  
          AccountProcessor.countContacts(accId);  
     Test.stopTest();  
     Account accList = [SELECT Number_of_Contacts__c FROM Account where id = : acc.Id LIMIT 1];  
     System.assertEquals(accList.Number_of_Contacts__c, 1);  
   }  
 }  

Use Batch Apex

Create an Apex class that uses Batch Apex to update Lead records.
Create an Apex class that implements the Database.Batchable interface to update all Lead records in the org with a specific LeadSource. Write unit tests that achieve 100% code coverage for the class.
 global class LeadProcessor implements  Database.Batchable<Sobject>   
 {  
   global Database.QueryLocator start(Database.BatchableContext bc)   
   {  
     return Database.getQueryLocator([Select LeadSource From Lead ]);  
   }  
   global void execute(Database.BatchableContext bc, List<Lead> scope)  
   {  
       for (Lead Leads : scope)   
       {  
         Leads.LeadSource = 'Dreamforce';  
       }  
     update scope;  
   }    
   global void finish(Database.BatchableContext bc){  }    
 }  
Test Class :-
 @isTest   
 public class LeadProcessorTest   
 {  
   static testMethod void testMethod1()   
   {  
     List<Lead> lstLead = new List<Lead>();  
     for(Integer i=0 ;i <200;i++)  
     {  
       Lead led = new Lead();  
       led.FirstName ='FirstName';  
       led.LastName ='LastName'+i;  
       led.Company ='demo'+i;  
       lstLead.add(led);  
     }  
     insert lstLead;  
     Test.startTest();  
       LeadProcessor obj = new LeadProcessor();  
       DataBase.executeBatch(obj);   
     Test.stopTest();  
   }  
 }  

Control Processes with Queueable Apex

Create an Queueable Apex class that inserts Contacts for Accounts.
Create a Queueable Apex class that inserts the same Contact for each Account for a specific state. Write unit tests that achieve 100% code coverage for the class.
 public class AddPrimaryContact implements Queueable {  
   public Contact con;  
   public String state;  
      public AddPrimaryContact(Contact con, String state)  
   {  
     this.con = con;  
     this.state = state;  
   }  
   public void execute(QueueableContext qc)  
   {  
     List<Account> accList = [SELECT Id, Name, BillingState FROM Account WHERE Account.BillingState =: this.state Limit 200];  
     List<Contact> newContact = new List<Contact>();  
     for(Account acc : accList)  
     {  
       Contact con = new Contact();  
       con = this.con.clone(false, false, false, false);  
       con.AccountId = acc.Id;  
       newContact.add(con);  
     }  
     insert newContact;  
   }  
 }  
Test Class :-
 @isTest  
 public class AddPrimaryContactTest {  
 @testSetup  
 static void setup() {  
      List<Account> insertAccount = new List<Account>();  
      for(integer i=0; i<=100; i++) {  
      if(i <=50) {  
      insertAccount.add(new Account(Name='Acc'+i, BillingState = 'NY'));  
      }   
     else {  
           insertAccount.add(new Account(Name='Acc'+i, BillingState = 'CA'));  
           }  
      }  
      insert insertAccount;  
      }  
 static testMethod void testAddPrimaryContact() {  
   Contact con = new Contact(LastName = 'LastName');  
   AddPrimaryContact addPC = new AddPrimaryContact(con, 'CA');  
   Test.startTest();  
   system.enqueueJob(addPC);  
   Test.stopTest();  
      system.assertEquals(50, [SELECT count() FROM Contact]);  
      }  
 }  

Schedule Jobs Using Apex Scheduler

Create an Apex class that uses Scheduled Apex to update Lead records.
Create an Apex class that implements the Schedulable interface to update Lead records with a specific LeadSource. Write unit tests that achieve 100% code coverage for the class. This is very similar to what you did for Batch Apex.
 global class DailyLeadProcessor implements Schedulable{  
   global void execute(SchedulableContext sc){  
     List<Lead> lstOfLead = [SELECT Id FROM Lead WHERE LeadSource = null LIMIT 200];  
     List<Lead> lstOfUpdatedLead = new List<Lead>();  
     if(!lstOfLead.isEmpty()){  
       for(Lead ld : lstOfLead){  
         ld.LeadSource = 'Dreamforce';  
         lstOfUpdatedLead.add(ld);  
       }  
       UPDATE lstOfUpdatedLead;  
     }      
   }  
 }  
Test Class :-
 @isTest  
 private class DailyLeadProcessorTest{  
   @testSetup  
   static void setup(){  
     List<Lead> listOfLead = new List<Lead>();  
     for(Integer i = 1; i <= 200; i++){  
       Lead ld = new Lead(Company = 'Comp' + i ,LastName = 'LN'+i, Status = 'Working - Contacted');  
       listOfLead.add(ld);  
     }  
     Insert listOfLead;  
   }  
   static testmethod void testDailyLeadProcessorScheduledJob(){  
     String sch = '0 5 12 * * ?';  
     Test.startTest();  
     String jobId = System.schedule('ScheduledApexTest', sch, new DailyLeadProcessor());  
     List<Lead> listOfLead = [SELECT Id FROM Lead WHERE LeadSource = null LIMIT 200];  
     System.assertEquals(200, listOfLead.size());  
     Test.stopTest();  
   }  
 }  

Monitor Asynchronous Apex

1. What type of jobs do not show up in the Apex Flex Queue.
A. Future Method Jobs
B. Batch Apex Jobs
C. Queueable Apex Jobs
D. Scheduled Apex Jobs
2. Which statement is true regarding the Flex Queue.
A. You can submit up to 200 batch jobs for execution.
B. Jobs are processed first-in first-out.
C. Jobs can only be scheduled during Taco Tuesdays.
D. Jobs are executed depending upon user license


EmoticonEmoticon