function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chitral ChaddaChitral Chadda 

test class

scenario :
suppost i hav accnt a1 and related contact as c1, c2
there addres_for_contact__c field on contact 
and final_address_of_contact__c field on account
if for c1 ...addres_for_contact__c='a';
for c2 ..addres_for_contact__c='b';
then on account  ..final address of contact = a,b
 
trigger listOfContactsOnAccount on contact (after insert , after update,after delete){ 
  set<id> accountIdSet = new set<id>();
  
  if( trigger.isInsert|| trigger.isUpdate){
    for(contact c : trigger.new){
      accountIdSet.add(c.AccountId);
    }
  }

  if(trigger.isDelete){
    for(contact c: trigger.old){
      accountIdSet.add(c.AccountId);
    }
  }
            
  //2.create map
  List<contact> cont = [ Select AccountId, Address_for_contact__c from contact where AccountId IN : accountIdSet ]; 
    
    //using a map of lists instead of a map of contacts
    Map<id,List<contact>> accountContactsMap = new map<id, List<contact>>();
    for(contact c : cont){
      //check if a entry in the map exists for the account
      if(!accountContactsMap.containsKey(c.accountId)){
        //if it doesnt then create one
        accountContactsMap.put(c.accountId, new List<Contact>());
      }
      //get the contact list for the account and add the contact in
      accountContactsMap.get(c.accountId).add(c);
    }
  
    List<Account> accountsToUpdate= new List<Account>();
    string l='';
    
              for(contact cn :trigger.new)
   {
     if(accountContactsMap != null)
         { 
              list<contact> aci = accountContactsMap.get(cn.AccountId);  
              account ac = new account(id=cn.AccountId);
                  {
                    for(contact c : aci)
                         
                         { if(l==null)
                          
                              {
                              l =   c.Address_for_Contact__c ;        
                              }
                             else
                             {
                             l= l+','+c.Address_for_contact__c;
                             }
                         }     
                             
               
                          ac.Final_address_of_contacts__c = l;
                          accountsToUpdate.add(ac);
                    }  
              
          }
          
   }
 update accountsToUpdate;  
}

test class:
@isTest
public class testlistOfContactsOnAccount
{
public static testMethod void accountaddress()
{


account acc = new account();
acc.Name='account name';
//acc.Final_address_of_contacts__c ='agsah,bawdgh';
insert acc;


List<Contact> clist = new List<Contact>();

contact ct1 = new contact(AccountId=acc.Id);
ct1.LastName='abc';
ct1.Address_for_contact__c='agsah';
clist.add(ct1);

contact ct2 = new contact(AccountId=acc.Id);
ct2.LastName='xyz';
ct2.Address_for_contact__c='bawdgh';
clist.add(ct2);
//ct2.accountid=acc.id

insert clist;

string l = null;
for(contact c : cList)
{
 if(l == null)
 { l= c.Address_for_contact__c;
 
 }
 else
 {
 l=l+','+c.Address_for_contact__c;
 }
}


acc.Final_address_of_contacts__c=l;
update acc;
account updt =[ select id,Final_address_of_contacts__c from account where id=:acc.id];
system.assertEquals(l, updt.Final_address_of_contacts__c);

  }
  }

when i do run test it shows pass
 but there is 0 % code coverage 
is the test class okay ?
Best Answer chosen by Chitral Chadda
MithunPMithunP
Hi Chitral,

I made some more changes in your trigger, you can try this one. Seems your test class is ok.
 
trigger listOfContactsOnAccount on contact (after insert , after update,after delete){
  set<id> accountIdSet = new set<id>();
  if( trigger.isInsert|| trigger.isUpdate){
    for(contact c : trigger.new){
      accountIdSet.add(c.AccountId);
    }
  }
  if(trigger.isDelete){
    for(contact c: trigger.old){
      accountIdSet.add(c.AccountId);
    }
  }           
  //2.create map
  List<contact> cont = [ Select AccountId, Address_for_contact__c from contact where AccountId IN : accountIdSet ];
    //using a map of lists instead of a map of contacts
    Map<id,account> accountMap = new Map<id,account>([select id,Final_address_of_contacts__c From Account Where id in: accountIdSet]);
    Map<id,List<contact>> accountContactsMap = new map<id, List<contact>>();
    for(contact c : cont){
      //check if a entry in the map exists for the account
      if(!accountContactsMap.containsKey(c.accountId)){
        //if it doesnt then create one
        accountContactsMap.put(c.accountId, new List<Contact>());
      }
      //get the contact list for the account and add the contact in
      accountContactsMap.get(c.accountId).add(c);
    }
    List<Account> accountsToUpdate= new List<Account>();
    
              
     if(accountContactsMap != null)
         {
         for(Id acid :accountContactsMap.keySet())
         {
             string l='';
              list<contact> aci = accountContactsMap.get(acid); 
              account ac = accountMap.get(acid);
                    for(contact c : aci)
                         { 
                             if(l==null)
                              {
                              l =   c.Address_for_Contact__c ;       
                              }
                             else
                             {
                             l= l+','+c.Address_for_contact__c;
                             }
                         }    
                          ac.Final_address_of_contacts__c = l;
                          accountsToUpdate.add(ac);
        } 
   }
   if(accountsToUpdate.size() > 0){
 update accountsToUpdate; 
 }
}

Best Regards,
Mithun.

All Answers

MithunPMithunP
Hi Chitral,

Use (SeeAllData=true) like below at starting of test class.

@isTest(SeeAllData=true)

Best Regards,
Mithun.
Chitral ChaddaChitral Chadda

i get error
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, listOfContactsOnAccount: execution of AfterInsert

caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []

Trigger.listOfContactsOnAccount: line 62, column 1: []


i think there is issue with test class  , the following part shud not be there :
string l = null;

30 for(contact c : cList)

31 {

32  if(l == null)

33  { l= c.Address_for_contact__c;

34   

35  }

36  else

37  {

38  l=l+','+c.Address_for_contact__c;

39  }

40 }

i shud do
account updt =[ select id,Final_address_of_contacts__c from account where id=:acc.id];
system.assertEquals('a,b', updt.Final_address_of_contacts__c);

when i try this i get error System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, listOfContactsOnAccount: execution of AfterInsert

caused by: System.ListException: Duplicate id in list: 0019000001CHZClAAP

Trigger.listOfContactsOnAccount: line 62, column 1: []
MithunPMithunP
Can you share your updated trigger and test class code
Chitral ChaddaChitral Chadda
sure
trigger listOfContactsOnAccount on contact (after insert , after update,after delete){ 
  set<id> accountIdSet = new set<id>();
  
  if( trigger.isInsert|| trigger.isUpdate){
    for(contact c : trigger.new){
      accountIdSet.add(c.AccountId);
    }
  }

  if(trigger.isDelete){
    for(contact c: trigger.old){
      accountIdSet.add(c.AccountId);
    }
  }
            
  //2.create map
  List<contact> cont = [ Select AccountId, Address_for_contact__c from contact where AccountId IN : accountIdSet ]; 
    
    //using a map of lists instead of a map of contacts
    Map<id,List<contact>> accountContactsMap = new map<id, List<contact>>();
    for(contact c : cont){
      //check if a entry in the map exists for the account
      if(!accountContactsMap.containsKey(c.accountId)){
        //if it doesnt then create one
        accountContactsMap.put(c.accountId, new List<Contact>());
      }
      //get the contact list for the account and add the contact in
      accountContactsMap.get(c.accountId).add(c);
    }
  
    List<Account> accountsToUpdate= new List<Account>();
    string l='';
    
              for(contact cn :trigger.new)
   {
     if(accountContactsMap != null)
         { 
              list<contact> aci = accountContactsMap.get(cn.AccountId);  
              account ac = new account(id=cn.AccountId);
                  {
                    for(contact c : aci)
                         
                         { if(l==null)
                          
                              {
                              l =   c.Address_for_Contact__c ;        
                              }
                             else
                             {
                             l= l+','+c.Address_for_contact__c;
                             }
                         }     
                             
               
                          ac.Final_address_of_contacts__c = l;
                          accountsToUpdate.add(ac);
                    }  
              
          }
          
   }
 update accountsToUpdate;  
}

i made little changes in test class only 
@isTest(SeeAllData=true)
public class testlistOfContactsOnAccount
{
public static testMethod void accountaddress()
{


account acc = new account();
acc.Name='account name';
//acc.Final_address_of_contacts__c ='agsah,bawdgh';
insert acc;


List<Contact> clist = new List<Contact>();
//id accid=acc.id;
contact ct1 = new contact(LastName='test1',AccountId = acc.Id);
//ct1.Accountid=accid;
//ct1.LastName='abc';
ct1.Address_for_contact__c='a';
clist.add(ct1);

contact ct2 = new contact(LastName='test2',AccountId = acc.Id);
//ct2.AccountId=accid;
//ct2.LastName='xyz';
ct2.Address_for_contact__c='b';
clist.add(ct2);
//ct2.accountid=acc.id
//test.startTest();


insert clist;

//string l = null;
//for(contact c : cList)
//{
 //if(l == null)
 //{ l= c.Address_for_contact__c;
 
// }
// else
// {
// l=l+','+c.Address_for_contact__c;
// }
//}


//acc.Final_address_of_contacts__c=l;
//update acc;
//test.stopTest();

account updt =[ select id,Final_address_of_contacts__c from account where id=:acc.id];
system.assertEquals('a,b', updt.Final_address_of_contacts__c);

//List<Contact> deleteList = new List<Contact>();

  //  deleteList.add(ct1);
  //  delete deleteList;
    
//account updt1 =[ select id,Final_address_of_contacts__c from account where id=:acc.id];
//system.assertEquals('karol bagh', updt1.Final_address_of_contacts__c);

  }
  }

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, listOfContactsOnAccount: execution of AfterInsert

caused by: System.ListException: Duplicate id in list: 0019000001CHah8AAD

Trigger.listOfContactsOnAccount: line 62, column 1: []

i m stuck at this badly.. 
Chitral ChaddaChitral Chadda
thks.. i used ur trigger  with 1 change u included
Map<id,account> accountMap = new Map<id,account>([select id,Final_address_of_contacts__c FromAccount Where id in: accountIdSet]);

here is the test class
@isTest(SeeAllData=true)
public class testContactAddressOnAccount 
{
public static testMethod void accaddress()
{


account acc = new account();
acc.Name='account name';

insert acc;


List<Contact> clist = new List<Contact>();

contact ct1 = new contact(LastName='test1',AccountId = acc.Id);

ct1.Address_for_contact__c='a';
clist.add(ct1);

contact ct2 = new contact(LastName='test2',AccountId = acc.Id);

ct2.Address_for_contact__c='b';
clist.add(ct2);



insert clist;



account updt =[ select id,Final_address_of_contacts__c from account where id=:acc.id];
system.assertEquals('a,b', updt.Final_address_of_contacts__c);


  }
  }

i get same error:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ContactAddressOnAccount: execution of AfterInsert

caused by: System.ListException: Duplicate id in list: 0019000001CHlcDAAT

Trigger.ContactAddressOnAccount: line 52, column 1: []
MithunPMithunP
Hi Chitral,

I made some more changes in your trigger, you can try this one. Seems your test class is ok.
 
trigger listOfContactsOnAccount on contact (after insert , after update,after delete){
  set<id> accountIdSet = new set<id>();
  if( trigger.isInsert|| trigger.isUpdate){
    for(contact c : trigger.new){
      accountIdSet.add(c.AccountId);
    }
  }
  if(trigger.isDelete){
    for(contact c: trigger.old){
      accountIdSet.add(c.AccountId);
    }
  }           
  //2.create map
  List<contact> cont = [ Select AccountId, Address_for_contact__c from contact where AccountId IN : accountIdSet ];
    //using a map of lists instead of a map of contacts
    Map<id,account> accountMap = new Map<id,account>([select id,Final_address_of_contacts__c From Account Where id in: accountIdSet]);
    Map<id,List<contact>> accountContactsMap = new map<id, List<contact>>();
    for(contact c : cont){
      //check if a entry in the map exists for the account
      if(!accountContactsMap.containsKey(c.accountId)){
        //if it doesnt then create one
        accountContactsMap.put(c.accountId, new List<Contact>());
      }
      //get the contact list for the account and add the contact in
      accountContactsMap.get(c.accountId).add(c);
    }
    List<Account> accountsToUpdate= new List<Account>();
    
              
     if(accountContactsMap != null)
         {
         for(Id acid :accountContactsMap.keySet())
         {
             string l='';
              list<contact> aci = accountContactsMap.get(acid); 
              account ac = accountMap.get(acid);
                    for(contact c : aci)
                         { 
                             if(l==null)
                              {
                              l =   c.Address_for_Contact__c ;       
                              }
                             else
                             {
                             l= l+','+c.Address_for_contact__c;
                             }
                         }    
                          ac.Final_address_of_contacts__c = l;
                          accountsToUpdate.add(ac);
        } 
   }
   if(accountsToUpdate.size() > 0){
 update accountsToUpdate; 
 }
}

Best Regards,
Mithun.
This was selected as the best answer
Chitral ChaddaChitral Chadda

GRATE MAN !! got 89 % coverage
I m little unsure what went wrong with my trigger as although it was working grate

for(contact cn :trigger.new)
   {
     if(accountContactsMap != null)
         { 
              list<contact> aci = accountContactsMap.get(cn.AccountId);  
              account ac = new account(id=cn.AccountId);
                  {
                    for(contact c : aci)
                         
                         { if(l==null)
                          
                              {
                              l =   c.Address_for_Contact__c ;        
                              }
                             else
                             {
                             l= l+','+c.Address_for_contact__c;
                             }
                         }     
                             
               
                          ac.Final_address_of_contacts__c = l;
                          accountsToUpdate.add(ac);
                    }  
              
          }
          
   }
 update accountsToUpdate;  
}
 


because this..
account ac = new account(id=cn.AccountId);

would feth the account id of the related account 

its just that the trigger you gave i think is more proficient
so i m unable to get the point which part is wrong in my trigger

MithunPMithunP
Hi Chitral,

Initially we implemented main loop for contact (trigger.new), it will work for only one contact everytime but if you create/update bulk contacts it won't work. So we changed our main loop for Account, now it can handle bulk records.

Best Regards,
Mithun.
Chitral ChaddaChitral Chadda
User-added image
Thankyou  alright,  when i checked the trigger 
i get httphttp://,c,b  in the field account
http:// any idea hw can i remove this
MithunPMithunP
Hi Chitral,

Seems Final Address field in Account is URL datatype, if so change it to Text datatype.

Mark it as Best answer, if it solves your problem.

Best Regards,
Mithun.
Chitral ChaddaChitral Chadda
thnx .. worked !!