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
rajjjjrajjjj 

trigger code coverage

Can someone write a test class for this..

 

trigger FindConfAttendeeDuplicate on Conf_attendee__c (before insert,before update) {
    if(trigger.isinsert){
    Conf_attendee__c Con =trigger.new[0];
    List<Conf_attendee__c> ListCon = [select id from Conf_attendee__c where 
                                      Conference__c =:Con.Conference__c AND Contact__c =:Con.Contact__c];
    system.debug('------------------>'+Con.Conference__r.Conference_Date__c);
    
    List<Conf_attendee__c> compareDate = [select id,Conference__c,Conference__r.Conference_Date__c,Contact__c from Conf_attendee__c];
    for(Conf_attendee__c CompareEachdate : compareDate){
      
        if (CompareEachdate.Conference__r.Conference_Date__c == Con.Conference__r.Conference_Date__c){
            if(CompareEachdate.Contact__c == Con.Contact__c){
            Con.Contact__c.addError('Hello');
        }
        }else {
        try{
            insert Con;
           }catch(Exception ex){
           
           }
        }
          
    }
    
    system.debug('-------------'+ListCon);                                 
    if(ListCon.size() == 0){
    try{
        insert Con;
       }catch(Exception ex){
       }
    }
    else {
        Con.Contact__c.addError('Cannot Insert');
    }
    }
}

 I have written in this way but its not covering 75%

 

@istest
private class testtrigger{
   static testmethod void testTriggMethod()
   {
       conference__c conf = new conference__c(Name='conf1');
       insert conf;
       
       Contact c = new Contact(lastName='contact1');
       insert c;
       
       Conf_attendee__c conatt = new Conf_attendee__c(conference__c = conf.Id,contact__c = c.Id);
       //con.conference__c = 'Samplecon';
       //con.contact__c = 'Contact1';
       insert conatt;
       
       List<Conf_attendee__c> listcon = [select id from Conf_attendee__c];
       List<Conf_attendee__c> compareDate = [select id,Conference__c,Conference__r.Conference_Date__c,Contact__c from Conf_attendee__c];
       
      // for(Integer i=0; i<compareDate.size();i++ )
      // {
          if (compareDate.get(0) .Conference__r.Conference_Date__c == Conatt.Conference__r.Conference_Date__c)
          {
            if(compareDate.get(0).Contact__c == Conatt.Contact__c)
            {
            system.assert(true);
            }
          }
          else 
            {
            try{
                insert Conatt;
                //System.assertEquals('conf1',conference__c .Name);
               }
           catch(Exception ex){
           
           }
        }
   //}
   
   }
}

 

vishal@forcevishal@force

Hi Raj,

 

Before going into the test coverage. I see that you have an insert statement on the same record that is getting processed in your trigger's before insert.

 

Are you sure your trigger works correctly? Don't test for the error case, test it for the correct case and see if it actually gets inserted.

rajjjjrajjjj

Thats working fine. Before insert i am processing some logic .i.e checking with old records of junction object with new value (trigger.new) and if it doesnt meet the condition, then i am inserting it.

 

I got to cover the test class....for checking that date condition.

Before inserting(after condition check of date) i need to setup some data(I forgot to setup data)....so i am not able to cover it before.

Now it got covered 87%.

 

But i am able to cover first condition of date of conference but not the second condition.

Can i know the better way to do code coverage for iterating over list and checking condition.

 

 

Thanks,

Raj.