Create an Apex class that uses the @future annotation to update Account records – Asynchronous Apex

Use Future Methods:

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.

Code Example:

AccountProcessor Apex Class

 

AccountProcessorTest Apex Test Class for the AccountProcessor

(21)

Written by 

I am Sakthivel Madesh, a certified Salesforce Developer & Administrator working on Salesforce Technology since 2011. I am currently working in IBM Austraila as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 9 Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply