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
nikita dhamalnikita dhamal 

Increase code coverage for trigger

I have written a trigger and a test class for it.. But it is giving only 5 %code coverage.Can anyone help me to increase it..
Trigger:
trigger PopulateNewContact on Product_EntryStatus__c (after insert,after update) {
    for(Product_EntryStatus__c p : trigger.new)
    {
   if(p.Country__c!=null && p.Key_Opinion_Leader__c!=null)
     {
      system.debug('CONTACT INFO*************'+p.Name+'**************'+p.Key_Opinion_Leader__c);
    try{
   NewContact__c v = [select id,Awareness__c,Credibility__c,Connection__c,Convinced__c,Advocating__c,KOL_Title__c,Status__c,Product__c,Order__c,KOL_Name__c,Country__c from NewContact__c
   where KOL_Name__c =:p.Key_Opinion_Leader__c and Country__c=:p.Country__c and Product__c=:p.Product__c ];
           system.debug('KOL'+v);
             if(p.Process_Step__c == 'Awareness')
            {  
               v.Awareness__c = p.id;
            } 
             if(p.Process_Step__c == 'Credibility')
            {  v.Credibility__c = p.id;
            }
             if(p.Process_Step__c == 'Connection')
            {v.Connection__c = p.id;
  }
             if(p.Process_Step__c == 'Convinced')
            {
               
                v.Convinced__c = p.id;
                
          
            } if(p.Process_Step__c == 'Advocating')
            {
               
               v.Advocating__c = p.id;
          
            }
       
            if(p.Country__c != null)
            {
                v.Country__c = p.Country__c;
           
             } 
               if(p.KOL_Title__c != null)
            {
                v.KOL_Title__c = p.KOL_Title__c;
           
             }
   
            if(p.Status__c != null)
            {
                v.Status__c = p.Status__c;
           
             } 
              if(p.Product__c != null)
            {
                v.Product__c = p.Product__c;
           
             }
            if(p.Order__c != null)
            {
                v.Order__c = p.Order__c;
           
             } 
               if(p.Key_Opinion_Leader__c != null)
            {
                v.KOL_Name__c = p.Key_Opinion_Leader__c;
           
             } 
              update v;
          } 

      catch(Exception e)
      {    
          NewContact__c c = new NewContact__c();
        
           system.debug('Query*************'+c);
        
             if(p.Process_Step__c == 'Awareness')
            {                
               c.Awareness__c = p.id;
            } 
             if(p.Process_Step__c == 'Credibility')
            {
               
                c.Credibility__c = p.id;
          
            }
             if(p.Process_Step__c == 'Connection')
            {
                
                c.Connection__c = p.id;
          
            }
             if(p.Process_Step__c == 'Convinced')
            {
               
                c.Convinced__c = p.id;
                
          
            } if(p.Process_Step__c == 'Advocating')
            {
               
               c.Advocating__c = p.id;
          
            }
      
            if(p.Country__c != null)
            {
                c.Country__c = p.Country__c;
           
             } 
               if(p.KOL_Title__c != null)
            {
                c.KOL_Title__c = p.KOL_Title__c;
           
             }
   
            if(p.Status__c != null)
            {
                c.Status__c = p.Status__c;
           
             } 
              if(p.Product__c != null)
            {
                c.Product__c = p.Product__c;
           
             }

               if(p.Order__c != null)
            {
                c.Order__c = p.Order__c;
           
             } 
               if(p.Key_Opinion_Leader__c != null)
            {  c.KOL_Name__c = p.Key_Opinion_Leader__c;  } 
              insert c; 
      } 
      }   
}
}
Test Class:
@isTest(SeeAllData=true)
private class TestPopulateNewContact
{
 static testMethod void TestPopulateNewContact()
 {
 Map <String,Schema.RecordTypeInfo> recordTypeMap = Account.sObjectType.getDescribe().getRecordTypeInfosByName();
Account accountObj = new Account();
accountObj.name = 'Test Record Type';
if(recordTypeMap.containsKey('Country')) 
{
 accountObj.RecordTypeId= recordTypeMap.get('Country').getRecordTypeId();
}
insert accountObj;

Product2 p= new Product2(Name='Pali 250WG',Dilution_volume__c='ndklc');
insert p;

Contact c= new Contact( FirstName='John',LastName='Walton',MailingCountry='South Africa',MailingStreet='Free State',
MailingState='Free State');
insert c;        

Product_EntryStatus__c pes =new Product_EntryStatus__c(Product__c=p.Id,Global_Objective__c='AchieveRegistration',
Process_Step__c='Awareness',Country__c=accountObj.id,Order__c=1.00,Key_Opinion_Leader__c=c.Name);           
insert pes;

NewContact__c ns= new NewContact__c(KOL_Title__c='Title',KOL_Name__c =c.Name, Country__c= pes.Country__c, Product__c=pes.Product__c );
insert ns;
Product_EntryStatus__c pes1 =new Product_EntryStatus__c(Product__c=p.Id,Global_Objective__c='AchieveRegistration',
Process_Step__c='Credibility',Country__c=accountObj.id,Key_Opinion_Leader__c=c.Name);           
insert pes1;

NewContact__c ns1= new NewContact__c(KOL_Title__c='Title',KOL_Name__c =c.Name, Country__c= pes.Country__c, Product__c=pes.Product__c,Credibility__c  = pes1.id );
insert ns1;



 }

}