Pages

Showing posts with label Asynchronous Apex. Show all posts
Showing posts with label Asynchronous Apex. Show all posts

Sunday, January 15, 2017

Asynchronous Apex "Using 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.
  • Create an Apex class called 'LeadProcessor' that uses the Database.Batchable interface.
  • Use a QueryLocator in the start method to collect all Lead records in the org.
  • The execute method must update all Lead records in the org with the LeadSource value of 'Dreamforce'.
  • Create an Apex test class called 'LeadProcessorTest'.
  • In the test class, insert 200 Lead records, execute the 'LeadProcessor' Batch class and test that all Lead records were updated correctly.
  • The unit tests must cover all lines of code included in the LeadProcessor class, resulting in 100% code coverage.
  • Run your test class at least once (via 'Run All' tests the Developer Console) before attempting to verify this challenge.
LeadProcessor 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){
        
    }
}

LeadProcessorTest Class:

@isTest
public class LeadProcessorTest {
    static testMethod void leadProcessorTrailTest(){
        List<Lead> leadList = new List<Lead>();
        for(integer i = 0; i<200; i++){
            Lead leads = new Lead();
            leads.FirstName = 'Ankit';
            leads.LastName = 'Avula'+i;
            leads.Company = 'ARG';
            leadList.add(leads);           
        }
        insert leadList;
        Test.startTest();
        LeadProcessor L = new LeadProcessor();
        Database.executeBatch(L);
        Test.stopTest();
    }

}

Thursday, January 12, 2017

Asynchronous Apex "Using 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.
  • Create a field on the Account object called 'Number_of_Contacts__c' of type Number. This field will hold the total number of Contacts for the Account.
  • Create an Apex class called 'AccountProcessor' that contains a 'countContacts' method that accepts a List of Account IDs. This method must use the @future annotation.
  • For each Account ID passed to the method, count the number of Contact records associated to it and update the 'Number_of_Contacts__c' field with this value.
  • Create an Apex test class called 'AccountProcessorTest'.
  • The unit tests must cover all lines of code included in the AccountProcessor class, resulting in 100% code coverage.
  • Run your test class at least once (via 'Run All' tests the Developer Console) before attempting to verify this challenge.
AccountProcessor class:

public class AccountProcessor {
    @future
    public static void countContacts(List<ID> accIds){
        List<Account> listacc = [SELECT id,Number_of_Contacts__c,(SELECT id from contacts) from account where id in : accIds];
        for (account acc : listacc){
            List<Contact> con = acc.contacts;
            acc.Number_of_Contacts__c = con.size();
        }
        update listacc;
    }

}

AccountProcessTest class:

@isTest
public class AccountProcessorTest {
    public static testmethod void TestAccountProcessorTest(){
     Account a = new Account();
        a.Name = 'Test Account';
        Insert a;
        system.debug('account a :'+a.id);
        
        Contact cont = New Contact();
        cont.FirstName ='Ankit';
        cont.LastName ='Avula';
        cont.AccountId = a.Id;
        Insert cont;
        system.debug('contact cont :' +cont.id);
        
        List<Id> accIds = new List<Id>();
        accIds.add(a.Id);
        
        Test.startTest();
        AccountProcessor.countContacts(accIds);
        Test.stopTest();
        
        Account ACC = [select Number_of_Contacts__c from Account where id = :a.id LIMIT 1];
        System.assertEquals(ACC.Number_of_Contacts__c, 1);
    }
}