+ Start a Discussion
Glenn Dailey 1Glenn Dailey 1 

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

The 'AccountProcessor' class did not achieve 100% code coverage via your test methods. Make sure that you chose 'Run All' tests in the Developer Console at least once before attempting to verify this challenge. I did the run all with no luck. I did notice that this is the new section that was just added and I ran into issues in the first unit too.

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.

public class AccountProcessor
{
  @future
  public static void countContacts(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;
  }
}

and
@IsTest
public class AccountProcessorTest {
    public static testmethod void TestAccountProcessorTest(){
        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);
 
        Test.startTest();
            AccountProcessor.countContacts(setAccId);
        Test.stopTest();
       
        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);
  }
 
}
Best Answer chosen by Glenn Dailey 1
Amit Chaudhary 8Amit Chaudhary 8
I am glad that works. PLease mark best answer and close this thread so that if some one has same issue that post can help other

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please check below post similer issue
1) https://developer.salesforce.com/forums/?id=906F0000000D8hwIAC
2) https://developer.salesforce.com/forums/?id=906F0000000DDdQIAW

Your Code look good to me.

NOTE:- Before checking the challange you need to click on Run Test Button on top of Test class. Once Test class execution will over then check code coverage. Then check your challange

Let us know if this will help you

Thanks
Amit Chaudhary
Glenn Dailey 1Glenn Dailey 1
I did the test getting the following
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountDeletion: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

External entry point: []
Amit Chaudhary 8Amit Chaudhary 8
It look like you have any "AccountDeletion" Trigger on Account object which is failing. Can you please deactivate the same trigger and try above code again.
Please follow below steo:-

1) Please deactivate trigger "AccountDeletion"
2) Execute Test Class again
3) Check your challange

Let us know if this will help you

 
Glenn Dailey 1Glenn Dailey 1
Yes that worked Thank you Amit
Amit Chaudhary 8Amit Chaudhary 8
I am glad that works. PLease mark best answer and close this thread so that if some one has same issue that post can help other
This was selected as the best answer
Nitesh Acharya 19Nitesh Acharya 19
The AccountProcesser can be modified slightly
public class AccountProcessor {
    @future
    public static void countContacts(set<ID> Accnts){
        List<Account> acc = [select Name from Account where Id IN :accnts];
        List<Account> xyz = new List<Account>();
        for(Account a:acc){
            a.Number_of_Contacts__c = [select Count() from Contact where AccountId =:a.Id];
            xyz.add(a);
        }
        update xyz;
    }
}



Test Class
@isTest
public class AccountProcessorTest {
    @isTest public static void accnt(){
        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);
        
        Test.startTest();
        AccountProcessor.countContacts(setAccId);
        Test.stopTest();
    }
}
Vishal_SharmaVishal_Sharma

What is the purppose of  (Set<id> setId) in

 public static void countContacts(Set<id> setId) Statement ?

Myrna LoveMyrna Love
Denise Love
I've tried everything I can think of at least 3 times and I still get the error message:
Challenge not yet complete... here's what's wrong:
The Apex class does not appear using the '@future' annotation.
What am I missing, please.
Robin Bansal 35Robin Bansal 35
Below solution worked for me:
public class AccountProcessor {
    @future
    public static void countContacts(List<Id> accountIds){
        List<Account> accounts = [Select Id, Name from Account Where Id IN : accountIds];
        List<Account> updatedAccounts = new List<Account>();
        for(Account account : accounts){
           account.Number_of_Contacts__c = [Select count() from Contact Where AccountId =: account.Id];
            System.debug('No Of Contacts = ' + account.Number_of_Contacts__c);
            updatedAccounts.add(account);
        }
        update updatedAccounts;
    }

}

Test class:
@isTest
public class AccountProcessorTest {
    @isTest
    public static void testNoOfContacts(){
        Account a = new Account();
        a.Name = 'Test Account';
        Insert a;
        
        Contact c = new Contact();
        c.FirstName = 'Bob';
        c.LastName =  'Willie';
        c.AccountId = a.Id;
        
        Contact c2 = new Contact();
        c2.FirstName = 'Tom';
        c2.LastName = 'Cruise';
        c2.AccountId = a.Id;
        
        List<Id> acctIds = new List<Id>();
        acctIds.add(a.Id);
        
        Test.startTest();
        AccountProcessor.countContacts(acctIds);
        Test.stopTest();
    }

}
Ronak BansalRonak Bansal
@istest 
public class AccountProcessorTest
{
    @istest
    public static void test()
    {
        account a=new account();
        a.name='reddy;
        a.emp_name__c='john';
        insert a;
        
        contact c=new contact();
        c.lastname='jjen';
        c.accountid=a.id;
*************error********************
        insert c;
*************error********************

        
        list<id> x=new list<id>();
        x.add(a.id);
           
        test.startTest();
        AccountProcessor.countContacts(x);
        test.stopTest();
        
        
    }
        
}


It is showing error in the statement " insert c "
please tell me why?

 
Rehan AhmedRehan Ahmed
Hi Amit, There is issue while running the test, Assertion failure is occuring,  

At this line :

System.assertEquals ( Integer.valueOf(ACC.Number_of_Contacts__c) ,1);

Error details:

System.AssertException: Assertion Failed: Expected: 2, Actual: 1

 
anuroop sudanagunta 7anuroop sudanagunta 7
Apex Class:

public class AccountProcessor {
    @future
    public static void CountContacts(Set<Id> Acctid){
        List<Account> AccList = [select id , number_of_contacts__c ,(Select id from Contacts) From Account where id In :Acctid];
       List<Account> updtLst = new List<Account>();
        for(Account a :AccList){
            a.Number_of_Contacts__c = a.contacts.size();
             UpdtLst.add(a);
        }
        Update updtLst;
    }
}

Test Class:

@isTest
public class AccountProcessorTest {
@isTest
    static void countContactsTest(){
        Account a = new Account(Name='Test');
             insert a;
        Contact c = new Contact(FirstName='testing',LastName='Test',Accountid=a.id);
             insert c;
        Contact cn = new Contact(FirstName='Nice',LastName='Test',AccountId = a.id);
             insert cn;
       list<Account> accList = [select id ,Number_of_Contacts__C,(Select id from Contacts) From Account where id = :a.Id ];
       set <Id> nlist = new set <Id>();
        for(Account at :accList){
          nlist.add(at.id);
        }
        test.startTest();
        AccountProcessor.CountContacts(nlist);
        test.stopTest();
        Account aCnt = [Select id , Number_of_Contacts__c from Account];
       system.assertEquals(2,aCnt.Number_of_Contacts__c); 

    }
}

 
ajay singh 167ajay singh 167
public class AccountProcessor {
    @future
    public static void countContacts(List<Id> accountIds){
        List<Account> accounts = [Select Id, Name from Account Where Id IN : accountIds];
        List<Account> updatedAccounts = new List<Account>();
        for(Account account : accounts){
           account.Number_of_Contacts__c = [Select count() from Contact Where AccountId =: account.Id];
            System.debug('No Of Contacts = ' + account.Number_of_Contacts__c);
            updatedAccounts.add(account);
        }
        update updatedAccounts;
    }

}

@IsTest
-------------Error----------------
Missing" " at'@'
---------------------------------------
public class AccountProcessorTest {
    @IsTest
    public static void testNoOfContacts(){
        Account a = new Account();
        a.Name = 'Test Account';
        Insert a;
        
        Contact c = new Contact();
        c.FirstName = 'Bob';
        c.LastName =  'Willie';
        c.AccountId = a.Id;
        
        Contact c2 = new Contact();
        c2.FirstName = 'Tom';
        c2.LastName = 'Cruise';
        c2.AccountId = a.Id;
        
        List<Id> acctIds = new List<Id>();
        acctIds.add(a.Id);
        
        Test.startTest();
       
        Test.stopTest();
    }

}

 
Ahmed Nawaz 14Ahmed Nawaz 14
Thanks for info You Like to Read Happy New Year 2020 (http://https://happynewyearcraze.com" target="_blank)
 
Alex sandro 1Alex sandro 1
It's a helpful article for update Account record. Thanks. If you like to know all kind of apk files, tamilrockers new link join us(bdtechsupport (https://bdtechsupport.com))