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
mallikharjunarao gundamallikharjunarao gunda 

Testclass for trgger code

Hai hall
 i got only 88% code only

i wrote my trigger code & testclass below  please help me.
Red mark show only a.name.adderror in code coverage.
trigger code:
trigger validationoncontact on Account (before insert) {
    set<string>setv=new set<string>();
       // soql query to return the no of records.
    list<contact>con=[select id,lastname from contact];
       // if size of the records is more than 0 ,then proceed our logic
     if(con.size()>0)
    { 
        // Return the number of records one by one
       for(contact c:con)
       {
           // add a contact lastname to setv  to comapre the values. 
       setv.add(c.lastname);
        }
        }
    // "trigger.new" which holds the list of new records, whatever trying to insert the records. 
    for(account a:trigger.new){
        
       // it returns a true if the string contains specfied character otherwise it return false.
        
    if(setv.contains(a.name)){
        
            a.name.adderror('Duplicate : Already contact is Existed with this name');
        }
    }
  }

test class 
@istest
public class testclassforvalidationcontact {
@istest
    static void testme(){
     contact c=new contact(lastname='arjun');
        insert c;
        string names='';
        list<contact>cc=[select id,lastname from contact where id=:c.id];
        for(contact c1:cc){
            names=c1.lastname;
        }
        account a1=new account(name='mallik');
        try{
            insert a1;
            system.assertnotEquals(a1.name, names);
        }catch(exception e){
            System.debug('should not be created duplicate record');
        }
    }
}

 
Pramod GowdaPramod Gowda
Hi,

Try to insert an  Account whose Name == Contact's LastName.

You have written the code as, error if the account name matchs any of the contacts Lastname.
Contact c = new Contact(LastName = 'Gowda');
insert c;
try{
     Account a  = new Account(Name = 'Gowda');
     insert a;
}
catch(Exception e){
      system.assert(e.getMessage().contains('Duplicate:Already contact is Existed with this name'));
}

Thanks.
Ankur Saini 9Ankur Saini 9
Hi mallikharjunarao gunda

try this:
@istest
public class testclassforvalidationcontact {
@istest
    static void testme(){
     contact c=new contact(lastname='arjun');
        insert c;
        string names='';
        list<contact>cc=[select id,lastname from contact where id=:c.id];
        for(contact c1:cc){
            names=c1.lastname;
        }
        account a1=new account(name='mallik');
        try{
            insert a1;
            system.assertnotEquals(a1.name, names);
        }catch(exception e){
            System.debug('should not be created duplicate record');
        }
    }
    
    @istest
    static void testme2(){
     contact c=new contact(lastname='arjun');
        insert c;
        string names='';
        list<contact>cc=[select id,lastname from contact where id=:c.id];
        for(contact c1:cc){
            names=c1.lastname;
        }
        account a1=new account(name='arjun');
        try{
            insert a1;
            system.assertnotEquals(a1.name, names);
        }catch(exception e){
            System.debug('should not be created duplicate record');
        }
    }
}

Thanks 
Ankur Saini
http://mirketa.com
mallikharjunarao gundamallikharjunarao gunda
Thank you Ankur