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
Proposal ButtonProposal Button 

Test class

Hi All

 

Can you please help me to write a test calss for the following code.

 

private void createOrderASOCs(Id orderId, Id accountId, QuoteLine__c[] orderASOCs){
        System.debug('In createOrderASOCs() Function');
        //******************** ASOCs ********************
        List<ASOCs__c> insertASOC = new List<ASOCs__c>();
        List<Maintenance_Plan__c> insertMaint =new List<Maintenance_Plan__c>();
        
        for(QuoteLine__c asoc:orderASOCs){
            System.debug('Looping Lines (ASOCS)');
            
            String tnId;
            Double currQTY=asoc.Quantity__c;
            
            if(asoc.Quote_Telephone_Number_Config__r!=null&&asoc.Quote_Telephone_Number_Config__r.size()>0){
                System.debug('Adding TN Level ASOCS');
                for(QuoteTelephoneNumberConfig__c orderTNs:asoc.Quote_Telephone_Number_Config__r){
                    tnId = tnMap.get(orderTNs.QuoteTelephoneNumber__r.Name);
                    System.debug('    Current TN ID:'+tnId);
                    
                    insertASOC.addAll(buildTNAsocs(tnId, orderId, asoc,1));
                    currQTY -=1;
                }
            }
            
            //********* Working with the Main BTN *********
            if(currQTY>0){
                System.debug('Adding BTN Level ASOCS');
                tnId = tnMap.get(mainBTN);
                
                System.debug('    Current BTN Id:'+ tnId);
                insertASOC.addAll(buildTNAsocs(tnId, orderId, asoc,currQTY));
            }
                        
            //Maintenance Plan
            if(asoc.MasterProduct__r!=null&&asoc.MasterProduct__r.QuantityType__c=='Maintenance Plan'){
                System.debug('Creating Maintenance Plan');
                Maintenance_Plan__c maintPlan = new Maintenance_Plan__c();
                
                maintPlan.Account__c = accountId;
                maintPlan.Order__c = orderId;
                
                maintPlan.Maintenance_Plan_Type__c = asoc.MasterProduct__r.Name;
                maintPlan.Monthly_Maintenance_Rate__c = asoc.Price__c;
                maintPlan.Contract_Term__c = asoc.Term__c;
                maintPlan.Contract_Start_Date__c = asoc.Quote__r.Contract_Start_Date__c;
                
                insertMaint.add(maintPlan);
            }
        }
        
        //PIC/IPIC
        if(otherASOCS!=null&&otherASOCS.size()>0){
            System.debug('Adding PIC and IPIC');
            
            for(ASOCs__c tmpASOC:otherASOCS){
                tmpASOC.Order__c=orderId;
                tmpASOC.Telephone_Number__c=tnMap.get(mainBTN);
                
                insertASOC.add(tmpASOC);
            }
        }
        
        if(insertASOC!=null&&insertASOC.size()>0){
            System.debug('Inserting ASOCS');
            insert insertASOC;
        }
        
        //insert Maint Plan objects.
        if(insertMaint!=null&&insertMaint.size()>0){
            System.debug('Inserting Maint');
            insert insertMaint;
        }
    }
    
    private ASOCs__c[] buildTNAsocs(Id tnId, Id orderId, QuoteLine__c asoc, Double qty){
        List<ASOCs__c> tnASOCs = new List<ASOCs__c>();
        ASOCs__c orderASOC = new ASOCs__c();
        
        orderASOC.Telephone_Number__c=tnId;
            orderASOC.Order__c=orderId;
            if(asoc.Product__r!=null){
                orderASOC.Asoc__c=asoc.Product__r.Name;
                System.debug('    ASOC Name:'+ asoc.Product__r.Name);
                orderASOC.ASOC_Description__c=asoc.Product__r.Description;
            }else{
                System.debug('    ASOC has Null Product');
                orderASOC.Asoc__c=asoc.CustomAsoc__c;
                System.debug('    ASOC Name:'+ asoc.CustomAsoc__c);
                orderASOC.ASOC_Description__c=asoc.CustomDescription__c;
            }
            orderASOC.Rate__c=asoc.Price__c;
            orderASOC.Quantity__c=qty;
            orderASOC.Magnys_Service_Code_Desc__c=asoc.Magnys_Service_Code_Desc__c;
            
            tnASOCs.add(orderASOC);
            
        //Check for BOM Items
        System.debug('ASOC BOM Items');
        
        if(asoc.Quote_Line_BOM_Items__r!=null&&asoc.Quote_Line_BOM_Items__r.size()>0){
            QuoteLineBomItem__c[] tmpBMs = asoc.Quote_Line_BOM_Items__r;
            
            for(QuoteLineBomItem__c bom:tmpBMs){
                System.debug('Looping BOM Items');
                ASOCs__c bomASOC = new ASOCs__c();
                
                bomASOC.Telephone_Number__c=tnId;
                bomASOC.Order__c=orderId;
                if(bom.Product__r!=null){
                    bomASOC.Asoc__c=bom.Product__r.Name;
                    bomASOC.ASOC_Description__c=bom.Product__r.Description;
                }else{
                    System.debug('ASOC has Null Product');
                }
                bomASOC.Rate__c=0.0;
                bomASOC.Quantity__c=bom.Quantity__c;
                bomASOC.Magnys_Service_Code_Desc__c=asoc.Magnys_Service_Code_Desc__c;
                
                tnASOCs.add(bomASOC);
            }
        }
        
        return tnASOCs;
    }
    

MJ09MJ09

I can't write the test class for you, but I do have a few comments:

 

 

  1. You've defined the methods as private, so you'll need to either put the unit tests in the same class as the methods, or re-define the methods as public so you can call them from a separate unit test class.
  2. Using a separate unit test class is a best practice for a few reasons, one of which is that code defined in a unit test class doesn't count against your org's code limits. 
  3. As a general approach, when you write your unit test, you should insert any records that you want to test against, then call the methods you're testing, then use System.assert* statements to verify the results.