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

i have a trigger which displays list of contact address on account 
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;  
}


i m facing issue how to match the strings or may be i m wrong
@isTest
public class testlistOfContactsOnAccount
{
public static testMethod void accountaddress()
{


account acc = new account();
acc.Name='account name';
//acc.Final_address_of_contacts__c ='rp bagh,karol bagh';
insert acc;


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

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

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


insert clist;

account updt =[ select id,Final_address_of_contacts__c from account where id=:acc.id];
system.assertEquals('rp bagh,karol bagh', 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);

  }
  }


any help ?
SonamSonam (Salesforce Developers) 
As you have described what the trigger, following is what you can do in the test class:
1)Create a method which creates Account
2)Create a method which created two contacts associated with a account passes in as parameter
3)Start test : insert the account
get the ID of the inserted Account
insert contacts using the create contacts using the Account ID
Stop test
Using system.assert compare the Account Address with string created combining the addresses from the contacts.
MithunPMithunP

Hi Chitral,

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

@isTest(SeeAllData=true)

Best Regards,
Mithun.
SonamSonam (Salesforce Developers) 
@Chitral, I got your question related to the 3rd point

3)Start test : insert the account
get the ID of the inserted Account
insert contacts using the create contacts using the Account ID
Stop test

Since the work of the trigger is to update the account address with the concatenated string of all its contacts's addresses - therefore, we need to first create an account, then create and insert  two contacts associated to that account such that the trigger fires and you can test if its working or not.

System.assert is used to compare the value you think should have been updated on the account address field to the value that the trigger has updated on the field.

Hope this makes things clear..