+ Start a Discussion
Kiru535Kiru535 

Code coverage issue

When ever new record inserted in particular object.... this particular record must be copied to all the accounts which are assigned to particular template. I have written trigger for this and it is working perfectly but code coverage is 55%. I need to cover the code more than 85%. Please help me here how to increase the code coverage.

Actually the code which is in the for loop not covering.... I have highlighed the code in the BOLD

Apex trigger:

trigger PACodeUpdate on Program_Criteria_Details__c (after insert)
{
    Set<Id> parentIdSet = new Set<ID>();
   
    List<Program_Criteria_Details__c> pcdList = new List<Program_Criteria_Details__c>();
    List<Program_Requirement_detail__c> insertlist = new List<Program_Requirement_detail__c>();
    Map<String,Program_Criteria_Details__c> pcdMap = new Map<String,Program_Criteria_Details__c>();       
    for(Program_Criteria_Details__c pnew : Trigger.new)
    {
        if(Trigger.isInsert)
        {
            parentIdSet.add(pnew.ProgramTemplates__c);
        }
        pcdMap.put(pnew.ProgramTemplates__c,pnew);  
    }
   /* if(parentIdSet.size() > 0)
    {
      pcdList = [select Id,criteria__c,Metric_Status__c,ProgramTemplates__c,Target__c,Product__c from Program_Criteria_Details__c where ProgramTemplates__c in :parentIdSet];
    }*/
   
      List<Program_Level_Requirements__c> plr = [select id,Name,Account__c,Community__c,ProgramTemplates__c,Technology__c from Program_Level_Requirements__c where ProgramTemplates__c in :parentIdSet];
      for(Program_Level_Requirements__c plrlist:plr)
      {
        Program_Requirement_detail__c prd = new Program_Requirement_detail__c();
        prd.Account__c = plrlist.Account__c;
        prd.Program_Level_Requirement__c = plrlist.id;
        //prd.Community__c = plrlist.Community__c;
        //prd.Technology__c = plrlist.Technology__c;
        prd.Program_Criteria_Details__c = pcdMap.get(plrlist.ProgramTemplates__c).id;
        prd.Criteria__c = pcdMap.get(plrlist.ProgramTemplates__c).Criteria__c;
        prd.Product__c = pcdMap.get(plrlist.ProgramTemplates__c).Product__c;
        prd.Target__c = pcdMap.get(plrlist.ProgramTemplates__c).Target__c;
        prd.Measure__c = pcdMap.get(plrlist.ProgramTemplates__c).Measure__c;
        insertlist.add(prd);
      }
      insert insertlist;
}



Test Class:

@isTest(seealldata=true)
private class PACodeUpdateTest
{
   static testMethod void PATest()  
   {
     Test.startTest();
     RecordType RT = [Select Id from RecordType where Name='Partner' and SobjectType='Account' limit 1];
     Account ac1 = new Account(name='Test Account1',RecordTypeId=RT.id,Metal_Level__c='Gold',Partner_HQ_Country__c='United States',AccountNumber='11223344');
     Account ac2 = new Account(name='Test Account2',RecordTypeId=RT.id,Metal_Level__c='Gold',Partner_HQ_Country__c='United States',AccountNumber='11223345');
     list<Account> acc=new list<Account>();
     acc.add(ac1);
     acc.add(ac2);
     insert acc;
     Partner_Type__c pt1 = new Partner_Type__c(Account__c = ac1.id, Program__c = 'Mobility',Status__c='Active',Support_Services__c=True,Advanced_Services__c=True,US_Federal_Specialization__c='Authorized',Partner_Track__c='DMR');
     Partner_Type__c pt2 = new Partner_Type__c(Account__c = ac2.id, Program__c = 'Mobility',Status__c='Active',Support_Services__c=True,Advanced_Services__c=True,US_Federal_Specialization__c='Authorized',Partner_Track__c='DMR');
     list<Partner_Type__c> pt=new list<Partner_Type__c>();
     pt.add(pt1);
     pt.add(pt2);
     insert pt;
   
     Technology_Specialization__c ts1 = new Technology_Specialization__c(Partner_Program__c=pt1.id,Account__c=ac1.id,Technology_specialization__c='Mobile Computing',Technology_specialization_level__c='Authorized',Technology_Specialization_Status__c='Active',Community_Type__c='Value Added Reseller');
      Technology_Specialization__c ts2 = new Technology_Specialization__c(Partner_Program__c=pt2.id,Account__c=ac2.id,Technology_specialization__c='Mobile Computing',Technology_specialization_level__c='Authorized',Technology_Specialization_Status__c='Active',Community_Type__c='Value Added Reseller');
     list<Technology_Specialization__c> ts=new list<Technology_Specialization__c>();
     ts.add(ts1);
     ts.add(ts2);
     insert ts;
    
     ProgramTemplates__c ptp = new ProgramTemplates__c(Program__c ='Mobility',Technology__c ='Mobile Computing',Region__c ='NAMR',Status__c ='Active',Community__c='Value Added Reseller');
     List<ProgramTemplates__c> ptlst = new List<ProgramTemplates__c>();
     ptlst.add(ptp);
     insert ptlst;
     Program_Criteria_Details__c pcd = new Program_Criteria_Details__c(Criteria__c ='TestPA1',Target__c =2,Measure__c='Annually',ProgramTemplates__c =ptp.id,Product__c ='WT4090');
     List<Program_Criteria_Details__c> pcdlst = new List<Program_Criteria_Details__c>();
     pcdlst.add(pcd);
     insert pcdlst;
     Id recordtyid= Schema.SObjectType.Program_Level_Requirements__c.getRecordTypeInfosByName().get('PLR_Product Access').getRecordTypeId();
     Program_Level_Requirements__c pr1=new Program_Level_Requirements__c(Account__c=ac1.id,Technology__c=ts1.Technology_Specialization__c,Community__c=ts1.Community_Type__c, Recordtypeid=recordtyid,ProgramTemplates__c =ptp.id);
     Program_Level_Requirements__c pr2=new Program_Level_Requirements__c(Account__c=ac2.id,Technology__c=ts2.Technology_Specialization__c,Community__c=ts2.Community_Type__c, Recordtypeid=recordtyid,ProgramTemplates__c =ptp.id);
     List<Program_Level_Requirements__c> pr1lst = new List<Program_Level_Requirements__c>();
     pr1lst.add(pr1);
     pr1lst.add(pr2);
     insert pr1lst;
     /*Program_Requirement_detail__c prdetail=new Program_Requirement_detail__c(Account__c=ac1.id,Program_Level_Requirement__c=pr1.id,Program_Criteria_Details__c =pcd.id,Criteria__c = pcd.Criteria__c,Product__c =pcd.Product__c,Target__c =pcd.Target__c,Measure__c =pcd.Measure__c);
     Program_Requirement_detail__c prdetail1=new Program_Requirement_detail__c(Account__c=ac2.id,Program_Level_Requirement__c=pr2.id,Program_Criteria_Details__c =pcd.id,Criteria__c = pcd.Criteria__c,Product__c =pcd.Product__c,Target__c =pcd.Target__c,Measure__c =pcd.Measure__c);
     List<Program_Requirement_detail__c> prdetaillst = new List<Program_Requirement_detail__c>();
     prdetaillst.add(prdetail);
     prdetaillst.add(prdetail1);
     insert prdetaillst;*/
    
     List<Program_Level_Requirements__c> plrfinal= [select id,Name,Account__c,Community__c,ProgramTemplates__c,Technology__c from Program_Level_Requirements__c where ProgramTemplates__c =:ptp.id];
     List<Program_Requirement_detail__c> prdfinal = new List<Program_Requirement_detail__c>();
     for(Program_Level_Requirements__c plrrec:plrfinal)
     {
        Program_Requirement_detail__c prdinsert = new Program_Requirement_detail__c();
        prdinsert.Account__c = plrrec.Account__c;
        prdinsert.Program_Level_Requirement__c = plrrec.id;
        prdinsert.Program_Criteria_Details__c = pcd.id;
        prdinsert.Criteria__c = pcd.Criteria__c;
        prdinsert.Product__c = pcd.Product__c;
        prdinsert.Target__c = pcd.Target__c;
        prdinsert.Measure__c = pcd.Measure__c;
        prdfinal.add(prdinsert);
     }
     insert prdfinal;
     Test.stopTest();
   }
}
Best Answer chosen by Kiru535
Arunkumar RArunkumar R
Hi Kiru,
Can u try the below code,

@isTest(seealldata=true)
private class PACodeUpdateTest
{
   static testMethod void PATest()  
   {
		Test.startTest();
		ProgramTemplates__c ptp = new ProgramTemplates__c(Program__c ='Mobility',Technology__c ='Mobile Computing',Region__c ='NAMR',Status__c ='Active',Community__c='Value Added Reseller');
		insert ptp;

		Program_Criteria_Details__c pcd = new Program_Criteria_Details__c(Criteria__c ='TestPA1',Target__c =2,Measure__c='Annually',ProgramTemplates__c =ptp.id,Product__c ='WT4090');
		insert pcd;

		Program_Requirement_detail__c prdetail=new Program_Requirement_detail__c(Account__c=ac1.id,Program_Level_Requirement__c=pr1.id,Program_Criteria_Details__c =pcd.id,Criteria__c = pcd.Criteria__c,Product__c =pcd.Product__c,Target__c =pcd.Target__c,Measure__c =pcd.Measure__c);
		insert prdetail;

		Id recordtyid= Schema.SObjectType.Program_Level_Requirements__c.getRecordTypeInfosByName().get('PLR_Product Access').getRecordTypeId();

		RecordType RT = [Select Id from RecordType where Name='Partner' and SobjectType='Account' limit 1];

		Account ac1 = new Account(name='Test Account1',RecordTypeId=RT.id,Metal_Level__c='Gold',Partner_HQ_Country__c='United States',AccountNumber='11223344');
		insert ac1;

		Partner_Type__c pt1 = new Partner_Type__c(Account__c = ac1.id, Program__c = 'Mobility',Status__c='Active',Support_Services__c=True,Advanced_Services__c=True,US_Federal_Specialization__c='Authorized',Partner_Track__c='DMR');
		insert pt1;

		Technology_Specialization__c ts1 = new Technology_Specialization__c(Partner_Program__c=pt1.id,Account__c=ac1.id,Technology_specialization__c='Mobile Computing',Technology_specialization_level__c='Authorized',Technology_Specialization_Status__c='Active',Community_Type__c='Value Added Reseller');
		insert ts1;

		Program_Level_Requirements__c pr1=new Program_Level_Requirements__c(Account__c=ac1.id,Technology__c=ts1.Technology_Specialization__c,Community__c=ts1.Community_Type__c, Recordtypeid=recordtyid,ProgramTemplates__c =ptp.id);
		insert pr1;
		Test.stopTest();
     
   }
}

The above test class is not bulkified records so insert multiple records and put assertion for validating desired result.

All Answers

Kiru535Kiru535
Need urgent help on this issue..................
Arunkumar RArunkumar R
Hi Kiru,
Can u try the below code,

@isTest(seealldata=true)
private class PACodeUpdateTest
{
   static testMethod void PATest()  
   {
		Test.startTest();
		ProgramTemplates__c ptp = new ProgramTemplates__c(Program__c ='Mobility',Technology__c ='Mobile Computing',Region__c ='NAMR',Status__c ='Active',Community__c='Value Added Reseller');
		insert ptp;

		Program_Criteria_Details__c pcd = new Program_Criteria_Details__c(Criteria__c ='TestPA1',Target__c =2,Measure__c='Annually',ProgramTemplates__c =ptp.id,Product__c ='WT4090');
		insert pcd;

		Program_Requirement_detail__c prdetail=new Program_Requirement_detail__c(Account__c=ac1.id,Program_Level_Requirement__c=pr1.id,Program_Criteria_Details__c =pcd.id,Criteria__c = pcd.Criteria__c,Product__c =pcd.Product__c,Target__c =pcd.Target__c,Measure__c =pcd.Measure__c);
		insert prdetail;

		Id recordtyid= Schema.SObjectType.Program_Level_Requirements__c.getRecordTypeInfosByName().get('PLR_Product Access').getRecordTypeId();

		RecordType RT = [Select Id from RecordType where Name='Partner' and SobjectType='Account' limit 1];

		Account ac1 = new Account(name='Test Account1',RecordTypeId=RT.id,Metal_Level__c='Gold',Partner_HQ_Country__c='United States',AccountNumber='11223344');
		insert ac1;

		Partner_Type__c pt1 = new Partner_Type__c(Account__c = ac1.id, Program__c = 'Mobility',Status__c='Active',Support_Services__c=True,Advanced_Services__c=True,US_Federal_Specialization__c='Authorized',Partner_Track__c='DMR');
		insert pt1;

		Technology_Specialization__c ts1 = new Technology_Specialization__c(Partner_Program__c=pt1.id,Account__c=ac1.id,Technology_specialization__c='Mobile Computing',Technology_specialization_level__c='Authorized',Technology_Specialization_Status__c='Active',Community_Type__c='Value Added Reseller');
		insert ts1;

		Program_Level_Requirements__c pr1=new Program_Level_Requirements__c(Account__c=ac1.id,Technology__c=ts1.Technology_Specialization__c,Community__c=ts1.Community_Type__c, Recordtypeid=recordtyid,ProgramTemplates__c =ptp.id);
		insert pr1;
		Test.stopTest();
     
   }
}

The above test class is not bulkified records so insert multiple records and put assertion for validating desired result.

This was selected as the best answer
Vinit_KumarVinit_Kumar
The issue here is your query is returning 0 rows in test class :-

Make sure Program_Level_Requirements__c is related with Program_Criteria_Details__c based on this field ProgramTemplates__c in your test class.
Kiru535Kiru535
Hi Arun,

Thanks alot. your code is works fine...