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
MokshadaMokshada 

how to write test class for trigger if we create or update account then related contact should be created with same name as account name

Trigger:-

trigger AccountContactTrig on Account (after insert,after update) {
     Set<Id> accIds= new Set<Id>();
    If(Trigger.isinsert && Trigger.isafter){
        List<Contact> cont = new List <Contact>();
        for(Account acc : trigger.new){
            Contact c = new Contact();
            c.AccountId=acc.id;
            c.LastName = acc.name;
            cont.add(c);
        }
        insert cont; 
    }
    if(Trigger.isupdate && Trigger.isafter){
        Set<String> accnames= new Set<String>();
        For(Account acc:Trigger.new){
            Account oldacc= Trigger.oldmap.get(acc.id);
            if(acc.name!= oldacc.Name){
                accnames.add(acc.name);
            }
        }
        Map<Id,Account> mapaccounts = new Map<Id,Account>([Select Name,(Select id, Lastname from contacts where Lastname in: accnames) from Account where id in:Trigger.new]);
        List<Contact> cont = new List <Contact>();
        if(mapaccounts.size()>0){
        for(Account ac: Trigger.new){
            if(Ac.Name != trigger.OldMap.get(Ac.Id).Name){               
            if(mapaccounts.containsKey(ac.id)){
                if(mapaccounts.get(ac.id).Contacts.size()==0){
                        Contact c = new Contact();
                        c.AccountId=ac.id;
                        c.LastName = ac.name;
                        cont.add(c);
                }
            }
        }
         insert cont;
    }
        }
    }
}
Best Answer chosen by Mokshada
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below test class.
 
@istest
public class Testcontact {
@isTest static void testcontactmethod() {
     Account acc= new Account();
    acc.name='Contact';
    insert acc;
    Contact con=[select id,Lastname from Contact];
    system.assertEquals('Contact', con.lastname);
    
    acc.name='sample';
    update acc;
     Contact con1=[select id,Lastname from Contact where lastname='sample'];
    system.assertEquals('sample', con1.lastname);
    }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mokshada,

I dont see any issue with the above code. I tried the same code in my org when trying to some other field than Name it is not creating new Contact.

There might be some automachine which is creating new Contact. Can you check deactivating all those and check.

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
MokshadaMokshada
Hi Praveen,
yes the trigger is working fine I was asking for test class.


Thanks,
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below test class.
 
@istest
public class Testcontact {
@isTest static void testcontactmethod() {
     Account acc= new Account();
    acc.name='Contact';
    insert acc;
    Contact con=[select id,Lastname from Contact];
    system.assertEquals('Contact', con.lastname);
    
    acc.name='sample';
    update acc;
     Contact con1=[select id,Lastname from Contact where lastname='sample'];
    system.assertEquals('sample', con1.lastname);
    }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
MokshadaMokshada
Hi praveen,
I want to test it for 100 accounts , how to do that?

Thanks,
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you check below .
@istest
public class Testcontact {
@isTest static void testcontactmethod() {
    List<Account> acclist= new List<Account>();
    For(Integer i=0;i<100;i++){
     Account acc= new Account();
    acc.name='Contact'+i;
        acclist.add(acc);
   
    }
    insert acclist;
    List<Contact> conlist=[select id,Lastname from Contact where LastName='Contact0'];
    system.assertEquals(1, conlist.size());
    Account acc=[select id,Name from Account limit 1];
    acc.name='sample';
    update acc;
     Contact con1=[select id,Lastname from Contact where lastname='sample'];
    system.assertEquals('sample', con1.lastname);
    }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
MokshadaMokshada
Hi praveen ,
Thank you It worked but I have one question why we have used "LastName='Contact0'" , what does it mean by contact0.
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mokshada,

We are inserting 100 Accounts with names as Contact0,Contact1,Contact2 ... etc. So these 100 Contacts should be inserted with same Lastnames. So we are checking on it.

Thanks,
 
MokshadaMokshada
Yes got it. Thank you!
saurabh singh 310saurabh singh 310
@isTest
public class AccountContactTrigTest {
    static testMethod void testInsert() {
        //create account and contact
        Account acc = new Account();
        acc.Name = 'Test Account';
        insert acc;
        
        Test.startTest();
            //insert account and check if contact is also created
            insert acc;
            acc = [SELECT Id, (SELECT Id FROM Contacts) FROM Account WHERE Id =: acc.Id];
            System.assertEquals(1, acc.Contacts.size());
        Test.stopTest();
    }
    
    static testMethod void testUpdate() {
        //create account and contact
        Account acc = new Account();
        acc.Name = 'Test Account';
        insert acc;
        
        Contact cont = new Contact();
        cont.AccountId = acc.Id;
        cont.LastName = 'Test Account';
        insert cont;
        
        Test.startTest();
            //update account name and check if contact last name is also updated
            acc.Name = 'New Test Account';
            update acc;
            cont = [SELECT Id, LastName FROM Contact WHERE Id =: cont.Id];
            System.assertEquals('New Test Account', cont.LastName);
        Test.stopTest();
    }
}