0

I am doing Trailhead from : https://trailhead.salesforce.com/modules/asynchronous_apex/units/async_apex_future_methods and getting the below error.

enter image description here

AccountProcessor

public class AccountProcessor {
    @future
    public static void countContact(Set<Id> setId){
        List<Account> lstAccount = [select Id,Number_of_Contacts__c,(select id from contacts) from account where id in :setId];
        for(Account acc : lstAccount){
            List<Contact> lstCont = acc.contacts;
            acc.Number_of_Contacts__c = lstCont.size();
        }
        update lstAccount;
    }
}

AccountProcessorTest

@isTest
public class AccountProcessorTest {
    public static testMethod void testAccountProcessorTest(){
        Test.startTest();
        Account a = new Account();
        a.Name = 'Test Account';
        insert a;

        Contact cont = new Contact();
        cont.FirstName ='Bob';
        cont.LastName ='Masters';
        cont.AccountId = a.Id;
        insert cont;

        Set<Id> setAccId = new Set<ID>();
        setAccId.add(a.Id);

        AccountProcessor.countContact(setAccId);       

        Account acc = [select Number_of_Contacts__c from Account where id = :a.id LIMIT 1];
        System.assertEquals(Integer.valueOf(acc.Number_of_Contacts__c) ,1);
        Test.stopTest();
    } 
}
3

Change the method name from countContact to countContacts as mentioned in the specified trailhead module - Use Future Methods:

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.

0

Test.startTest();

AccountProcessor.countContacts(setAccId);

//Just calling the future method between Test.startTest() and Test.stopTest() will do. Creating test data and System.assert statements can be done outside.

Test.stopTest();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.