Solving Use Future Methods Challenge


The Trailhead challenge says:


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.


So, lets start resolving the first points by creating a number field called 
Number_of_Contacts__c in the Account Object.

Once we had this field, lets create the class file called AccountProcessor with next code:




public class AccountProcessor {
    
    //Writting the countContacts method and marking it whit the @future label.
    @future
    public static void countContacts(Set<Id> accountIDs) {
        
        // Creating a list that will contain all those accounts that are referenced through the accounIDs list.
        List<Account> accounts = [SELECT Id, Number_of_Contacts__c, (SELECT id FROM Contacts) from Account where id in :accountIDs];
     
        //Assigment from the total contact number to the Number_of_Contacts__c field for each account at accounts list.
        for( Account account : accounts ) {
          account.Number_of_Contacts__c = account.contacts.size();
        }

        //Updating all accounts in list
        update accounts;

    }

}


and then we can create the Test Class:


@isTest
public class AccountProcessorTest {
    
    @isTest
    public static void countContactsTest(){
        //Creating an account and inserting it
        Account account = New Account(Name = 'Account Number 1');
        insert account;

        //Creating some contacts related to the account and inserting them
        List<Contact> contacts = new List<Contact>();
        contacts.add(New Contact(lastname = 'Related Contact 1', AccountId = account.Id));
        contacts.add(New Contact(lastname = 'Related Contact 2', AccountId = account.Id));
        contacts.add(New Contact(lastname = 'Related Contact 3', AccountId = account.Id));
        contacts.add(New Contact(lastname = 'Related Contact 4', AccountId = account.Id));
        insert contacts;

        //Creating a List with account Ids to pass them throught the AccountProcessor.countContacts method
        Set<Id> accountIds = new Set<Id>();
        accountIds.add(account.id);

        //Starting Test:
        Test.startTest();

        //Calling the AccountProcessor.countContacts method
        AccountProcessor.countContacts(accountIds);

        //Finishing Test:
        Test.stopTest();
        Account ACC = [SELECT Number_of_Contacts__c FROM Account WHERE id = :account.Id LIMIT 1];
        
        //Setting Assert (We have to parse the account.Number_of_Contacts__c 
        //to integer to avoid some comparasion error between decimal and integer)
        System.assertEquals( Integer.valueOf(ACC.Number_of_Contacts__c) , 4);
    }


}


Comentarios

Publicar un comentario

Entradas más populares de este blog

Solving Subscribe to a Platform Event in an Apex Trigger Challange

Solving Schedule Jobs Using the Apex Scheduler

Solving Use Batch Apex Challenge