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
Sarthak Bajaj 14Sarthak Bajaj 14 

Unable to cover test class coverage for the below before update trigger on custom object.

Hi All,

I have a trigger which does not allow changing of parent record status to implemented till the time child record status is implemented.
Below is the trigger for the same:
trigger RequirementStatusUpdate on Requirement__c (before update) {
	
    Set<Id> setRequirement = new Set<Id>();

    for(Requirement__c rm : trigger.new)
        if(rm.Status__c == 'Implemented')
            setRequirement.add(rm.Id);

    Map<Id, List<System_Action__c>> mapCaseToBuildCard
        = new Map<Id, List<System_Action__c>>();

    for(
        System_Action__c iCard :
        [
            SELECT
                Id, Requirement__c
            FROM
                System_Action__c
            WHERE 
                Requirement__c IN :setRequirement AND
                Status__c != 'Done/Tested'
        ]
    ){
        if(
            !mapCaseToBuildCard.containsKey(
                iCard.Requirement__c
            )
        ){
            mapCaseToBuildCard.put(
                iCard.Requirement__c,
                new List<System_Action__c>{
                    iCard
                }
            );
        }
        else{
            mapCaseToBuildCard.get(
                iCard.Requirement__c
            ).add(iCard);
        }
    }

    for(Requirement__c rm : trigger.new){
        if(rm.Status__c == 'Implemented'){
            if(
                mapCaseToBuildCard.containsKey(rm.Id) &&
                mapCaseToBuildCard.get(
                    rm.Id
                ).size() > 0
            ){
                rm.addError(
                    'You cannot Change the Status to implemented this Requirement. ' + 
                    'There are Open Build Card' + 
                    ' under this Requirement.'
                );
            }
        }
    }
}
Requirement: Parent Object
System Action: Child Object

In test class, so far I have created a dummy Requirement object data and the System Action object data.
but unable to proceed forward with the coverage.
Could you please suggest how exactly I need to cover the code coverage for data presnt in set and checking the if logic criteria.

So far I have written:
@isTest
public class RequirementStatusUpdateTest {
	static testMethod void RequirementStatusUpdateTest(){
    	Recordtype rname = [Select id, name from recordtype where name = 'SFDC Requirement'];
        List<Requirement__c> Req= new List<Requirement__c>();
        //Set up user
        User u1 = [SELECT Id,Name FROM User WHERE GE_ES_SSO__c='502690635'];
      //  Initiative_Project__c VerticalTeam = [select id from Initiative_Project__c where id = 'a0GC000000Liwkw' limit 1];
        
        //Run As U1
        System.runAs(u1){
            test.startTest();
            
            try{
//parent record
            Requirement__c rObj = new Requirement__c(
                RecordTypeid = rname.id,
                Requirement_Type1__c = 'Production Change Request',
                SFDC_Project__c = 'a0GC000000Liwkw',
                Development_Ownership__c= 'P&W > HQ',
                User_Story_Name__c = 'Test class coverage class',
                Tier_2_P_L__c = 'HQ',
                Target_Release__c= 'TBD',
                Prioritization_P_L__c= 'P&W Power Services',
                SFDC_P_L_Owner__c=u1.Id,
                User_Story__c='Test',
                Release__c='TBD1',
                Status__c = 'Draft');
            insert rObj;
            
            

//child record
            System_Action__c BCObj = new System_Action__c();
            BCObj.Requirement__c = rObj.id;
            BCObj.System_Action_Name__c = 'Test coverage';
            BCObj.Status__c = 'Draft';
            insert BCObj;
             
                
                
              
				Set<Requirement__c> setRequirement = new Set<Requirement__c>();
				setRequirement.add(rObj);
                
                
                rObj.Status__c = 'Implemented';
                Update rObj;
            
        
    }
            catch(Exception e){
         
              
}
}
}

Thanks in advance.

Sarthak
Maharajan CMaharajan C
Hi Sarthak,

You have to just check the exception:

@isTest
public class RequirementStatusUpdateTest {
    static testMethod void RequirementStatusUpdateTest(){
        Recordtype rname = [Select id, name from recordtype where name = 'SFDC Requirement'];
        List<Requirement__c> Req= new List<Requirement__c>();
        //Set up user
        User u1 = [SELECT Id,Name FROM User WHERE GE_ES_SSO__c='502690635'];
      //  Initiative_Project__c VerticalTeam = [select id from Initiative_Project__c where id = 'a0GC000000Liwkw' limit 1];
        
        //Run As U1
        System.runAs(u1){
            
            
            try{
//parent record
            Requirement__c rObj = new Requirement__c(
                RecordTypeid = rname.id,
                Requirement_Type1__c = 'Production Change Request',
                SFDC_Project__c = 'a0GC000000Liwkw',
                Development_Ownership__c= 'P&W > HQ',
                User_Story_Name__c = 'Test class coverage class',
                Tier_2_P_L__c = 'HQ',
                Target_Release__c= 'TBD',
                Prioritization_P_L__c= 'P&W Power Services',
                SFDC_P_L_Owner__c=u1.Id,
                User_Story__c='Test',
                Release__c='TBD1',
                Status__c = 'Draft');
            insert rObj;
            
            

//child record
            System_Action__c BCObj = new System_Action__c();
            BCObj.Requirement__c = rObj.id;
            BCObj.System_Action_Name__c = 'Test coverage';
            BCObj.Status__c = 'Draft';
            insert BCObj;
             
            
                
                test.startTest();
                rObj.Status__c = 'Implemented';
                Update rObj;
                test.stopTest();
        
    }
            catch(Exception e)
            
            {
            Boolean expectedExceptionThrown =  e.getMessage().contains('You cannot Change the Status to implemented this Requirement.') ? true : false;
            System.AssertEquals(true, expectedExceptionThrown);

              
}
}
}
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
​Raj
Sarthak Bajaj 14Sarthak Bajaj 14
Thanks for writing back Raj.
I was able to successfully save the test class without any error, but the problem is test class coverage is still showing zero percent.
Maharajan CMaharajan C
Hi Sarthak,

Check the debug log because you are using the try catch block did you have any other exception is caused in code and also checks the trigger is Active.

You have to insert All the related records properly.

use the debug log level as finest.

thanks,
Raj