+ Start a Discussion
santhiya duraisanthiya durai 

Need help on Apex test class

Hi,
I have tried to cover the below Apex class but unable to cover.

Apex class:
public class ItemSetupNewVersionController {
    
    /*Description: 
     * This method is used for cloning item setup record from parent record
     * Creating new case record for new Item setup
     * Creating new itemCaseRelation for new case record
     * Creating new Case Product RelationShips for new case record
     */ 
    
    @AuraEnabled
    public static void itemSetupClone(String recordId){ 
        Boolean isItprocessed = false;  
        Map<String, Integer> parentItemSetupMap = new Map<String, Integer>();         
        Case__c newCase = new Case__c();
        List<CaseICIXProductRelationship__c> newCaseProductList = new List<CaseICIXProductRelationship__c>();   
        List<String> fieldAPINames = new List<String>(Schema.SObjectType.Item_Setup__c.fields.getMap().keySet());
        String query = 'SELECT Case__r.Name, Case__r.Case_Description__c, Case__r.Pre_Listing_Case_Number__c, Case__r.Case_Content__c, Case__r.ELWIS_Case_Number__c, Case__r.EKS_Case_Number__c, Case__r.Category_Group_Item_Family__c, Case__r.Temporary_Unique_Key__c, Case__r.Case_version__c, (SELECT Id, ParentId__c FROM Item_setup__r),'+String.join(fieldAPINames,',')+ ' FROM Item_Setup__c WHERE Id=:recordId';  
        Item_Setup__c parentItemSetup = Database.query(query);
        parentItemSetupMap.put(parentItemSetup.ParentId__c == null?recordId:parentItemSetup.ParentId__c, parentItemSetup.ParentId__c == null?parentItemSetup.Item_setup__r.size():[SELECT (SELECT Id,ParentId__c from Item_setup__r) FROM Item_Setup__c WHERE Id =: parentItemSetup.ParentId__c].Item_setup__r.size());        
        // New Case record insertion
        if(parentItemSetup.Case__c != null){
           newCase = new Case__c(Name = parentItemSetup.ParentId__c == null?parentItemSetup.Case__r.Name+'_v'+(parentItemSetupMap.get(recordId)+1):parentItemSetup.Case__r.Name.split('_v')[0]+'_v'+(parentItemSetupMap.get(parentItemSetup.ParentId__c)+1), Case_Description__c = parentItemSetup.Case__r.Case_Description__c, Pre_Listing_Case_Number__c = parentItemSetup.Case__r.Pre_Listing_Case_Number__c, Case_Content__c = parentItemSetup.Case__r.Case_Content__c, ELWIS_Case_Number__c = parentItemSetup.Case__r.ELWIS_Case_Number__c, EKS_Case_Number__c = parentItemSetup.Case__r.EKS_Case_Number__c, Category_Group_Item_Family__c = parentItemSetup.Case__r.Category_Group_Item_Family__c, Temporary_Unique_Key__c = parentItemSetup.Case__r.Temporary_Unique_Key__c, Case_version__c = parentItemSetup.Case__r.Case_version__c);  
            try{
                insert newCase;
            }catch(exception ex){
                System.debug('ItemSetupNewVersionController:itemSetupClone:case Error: '+ex.getMessage());
                throw new AuraHandledException('There is an Exception while inserting case records: '+ex.getMessage());
            }            
    
        }
  
        // Item Setup clone
        Item_Setup__c newItemSetup = parentItemSetup.clone();
        newItemSetup.Name = parentItemSetup.ParentId__c == null?parentItemSetup.Name+'_v'+(parentItemSetupMap.get(recordId)+1):parentItemSetup.Name.split('_v')[0]+'_v'+(parentItemSetupMap.get(parentItemSetup.ParentId__c)+1);       
        newItemSetup.Id = null;
        newItemSetup.Contract_Log_Created__c = false;
        newItemSetup.Valid_From_Verified__c = false;
        newItemSetup.Approved_by_Buyer__c = false;
        newItemSetup.Approved_by_MD__c = false;
        newItemSetup.Item__c = parentItemSetup.Item__c;
        newItemSetup.Case__c = newCase.Id;  
        if(parentItemSetup.ParentId__c == null){
           newItemSetup.ParentId__c = recordId;  
        }
        if(parentItemSetup.Processed__c){
            //Updating the Pricing Information on ItemSetup Record from SQL Server inorder to Populate the same on to the FORM
            List<SQLServerConnector.updateFieldsWrapperClass> pricingInfo = SQLServerConnector.getPricingInfo(recordId);
                                
            for(SQLServerConnector.updateFieldsWrapperClass wrap: pricingInfo){
                newItemSetup.put(wrap.fieldAPI,wrap.fieldValue);
            } 
            
        }              
            try{
                insert newItemSetup;
            }
            catch(exception ex){
                System.debug('ItemSetupNewVersionController:itemSetupClone:case Error: '+ex.getMessage());
                throw new AuraHandledException('There is an Exception while inserting Item Setup records: '+ex.getMessage());
            }
      
          // New itemCaseRelation insertion  
          ItemCaseRelationship__c itemCaseRelation = new ItemCaseRelationship__c ( Case__c = newCase.Id, Item__c = parentItemSetup.Item__c);
              
            try{
                insert itemCaseRelation; 
            }
            catch(exception ex){
                System.debug('ItemSetupNewVersionController:itemSetupClone:Item Case Relationship Error: '+ex.getMessage());
                throw new AuraHandledException('There is an Exception while inserting Item Case Relationship records: '+ex.getMessage());
                
            }
            
    
        //Insertion of new Case Product RelationShips
     
            CaseICIXProductRelationship__c newCaseProduct;
            for(CaseICIXProductRelationship__c caseProduct: [SELECT Id,case__c,ICIX_Product__c,Units_Per_Case__c,Product_UPC__c,Brand_Name__c,Number_of_Units_in_a_Pack__c,Pack_Size_a__c,Unit_of_Measure_a__c,Package_Type_a__c,Product_Long_Description__c FROM CaseICIXProductRelationship__c WHERE Case__c  =:parentItemSetup.Case__c]){
                newCaseProduct = caseProduct.clone();
                newCaseProduct.case__c = newCase.Id;
                newCaseProduct.Id = null;
                newCaseProductList.add(newCaseProduct);
            } 
            
            if(newCaseProductList.size()>0 && !newCaseProductList.isEmpty()){
                try{
                    insert newCaseProductList; 
                    
                }
                catch(exception ex){
                    System.debug('ItemSetupNewVersionController:itemSetupClone:Case ICIXProduct Relationship Error'+ex.getMessage());
                    throw new AuraHandledException('There is an Exception while inserting Case ICIXProduct Relationship records: '+ex.getMessage());
                }
                
            }  
}}

Need help on:

Can someone help me to cover the below lines
 String query = 'SELECT Case__r.Name, Case__r.Case_Description__c, Case__r.Pre_Listing_Case_Number__c, Case__r.Case_Content__c, Case__r.ELWIS_Case_Number__c, Case__r.EKS_Case_Number__c, Case__r.Category_Group_Item_Family__c, Case__r.Temporary_Unique_Key__c, Case__r.Case_version__c, (SELECT Id, ParentId__c FROM Item_setup__r),'+String.join(fieldAPINames,',')+ ' FROM Item_Setup__c WHERE Id=:recordId';  
        Item_Setup__c parentItemSetup = Database.query(query);
        parentItemSetupMap.put(parentItemSetup.ParentId__c == null?recordId:parentItemSetup.ParentId__c, parentItemSetup.ParentId__c == null?parentItemSetup.Item_setup__r.size():[SELECT (SELECT Id,ParentId__c from Item_setup__r) FROM Item_Setup__c WHERE Id =: parentItemSetup.ParentId__c].Item_setup__r.size());        
        // New Case record insertion
        if(parentItemSetup.Case__c != null){
           newCase = new Case__c(Name = parentItemSetup.ParentId__c == null?parentItemSetup.Case__r.Name+'_v'+(parentItemSetupMap.get(recordId)+1):parentItemSetup.Case__r.Name.split('_v')[0]+'_v'+(parentItemSetupMap.get(parentItemSetup.ParentId__c)+1), Case_Description__c = parentItemSetup.Case__r.Case_Description__c, Pre_Listing_Case_Number__c = parentItemSetup.Case__r.Pre_Listing_Case_Number__c, Case_Content__c = parentItemSetup.Case__r.Case_Content__c, ELWIS_Case_Number__c = parentItemSetup.Case__r.ELWIS_Case_Number__c, EKS_Case_Number__c = parentItemSetup.Case__r.EKS_Case_Number__c, Category_Group_Item_Family__c = parentItemSetup.Case__r.Category_Group_Item_Family__c, Temporary_Unique_Key__c = parentItemSetup.Case__r.Temporary_Unique_Key__c, Case_version__c = parentItemSetup.Case__r.Case_version__c);  
            try{
                insert newCase;
            }catch(exception ex){
                System.debug('ItemSetupNewVersionController:itemSetupClone:case Error: '+ex.getMessage());
                throw new AuraHandledException('There is an Exception while inserting case records: '+ex.getMessage());
            }  

Thanks in Advance.