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
Lakshmi SLakshmi S 

test class for trigger on contact object

Hi Dev's

trigger onecontactforoneaccount on Contact (before insert,before update) {

    if(Trigger.isInsert || Trigger.isUpdate){
        for(Contact con : Trigger.New){
            for(Contact c : [Select id,accountid from contact]){
                if(con.accountid == c.accountid && con.accountid != Trigger.oldmap.get(con.id).accountid){
                    Trigger.oldmap.get(con.id).addError('Duplicate account id');
                }
        }
        }
    }
}


Regards
Lakshmi
Rohit SharmaGRohit SharmaG
Hi, Create test class with Account and Contact test data and run.
Lakshmi SLakshmi S
Hi Rohit

I have writtern test class, but it was not covered 100%.
Please find below code...

@isTest
private class TestOnecontactforoneaccount {
    static testMethod void testOneCon(){
        
        Account a = new Account(name='test acc',phone='9494146144');
        insert a;
        Account a2 = new Account(name='test acc 2',phone='9494146144');
        insert a2;
        Contact con = new Contact(accountid=a.id,lastname='test con',email='lnarasimha823@gmail.com');
        insert con;
        Contact con2 = new Contact(accountid=a.id,lastname='test con2',email='lnarasimha823@gmail.com');
        
        Test.startTest();
        try{
            insert con2;
        }
        catch(Exception e){
           system.debug('Duplicate Account Id');
        }
        
        
        Test.stopTest();
        
    }
}

Regards
Lakshmi
Vivek C 1Vivek C 1
Hi Lakshmi,

The code 
Trigger.oldmap.get(con.id).addError('Duplicate account id');
does'nt covers the test data. Because Trigger.oldmap context variable is available in update and delete triggers. So i have just updated a new value with the previous value.

The below is my code, negativeTest() is what i have cerated.
@isTest
private class TestOnecontactforoneaccount {
    static testMethod void testOneCon(){
        
        Account a = new Account(name='test acc',phone='9494146144');
        insert a;
        Account a2 = new Account(name='test acc 2',phone='9494146144');
        insert a2;
        Contact con = new Contact(accountid=a.id,lastname='test con',email='lnarasimha823@gmail.com');
        insert con;
        Contact con2 = new Contact(accountid=a.id,lastname='test con2',email='lnarasimha823@gmail.com');
        
        Test.startTest();
        try{
            insert con2;
        }
        catch(Exception e){
           system.debug('Duplicate Account Id');
        }
        Test.stopTest();
        }    
        static testMethod void negativeTest(){    
        Test.startTest();
        Account a = new Account(name='test acc1',phone='9494146144');
        insert a;
        Account a1 = new Account(name='test acc1',phone='9494146144');
        insert a1;   
        Contact con = new Contact(accountid=a.id,lastname='test con1',email='lnarasimha823@gmail.com');
        insert con;
        Contact con3 = new Contact(accountid=a1.id,lastname='test con',email='lnarasimha823@gmail.com');
         insert con3;
            con3.lastname = 'lastname';
            con3.accountid = a.id;
        try{
             update con3;
        }
        catch(Exception e){
           system.debug('Duplicate Account Id');
        }
        Test.stopTest();
        
    }
}
Please let us know if it does'nt works.

Regards,
Vivek

 
Amit Singh 1Amit Singh 1
Lakshmi, 

Try below code.
@isTest
private class TestOnecontactforoneaccount {
    static testMethod void testOneCon(){
        
        Account a = new Account(name='test acc',phone='9494146144');
        insert a;
        Account a2 = new Account(name='test acc 2',phone='9494146144');
        insert a2;
        Contact con = new Contact(accountid=a2.id,lastname='test con',email='lnarasimha823@gmail.com');
        insert con;
        Contact con2 = new Contact(accountid=a.id,lastname='test con2',email='lnarasimha823@gmail.com');
        
        Test.startTest();
        try{
            insert con2;
            con.AccountId = a.id;
            update con;
        }
        catch(Exception e){
           system.debug('Duplicate Account Id');
        }
        
        
        Test.stopTest();
        
    }
}
It will cover 100 % :)

Let me know if this helps.

Thanks,
Amit Singh.
 
Lakshmi SLakshmi S
Hi Vivek and Amit

Thanks for your reply.
Both code working fine.

Regards
Lakshmi.
 
Amit Singh 1Amit Singh 1
Lakshmi,

Mark as best answer if your issue has been resolved :)
Thanks,
Amit