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
Krishna Sahu 1Krishna Sahu 1 

Please Help to write the test class on Before Insert

public static void addRepForCredit(List<order> newOrderList){
        Set<Id> patientIdSet = new Set<Id>();
        for(order orderRec: newOrderList){
            if(orderRec.Type == Constants.PERMANENT_IMPLANT )
                patientIdSet.add(orderRec.patient_account__c);
        }
        List<order> trialOrderList = new List<order>();
        Map<Id, order> trialOrderMap = new Map<Id, order>();
        if(!patientIdSet.isEmpty()){        
            trialOrderList = [
                SELECT Id, patient_account__c, Type, rep_for_credit__c, Status, EffectiveDate
                FROM order
                WHERE patient_account__c IN :patientIdSet
                AND Type =:Constants.TRIAL_IMPLANT
                AND Status !=:Constants.PICKLIST_VALUE_STATUS_CANCELLED
            ];
            if(!trialOrderList.isEmpty()){
                for(order orderRec: trialOrderList){
                    if(!trialOrderMap.containsKey(orderRec.patient_account__c)){
                        trialOrderMap.put(orderRec.patient_account__c, orderRec);
                    }
                    else if(
                        orderRec.EffectiveDate 
                        > 
                        trialOrderMap.get(orderRec.patient_account__c).EffectiveDate
                    ){
                        trialOrderMap.put(orderRec.patient_account__c, orderRec);
                    }
                }
                for(order orderRec: newOrderList){
                    if(trialOrderMap.containsKey(orderRec.patient_account__c) && orderRec.rep_for_credit__c==null){
                        orderRec.rep_for_credit__c = 
                            trialOrderMap.get(orderRec.patient_account__c).rep_for_credit__c;
                    }
                    else if(
                        trialOrderMap.containsKey(orderRec.patient_account__c) 
                        && 
                        orderRec.rep_for_credit__c != trialOrderMap.get(orderRec.patient_account__c).rep_for_credit__c
                    ){
                        orderRec.addError(Constants.ORDER_RFC_TRIAL_PERMANENT_ERROR_MESSAGE);
                    }
                }
            }
        }   
    }
PriyaPriya (Salesforce Developers) 

Hey Krishna,

Have you made any attempt for the test class? Kindly provide, if any.

It would be difficult for the community to provide the complete implementation of test class. Hope you understand this. However, we can help you with error if you face any in your test code.

Thanks & Regards,

Priya Ranjan