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
RahulRahul 

Hello friends, can you please help me cover these methods in test class?

public without sharing class MaterialRequestManager {
public void validateOrderProducts(Map<Id, OrderItem> newItems, Map<Id,OrderItem> oldItems){
        List<OrderItem> orderItems = new List<OrderItem>();
        for(OrderItem oi : newItems.values()){
            if((oi.Quantity != oldItems.get(oi.Id).Quantity)
               || (oi.Product2Id != oldItems.get(oi.Id).Product2Id)){
                orderItems.add(oi);
            }
 public void validateOrderProducts(List<OrderItem> newLineItems){
        List<Id> orderIds = new List<Id>();
        Set<Id> productIds = new Set<Id>();
        for(OrderItem oi : newLineItems){
            orderIds.add(oi.OrderId);
            productIds.add(oi.Product2Id);
        }
        
        List<Id> serviceFormIds = new List<Id>();
        Map<Id, Id> orderIdVsServiceFormId = new Map<Id, Id>();
        for(Order od: [Select Id, Service_Form__c from Order Where Id IN: orderIds And Service_Form__c != null]){
            serviceFormIds.add(od.Service_Form__c);
            orderIdVsServiceFormId.put(od.Id, od.Service_Form__c);
        }
        
        
        Map<Id, Map<Id, SR_Products__c>> srFormIdVsSRProductsMap = new Map<Id, Map<Id,SR_Products__c>>();
        for(SR_Products__c srp : [Select Id, Service_Form__c, Product__c, Quantify__c From SR_Products__c Where Service_Form__c IN: serviceFormIds And Product__c IN: productIds]){
            if(srFormIdVsSRProductsMap.containsKey(srp.Service_Form__c)){
                srFormIdVsSRProductsMap.get(srp.Service_Form__c).put(srp.Product__c, srp);
            }else{
                srFormIdVsSRProductsMap.put(srp.Service_Form__c, new Map<Id, SR_Products__c>{srp.Product__c => srp});
            }
        }
        
        Boolean isProductPresent = false;
        if(!orderIdVsServiceFormId.isEmpty()){
            for(OrderItem oi : newLineItems){
                if(orderIdVsServiceFormId.containsKey(oi.OrderId)){
                    Id serviceFormId = orderIdVsServiceFormId.get(oi.OrderId);
                    if(srFormIdVsSRProductsMap.containsKey(serviceFormId)){
                        Map<Id, SR_Products__c> idVsSRProduct = srFormIdVsSRProductsMap.get(serviceFormId);
                        if(idVsSRProduct.containsKey(oi.Product2Id)){
                            isProductPresent = true;
                        }
                        if(idVsSRProduct.containsKey(oi.Product2Id)){
                            Double srQty = idVsSRProduct.get(oi.Product2Id).Quantify__c;
                            if(oi.Quantity > srQty){
                                oi.addError(System.Label.Validate_OrderItem_Quantity_From_SR_Products +' ' + srQty);
                            }
                        }
                    }
                    
                    if(isProductPresent == false){
                        oi.addError(System.Label.Validate_OrderItem_Product_From_SR_Products);
                    }
                }
            }
        }      
    }

        }
        if(orderItems.size() > 0)
            validateOrderProducts(orderItems);
    }
   } 
SwethaSwetha (Salesforce Developers) 
Hi Rahul ,
Since this code provided is huge and requires an understanding of your implementation, it might not be possible to provide exact test class suggestions. However, the below information should help you get started.

The below articles give a good insight into how to begin writing test class and how coverage can be improved

https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines 

Examples:
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm
 https://salesforce.stackexchange.com/questions/87533/writing-test-class-for-wrapper-class
https://salesforce.stackexchange.com/questions/108850/apex-test-method-for-wrapper-classes

If this information helps, please mark the answer as best. Thank you
Suraj Tripathi 47Suraj Tripathi 47

Hi Rahul,

I think your code is not correct so please share the correct code so that I could write a Test class for your code.

Thank You

RahulRahul
Hello suraj sir, Can you please share your email id to share the code as its a long code editor is not accepting it. The code is 68% covered, just need more 7% coverage. will post your solution and mark it as a best answer here.