+ Start a Discussion
Roopa S 1Roopa S 1 

how to write test class for this codee

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);
    }
}
CharuDuttCharuDutt
Hii Roopa
Try Below Test Class
@isTest
public class WorkOredrTriggerHelperTest {
@isTest
    public static void UnitTest(){
        SVMXC__Service_Order__c SVMXC = new SVMXC__Service_Order__c();
        //Fill All Required Fields
        SVMXC.SVMXC__Order_Status__c = 'Closed';
        insert SVMXC;
        
        SVMXC__Service_Order_Line__c SVMXCOL = new SVMXC__Service_Order_Line__c();
        //Fill All Required Fields
        SVMXCOL.SVMXC__Service_Order__c = SVMXC.Id;
        SVMXCOL.Status__c = 'open';
        insert SVMXC;
    }
    
    @isTest
    public static void UnitTest2(){
        SVMXC__Service_Order__c SVMXC = new SVMXC__Service_Order__c();
        //Fill All Required Fields
        insert SVMXC;
        
        SVMXC.SVMXC__Order_Status__c = 'Closed';
        update SVMXC;
        
        SVMXC__Service_Order_Line__c SVMXCOL = new SVMXC__Service_Order_Line__c();
        //Fill All Required Fields
        SVMXCOL.SVMXC__Service_Order__c = SVMXC.Id;
        insert SVMXC;
        
        SVMXCOL.Status__c = 'open';
        update SVMXCOL;
        
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
Suraj Tripathi 47Suraj Tripathi 47
Hi Roopa,
I have written a test class for your apex code. Please refer to my code 

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 = 'closed';
        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 = 'Open';
        insert serviceOrderObj;
        
        
        SVMXC_Service_Order_Line__c lineObj = new SVMXC_Service_Order_Line__c();
        lineObj.Status__c = 'Closed';
        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();
        
        
    }
}



Please mark it as the best answer, if it solves your problem.
Thanks and Regards
Suraj Tripathi
Roopa S 1Roopa S 1
@CharuDutt after running test getting error saying
System.DmlException: Insert failed. First exception on row 0 with id a1L5f000000FLdoEAG; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] and 
System.DmlException: Update failed. First exception on row 0 with id a1L5f000000FLdpEAG; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, No line items: []
Roopa S 1Roopa S 1
@Suraj Tripathi
after run test getting error saying System.DmlException: Insert failed. First exception on row 0; first error: INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST, Status: bad value for restricted picklist field: closed: [Status__c]
and
System.DmlException: Insert failed. First exception on row 0; first error: INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST, Status: bad value for restricted picklist field: Closed: [Status__c]