+ Start a Discussion
Roopa S 1Roopa S 1 

written test class but not getting code coverage and getting error plz help

writing test class for the below code

Handler class

public class WorkOredrTriggerHelper {
    public static void helperMethod(list<SVMXC__Service_Order__c> WorkOrders){
        
        Map<Id,List<SVMXC__Service_Order_Line__c>> mapLine = new Map<Id,List<SVMXC__Service_Order_Line__c>>();
        List<SVMXC__Service_Order_Line__c> lineItems = new List<SVMXC__Service_Order_Line__c>();
        List<SVMXC__Service_Order_Line__c> lineItemsToUpdate = new List<SVMXC__Service_Order_Line__c>();
        
        lineItems = [select id, Status__c, SVMXC__Service_Order__c from  SVMXC__Service_Order_Line__c where SVMXC__Service_Order__c in :WorkOrders];
        
        
        for(SVMXC__Service_Order_Line__c asd : lineItems) {
            if(mapLine.containsKey(asd.SVMXC__Service_Order__c)){
                mapLine.get(asd.SVMXC__Service_Order__c).add(asd);
                continue;
            }
            mapLine.put(asd.SVMXC__Service_Order__c,new List<SVMXC__Service_Order_Line__c>{asd});
        }
        
        for(SVMXC__Service_Order__c wo : WorkOrders){
            if(wo.SVMXC__Order_Status__c == 'Closed' || wo.SVMXC__Order_Status__c == 'Completed'){
                if(mapLine.get(wo.Id) != null) {
                   boolean flag = false;
                   for(SVMXC__Service_Order_Line__c wd : mapLine.get(wo.Id)){
                        if(wd.Status__c=='open'){
                            flag = true;
                        }
                    }
                   if(flag == true) {
                       System.debug('222The work order cannot be closed since there are no associated work detail lines or the related work detail lines are open.');
                       wo.addError('The work order cannot be closed since there are no associated work detail lines or the related work detail lines are open.');
                   }
                }
                else {
                    System.debug('No items');
                       wo.addError('No line items');
                }
                    
               
            }
        }

    }
    
    public static void updateRelatedLines(list<SVMXC__Service_Order__c> WorkOrdersNew, Map<Id,SVMXC__Service_Order__c> WorkOrdersOld) {
        
        Map<Id,List<SVMXC__Service_Order_Line__c>> mapLine = new Map<Id,List<SVMXC__Service_Order_Line__c>>();
        List<SVMXC__Service_Order_Line__c> lineItems = new List<SVMXC__Service_Order_Line__c>();
        List<SVMXC__Service_Order_Line__c> lineItemsToUpdate = new List<SVMXC__Service_Order_Line__c>();
        List<SVMXC__Service_Order__c> workOrdersToUpdate = new List<SVMXC__Service_Order__c>();
        String closureStatus = '';
        Integer countLineItems;
        Map<Id,String> closureStatusMap = new Map<Id,String>();
        
        lineItems = [select id, Status__c, SVMXC__Service_Order__c, Work_order_status__c, SVMXC__Start_Date_and_Time__c, SVMXC__End_Date_and_Time__c  from  SVMXC__Service_Order_Line__c where SVMXC__Service_Order__c in :WorkOrdersNew];
                
        for(SVMXC__Service_Order_Line__c asd : lineItems) {
            if(mapLine.containsKey(asd.SVMXC__Service_Order__c)){
                mapLine.get(asd.SVMXC__Service_Order__c).add(asd);
                continue;
            }
            mapLine.put(asd.SVMXC__Service_Order__c,new List<SVMXC__Service_Order_Line__c>{asd});
        }
        
        for(SVMXC__Service_Order__c wo : WorkOrdersNew){
            closureStatus = '';
            countLineItems = 0;
            if(wo.SVMXC__Order_Status__c != WorkOrdersOld.get(wo.Id).SVMXC__Order_Status__c && wo.SVMXC__Order_Status__c == 'Closed'){
                for(SVMXC__Service_Order_Line__c wd : mapLine.get(wo.Id)){
                    countLineItems = countLineItems + 1;
                    SVMXC__Service_Order_Line__c newLineItem = new SVMXC__Service_Order_Line__c();
                    newLineItem.Id = wd.Id;
                    newLineItem.Work_order_status__c = 'Work Order is Closed';
                    lineItemsToUpdate.add(newLineItem);
                    closureStatus += 'Start time of WDL'+ countLineItems + ' : ' + wd.SVMXC__Start_Date_and_Time__c+'\n';
                    closureStatus += 'End Date time of WDL'+ countLineItems + ' : ' + wd.SVMXC__End_Date_and_Time__c+'\n'+'\n';
                }
                closureStatusMap.put(wo.Id, closureStatus);
                
            }
        }
        
        List<SVMXC__Service_Order__c> workOrders = [SELECT Id, closure_status__c from SVMXC__Service_Order__c where Id in :WorkOrdersNew];
        for(SVMXC__Service_Order__c workOrder : workOrders){
            workOrder.closure_status__c = closureStatusMap.get(workorder.Id);
            workOrdersToUpdate.add(workOrder);
        }
        
        if(lineItemsToUpdate != null && lineItemsToUpdate.size() > 0){
            update lineItemsToUpdate;
        }
        
        if(lineItemsToUpdate != null && lineItemsToUpdate.size() > 0){
            update workOrdersToUpdate;
        }
    }    
}

Trigger

trigger woTriggerrr on SVMXC__Service_Order__c (before insert, before update, after update, after insert) {
    if(trigger.isBefore && trigger.isupdate){
        system.debug('Inside new trigger'+ Trigger.new);
        WorkOredrTriggerHelper.helperMethod(trigger.new);
    }
    
    if(trigger.isAfter && trigger.isupdate){
        system.debug('Inside new trigger after update'+ Trigger.new);
        WorkOredrTriggerHelper.updateRelatedLines(trigger.new, trigger.oldMap);
    }
}
Getting error like...
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, The work order cannot be closed since there are no associated work detail lines or the related work detail lines are open.: []    AND

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, The work order cannot be closed since there are no associated work detail lines or the related work detail lines are open.: []



TEST CLASS
@isTest
public class WorkOredrTriggerHelperTest {
    @isTest static void helperMethod(){
        
        SVMXC__Service_Order__c serviceOrderObj = new SVMXC__Service_Order__c();
        serviceOrderObj.closure_status__c= 'test';
        serviceOrderObj.SVMXC__Order_Status__c = 'Closed';
        insert serviceOrderObj;
        
        SVMXC__Service_Order_Line__c lineObj = new SVMXC__Service_Order_Line__c();
        lineObj.Status__c = 'open';
        lineObj.SVMXC__Service_Order__c = serviceOrderObj.ID ; 
        insert lineObj;
        Test.startTest();
        update serviceOrderObj;
        System.assertNotEquals('closed', serviceOrderObj.SVMXC__Order_Status__c);
        Test.stopTest();
        
    }
    
    @isTest static void helperMethodSecond(){
        
        SVMXC__Service_Order__c serviceOrderObj = new SVMXC__Service_Order__c();
        serviceOrderObj.closure_status__c= 'test';
        serviceOrderObj.SVMXC__Order_Status__c = 'Closed';
        insert serviceOrderObj;
        
        
        SVMXC__Service_Order_Line__c lineObj = new SVMXC__Service_Order_Line__c();
        lineObj.Status__c = 'open';
        lineObj.SVMXC__Service_Order__c = serviceOrderObj.ID ; 
        insert lineObj;
        Test.startTest();
        serviceOrderObj.SVMXC__Order_Status__c = 'Closed';
        update serviceOrderObj;
        System.assertNotEquals('closed', serviceOrderObj.SVMXC__Order_Status__c);
        Test.stopTest();
        
        
    }
}
Suraj Tripathi 47Suraj Tripathi 47

Hi Roopa,
As I can see after reviewing your query, In Order to Remove your exception I need to know the Options Available at picklist field of Status__c on SVMXC__Service_Order_Line__c Object.

Thanks and Regards
Suraj Tripathi

Roopa S 1Roopa S 1
@Suraj Tripathi In picklist 3 values are there close open Completed