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
community Name.ax928community Name.ax928 

Help needed to write test class for trigger

Hi,

i am a beginner i managed to create the below trigger to update the primary fields in contact from otc contact .  

 

can any one help me writing a test class for it.

 

thanks in advance for your help.

 

trigger UpdatePrimaryOTCContact on OTC_Contact__c (after insert, after update) {
    List <String> ContactIds= new List<String>();
     For(OTC_Contact__c otc : trigger.new)
     {
       ContactIds.add(otc.Contact__c);
     }
     List<Contact>Contacts = [Select Id, Name, Primary_OTC_Contact__c, Primary_CheckPoint_Ship_To__c, Primary_Checkpoint_Learning_Ship_To__c From Contact Where id IN:ContactIds];
     Map<String,Contact>ContactMap = New Map<String,Contact>(); 
     For(Contact ct : Contacts)
     {
       ContactMap.Put(ct.id, ct);
     }
     List<Contact> ConUpdates = New List<Contact>();
     For(OTC_Contact__c otc : Trigger.new)
     {
       system.debug('Entered for loop');
        if(otc.Is_Primary__c==TRUE && otc.Is_Primary_Checkpoint_Ship_To__c==TRUE
                        && otc.Is_Primary_Checkpoint_Learning_Ship_To__c==TRUE)
        {
         Contact con = ContactMap.get(otc.Contact__c);
                
                system.debug(con.Id);
                con.Primary_OTC_Contact__c=otc.id;
                con.Primary_CheckPoint_Ship_To__c=otc.id;
                con.Primary_Checkpoint_Learning_Ship_To__c=otc.id;
                ConUpdates.add(con);
         }
       else if(otc.Is_Primary__c==TRUE && otc.Is_Primary_Checkpoint_Ship_To__c==TRUE)
        {
         Contact con = ContactMap.get(otc.Contact__c);
                
                system.debug(con.Id);
                con.Primary_OTC_Contact__c=otc.id;
                con.Primary_CheckPoint_Ship_To__c=otc.id;
                ConUpdates.add(con);
         }
       else if(otc.Is_Primary_Checkpoint_Ship_To__c==TRUE && otc.Is_Primary_Checkpoint_Learning_Ship_To__c==TRUE)                        
        {
         Contact con = ContactMap.get(otc.Contact__c);
                
                system.debug(con.Id);
                con.Primary_CheckPoint_Ship_To__c=otc.id;
                con.Primary_Checkpoint_Learning_Ship_To__c=otc.id;
                ConUpdates.add(con);
         }
       else if(otc.Is_Primary__c==TRUE && otc.Is_Primary_Checkpoint_Learning_Ship_To__c==TRUE)
         {
           Contact con = ContactMap.get(otc.Contact__c);
                 
               system.debug(con.Id);
               con.Primary_OTC_Contact__c=otc.id;
               con.Primary_Checkpoint_Learning_Ship_To__c=otc.id;
               ConUpdates.add(con);
         }
     else if(otc.Is_Primary__c==TRUE)
        {
         Contact con = ContactMap.get(otc.Contact__c);
                
                system.debug(con.Id);
                con.Primary_OTC_Contact__c=otc.id;
                ConUpdates.add(con);
         }
     else if(otc.Is_Primary_Checkpoint_Ship_To__c==TRUE)
        {
         Contact con = ContactMap.get(otc.Contact__c);
                
                system.debug(con.Id);
                con.Primary_CheckPoint_Ship_To__c=otc.id;
                ConUpdates.add(con);
         }
        else if(otc.Is_Primary_Checkpoint_Learning_Ship_To__c==TRUE)
        {
         Contact con = ContactMap.get(otc.Contact__c);
                
                system.debug(con.Id);
                con.Primary_Checkpoint_Learning_Ship_To__c=otc.id;
                ConUpdates.add(con);
         }
      }
     
   update ConUpdates; 
   }

Best Answer chosen by Admin (Salesforce Developers) 
Pradeep_NavatarPradeep_Navatar

Tryout the code given below :

@isTest
class myTestClass
{
 static testMethod void myTestMethod()
 {
   List<OTC_Contact__c> otcCon= new List<OTC_Contact__c>();
  // insert contact record
  Contact con=new Contact(lastName='test Contact');
  insert con;
 
  // create OTC_Contact__c record
  OTC_Contact__c otc=new OTC_Contact__c();
  otc.contact__c=con.id;
  otc.Is_Primary__c==TRUE;
  otc.Is_Primary_Checkpoint_Ship_To__c==TRUE;
  otc.Is_Primary_Checkpoint_Learning_Ship_To__c==TRUE;
  otcCon.add(otc);
 
  /******************* Second case ******************/
  OTC_Contact__c otc1=new OTC_Contact__c();
  otc1.contact__c=con.id;
  otc1.Is_Primary__c==TRUE;
  otc1.Is_Primary_Checkpoint_Ship_To__c==TRUE;
  otcCon.add(otc1);
 
  /******************* Third case ******************/
  OTC_Contact__c otc2=new OTC_Contact__c();
  otc2.contact__c=con.id;
  otc2.Is_Primary_Checkpoint_Ship_To__c==TRUE;
  otc2.Is_Primary_Checkpoint_Learning_Ship_To__c==TRUE;
  otcCon.add(otc2);
 
  /******************* Forth case ******************/
  OTC_Contact__c otc3=new OTC_Contact__c();
  otc3.contact__c=con.id;
  otc3.Is_Primary__c==TRUE;
  otc3.Is_Primary_Checkpoint_Learning_Ship_To__c==TRUE;
  otcCon.add(otc3);
 
  /******************* Fifth case ******************/
  OTC_Contact__c otc4=new OTC_Contact__c();
  otc4.contact__c=con.id;
  otc4.Is_Primary__c==TRUE;
  otcCon.add(otc4);
 
  /******************* Sixth case ******************/
  OTC_Contact__c otc5=new OTC_Contact__c();
  otc5.contact__c=con.id;
  otc5.Is_Primary_Checkpoint_Learning_Ship_To__c==TRUE;
  otcCon.add(otc5);
 
  /******************* Sixth case ******************/
  OTC_Contact__c otc6=new OTC_Contact__c();
  otc6.contact__c=con.id;
  otc6.Is_Primary_Checkpoint_Ship_To__c==TRUE;
  otcCon.add(otc6);
 
  insert otcCon;
 }
}

All Answers

Pradeep_NavatarPradeep_Navatar

Tryout the code given below :

@isTest
class myTestClass
{
 static testMethod void myTestMethod()
 {
   List<OTC_Contact__c> otcCon= new List<OTC_Contact__c>();
  // insert contact record
  Contact con=new Contact(lastName='test Contact');
  insert con;
 
  // create OTC_Contact__c record
  OTC_Contact__c otc=new OTC_Contact__c();
  otc.contact__c=con.id;
  otc.Is_Primary__c==TRUE;
  otc.Is_Primary_Checkpoint_Ship_To__c==TRUE;
  otc.Is_Primary_Checkpoint_Learning_Ship_To__c==TRUE;
  otcCon.add(otc);
 
  /******************* Second case ******************/
  OTC_Contact__c otc1=new OTC_Contact__c();
  otc1.contact__c=con.id;
  otc1.Is_Primary__c==TRUE;
  otc1.Is_Primary_Checkpoint_Ship_To__c==TRUE;
  otcCon.add(otc1);
 
  /******************* Third case ******************/
  OTC_Contact__c otc2=new OTC_Contact__c();
  otc2.contact__c=con.id;
  otc2.Is_Primary_Checkpoint_Ship_To__c==TRUE;
  otc2.Is_Primary_Checkpoint_Learning_Ship_To__c==TRUE;
  otcCon.add(otc2);
 
  /******************* Forth case ******************/
  OTC_Contact__c otc3=new OTC_Contact__c();
  otc3.contact__c=con.id;
  otc3.Is_Primary__c==TRUE;
  otc3.Is_Primary_Checkpoint_Learning_Ship_To__c==TRUE;
  otcCon.add(otc3);
 
  /******************* Fifth case ******************/
  OTC_Contact__c otc4=new OTC_Contact__c();
  otc4.contact__c=con.id;
  otc4.Is_Primary__c==TRUE;
  otcCon.add(otc4);
 
  /******************* Sixth case ******************/
  OTC_Contact__c otc5=new OTC_Contact__c();
  otc5.contact__c=con.id;
  otc5.Is_Primary_Checkpoint_Learning_Ship_To__c==TRUE;
  otcCon.add(otc5);
 
  /******************* Sixth case ******************/
  OTC_Contact__c otc6=new OTC_Contact__c();
  otc6.contact__c=con.id;
  otc6.Is_Primary_Checkpoint_Ship_To__c==TRUE;
  otcCon.add(otc6);
 
  insert otcCon;
 }
}

This was selected as the best answer
community Name.ax928community Name.ax928

thanks for the help pradeep,

 

i tried that, i am able to save the code, when i do test run the code covering 91% but its says the test failure and giving me an error,

 

here is the error.

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdatePrimaryOTCContact: execution of AfterInsert caused by: System.ListException: Before Insert or Upsert list must not have two identically equal elements Trigger.UpdatePrimaryOTCContact: line 81, column 4: []



Class.TestUpdatePrimaryOTCContact.myTestMethod: line 72, column 3 External entry point

 

can you please help in that its urgent.

community Name.ax928community Name.ax928

thanks for the help pradeep, i did some changes in that it working fine 

 

thanks again,