+ Start a Discussion
santhiya duraisanthiya durai 

unable to cover code

One of my customer created below class. I have created a test class to cover but I am unable to cover the code. It seems I have created a correct data but it is not covering .Please anyone help me on this.

Class:

public class MassQEController{
    
    @Auraenabled
    public static List<string> getAccounts(){
        List<ICIX_V1__Trading_Partner_Relationship__c> tprlist=[select id,ICIX_V1__Responder__r.Facility_Name__c,ICIX_V1__Responder__r.name from ICIX_V1__Trading_Partner_Relationship__c where ICIX_V1__Status__c='Active' and ICIX_V1__Type__c='Vendor' order by ICIX_V1__Responder__r.facility_name__c asc];
        List<String> str=new List<String>();
        for(ICIX_V1__Trading_Partner_Relationship__c tpr:tprlist ){
            if(tpr.ICIX_V1__Responder__r.Facility_Name__c!=null){
                str.add(tpr.ICIX_V1__Responder__r.Facility_Name__c);
            }
        }
        return str;
    }
@Auraenabled
    public static string getprods(string TradingPartnerName,string  parentnumber,string Globalsku,String Productnumber){
        List<ICIX_V1__UP_Relationship__c> uprlsts=new List<ICIX_V1__UP_Relationship__c>();
        set<String> strn=new set<String>();
        strn.add('ASSORTMENT');
        strn.add('SOLID');
        set<Id> pid=new set<Id>();
        if(parentnumber==''){
            parentnumber='XYZ';
        }
        if(Globalsku==''){
            Globalsku='XYZ';
        }
        if(Productnumber==''){
            Productnumber='XYZ';
        }
        if(TradingPartnerName!='No Trading Partner' && TradingPartnerName!= 'All Trading Partners'){
            uprlsts=[select id,ICIX_V1__Product__c,ICIX_V1__Product__r.GlobalSKU__c,ICIX_V1__Product__r.PRODUCTTYPE__c,ICIX_V1__Product__r.Parent_Number__c,ICIX_V1__Product__r.Name,ICIX_V1__Product__r.Product_Number__c,ICIX_V1__Related_Account__r.name,ICIX_V1__Related_Account__r.facility_name__c from ICIX_V1__UP_Relationship__c where ICIX_V1__Related_Account__r.Facility_Name__c=:TradingPartnerName and (ICIX_V1__Product__r.Parent_Number__c like: '%'+parentnumber+'%' or ICIX_V1__Product__r.GlobalSKU__c like: '%'+Globalsku+'%' or ICIX_V1__Product__r.Product_Number__c like: '%'+Productnumber+'%' ) and ICIX_V1__Product__r.PRODUCTTYPE__c in :strn];
        }
        else if(TradingPartnerName=='No Trading Partner'){
            uprlsts=[select id,ICIX_V1__Product__c,ICIX_V1__Product__r.GlobalSKU__c,ICIX_V1__Product__r.PRODUCTTYPE__c,ICIX_V1__Product__r.Parent_Number__c,ICIX_V1__Product__r.Name,ICIX_V1__Product__r.Product_Number__c,ICIX_V1__Related_Account__r.name,ICIX_V1__Related_Account__r.facility_name__c from ICIX_V1__UP_Relationship__c where ICIX_V1__Related_Account__c=null and (ICIX_V1__Product__r.Parent_Number__c like: '%'+parentnumber+'%' or ICIX_V1__Product__r.GlobalSKU__c like: '%'+Globalsku+'%' or ICIX_V1__Product__r.Product_Number__c like: '%'+Productnumber+'%' )  and ICIX_V1__Product__r.PRODUCTTYPE__c in :strn];
        }
        else if(TradingPartnerName == 'All Trading Partners'){
          uprlsts=[select id,ICIX_V1__Product__c,ICIX_V1__Product__r.GlobalSKU__c,ICIX_V1__Product__r.PRODUCTTYPE__c,ICIX_V1__Product__r.Parent_Number__c,ICIX_V1__Product__r.Name,ICIX_V1__Product__r.Product_Number__c,ICIX_V1__Related_Account__r.name,ICIX_V1__Related_Account__r.facility_name__c from ICIX_V1__UP_Relationship__c where ICIX_V1__Related_Account__c !=null AND (ICIX_V1__Product__r.Parent_Number__c like: '%'+parentnumber+'%' or ICIX_V1__Product__r.GlobalSKU__c like: '%'+Globalsku+'%' or ICIX_V1__Product__r.Product_Number__c like: '%'+Productnumber+'%' ) and ICIX_V1__Product__r.PRODUCTTYPE__c in :strn];
     
        }
        set<id> upids=new set<id>();
        for(ICIX_V1__UP_Relationship__c upr:uprlsts){
            upids.add(upr.id);
        }
        map<id,ICIX_V1__UP_Relationship_Attribute__c> map_upr=new map<id,ICIX_V1__UP_Relationship_Attribute__c>();
        List<ICIX_V1__UP_Relationship_Attribute__c> uprAttr=[select id,ICIX_V1__UP_Relationship__c,ICIX_V1__Attribute_Value__c from ICIX_V1__UP_Relationship_Attribute__c where ICIX_V1__UP_Relationship__c in:upids and ICIX_V1__Attribute__r.ICIX_V1__Type__c='Tag' ];
        for(ICIX_V1__UP_Relationship_Attribute__c up:uprAttr){
            map_upr.put(up.ICIX_V1__UP_Relationship__c,up);
        }
        List<prodwrap> prodwraplist=new List<prodwrap>();
        for(ICIX_V1__UP_Relationship__c upr: uprlsts ){
            prodwrap prwrp=new prodwrap();
            prwrp.Productname=upr.ICIX_V1__Product__r.Name;
            prwrp.UprId=upr.Id;
            prwrp.Producttype=upr.ICIX_V1__Product__r.PRODUCTTYPE__c;
            prwrp.SFRecid='/'+upr.ICIX_V1__Product__c;
            prwrp.ProductNumber=upr.ICIX_V1__Product__r.Product_Number__c;
            prwrp.Facility=UPR.ICIX_V1__Related_Account__r.Facility_name__c;
            prwrp.Parentnumber=upr.ICIX_V1__Product__r.Parent_Number__c;
            prwrp.Globalsku=upr.ICIX_V1__Product__r.GlobalSKU__c;
            if(map_upr.containskey(upr.id)){
                if(map_upr.get(upr.id).ICIX_V1__Attribute_Value__c!=null){
                    prwrp.Responsibleqe=map_upr.get(upr.id).ICIX_V1__Attribute_Value__c;
                }
            }
            prodwraplist.add(prwrp);
        }
        string ks=JSON.serialize(prodwraplist,true);
        string ns=ks.replace('children', '_children');
        system.debug(ns);
        return ns;
    }
    @Auraenabled
    public static string UpdateQEs(List<Id> UprIds , String ResponsibleQE ,String FacilityName ){
        System.debug('=========UprIds=========='+UprIds);
        System.debug('=========ResponsibleQE=========='+ResponsibleQE);
        System.debug('=========FacilityName=========='+FacilityName);
        list<ICIX_V1__UP_Relationship_Attribute__c> Upattr=[select id,ICIX_V1__Attribute__c from ICIX_V1__UP_Relationship_Attribute__c where ICIX_V1__UP_Relationship__c in:UprIds and ICIX_V1__Attribute__r.ICIX_V1__Type__c='Tag'];
        delete  Upattr;
        list<ICIX_V1__Attribute__c> attr=[select id,name from ICIX_V1__Attribute__c where name=:ResponsibleQE and ICIX_V1__type__c='Tag' limit 1];
        list<ICIX_V1__UP_Relationship_Attribute__c> Uprattriinsert=new list<ICIX_V1__UP_Relationship_Attribute__c>();
        list<ICIX_V1__UP_Relationship_Attribute__c> UprattriinsertAsst=new list<ICIX_V1__UP_Relationship_Attribute__c>();
        Id AttrId=attr[0].Id;
        system.debug('attribute'+attr);
        for(Id ids:UprIds){
            for(ICIX_V1__Attribute__c c:attr){
                ICIX_V1__UP_Relationship_Attribute__c a=new ICIX_V1__UP_Relationship_Attribute__c();
                a.ICIX_V1__UP_Relationship__c=ids;
                a.ICIX_V1__Attribute_Value__c=ResponsibleQE;
                a.ICIX_V1__Attribute__c=c.id;
                UprattriinsertAsst.add(a);
            }
        }   
        list<ICIX_V1__PP_Relationship__c> pprel=[select id,ICIX_V1__Product1__c,ICIX_V1__Product2__c from ICIX_V1__PP_Relationship__c where ICIX_V1__Product1__c in (select ICIX_V1__Product__c from ICIX_V1__UP_Relationship__c where Id in : UprIds)  and ICIX_V1__type__c='Component'];
        set<id> rids=new set<id>();
        for(ICIX_V1__PP_Relationship__c p:pprel){
            if(p.ICIX_V1__Product2__c !=null){
                rids.add(p.ICIX_V1__Product2__c );
            }
        }
        List<ICIX_V1__UP_Relationship__c> uprlsts = new List<ICIX_V1__UP_Relationship__c>();
        if(FacilityName == 'All Trading Partners'){
         uprlsts = [SELECT Id,ICIX_V1__Product__c,ICIX_V1__Related_Account__c FROM ICIX_V1__UP_Relationship__c WHERE ICIX_V1__Related_Account__r.Facility_Name__c != null AND  ICIX_V1__Product__c in : rids];   
        }
        else{
        uprlsts=[select id,ICIX_V1__Product__c from ICIX_V1__UP_Relationship__c where ICIX_V1__Related_Account__r.Facility_Name__c=:FacilityName and ICIX_V1__Product__c in : rids];
        }
        list<ICIX_V1__UP_Relationship_Attribute__c> UpattrRetailItems=[select id,ICIX_V1__Attribute__c,ICIX_V1__UP_Relationship__c from ICIX_V1__UP_Relationship_Attribute__c where ICIX_V1__UP_Relationship__c in:uprlsts and ICIX_V1__Attribute__c=:AttrId];
        system.debug('Existing Attributess==>'+UpattrRetailItems);
        set<Id> Alreadyexists=new set<Id>();
        for(ICIX_V1__UP_Relationship_Attribute__c uprAtt : UpattrRetailItems){
            Alreadyexists.add(uprAtt.ICIX_V1__UP_Relationship__c);
        }
        system.debug('Existing Attributess==>'+Alreadyexists);
        set<Id> ReIds=new set<Id>();
        set<Id> ReUpr=new set<Id>();
        for(ICIX_V1__UP_Relationship__c Upr:uprlsts){
            if(!Alreadyexists.contains(Upr.Id)){
                for(ICIX_V1__Attribute__c c:attr){
                    ICIX_V1__UP_Relationship_Attribute__c a=new ICIX_V1__UP_Relationship_Attribute__c();
                    a.ICIX_V1__UP_Relationship__c=Upr.Id;
                    a.ICIX_V1__Attribute_Value__c=ResponsibleQE;
                    a.ICIX_V1__Attribute__c=c.id;
                    ReIds.add(Upr.ICIX_V1__Product__c);
                    ReUpr.add(Upr.Id);
                    Uprattriinsert.add(a);
                }
            }else{
                ReIds.add(Upr.ICIX_V1__Product__c);
                ReUpr.add(Upr.Id);
            }
        }
        RecursivePreventer.recursiveFlag=false;
        if(UprattriinsertAsst.size()>0){
            system.debug('UPR Tgas'+UprattriinsertAsst);
            database.insert(UprattriinsertAsst,false);
        }
        if(Uprattriinsert.size()>0){
            // insert Uprattriinsert;
            system.debug('Retail Item tags'+Uprattriinsert);
            Database.SaveResult[] srList=database.insert(Uprattriinsert,false);
            for (Database.SaveResult sr : srList) {
                if (sr.isSuccess()) {
                    // Operation was successful, so get the ID of the record that was processed
                    System.debug('Successfully inserted account. Account ID: ' + sr.getId());
                }
                else {
                    // Operation failed, so get all errors                
                    for(Database.Error err : sr.getErrors()) {
                        System.debug('The following error has occurred.');                    
                        System.debug(err.getStatusCode() + ': ' + err.getMessage());
                        System.debug('Account fields that affected this error: ' + err.getFields());
                    }
                }
            }
            system.debug('Calling Future Method');
            //Calling Future methods..Passing Retail Items Upr , Attribute Id and Facility Name as Parameters 
        } 
        if(FacilityName == 'All Trading Partners'){
       
             System.enqueueJob(new SyncRetailItemsQeQueueable(ReIds,ReUpr,AttrId,FacilityName));
        }
        else{
          MassQEController.SyncRetailItemsQE(ReIds,ReUpr,AttrId,FacilityName); 
        }
        return 'Updated successfully.';
    }
    //Asyncronous Processing of the Removal of Retail Items QE's
    @Future
    public static void SyncRetailItemsQE(set<Id> RetailId,set<Id> RetailUPRId,Id AttrId,string TradingPartnerName){
        system.debug('Retail Id==>'+RetailId);
        system.debug('Retail Id UPR==>'+RetailUPRId);
        system.debug('Retail Id UPR==>'+AttrId);
        system.debug('Trading Partner Name==>'+TradingPartnerName);
        list<ICIX_V1__PP_Relationship__c> pprel=[select id,ICIX_V1__Product1__c,ICIX_V1__Product2__c from ICIX_V1__PP_Relationship__c where ICIX_V1__Product2__c in :RetailId  and ICIX_V1__type__c='Component'];
        //map of Retail Item and Its Assortments...
        map<Id,List<Id>> MpRetailItemMap=new map<Id,List<Id>>();
        set<Id> AsstId=new set<Id>();
        for(ICIX_V1__PP_Relationship__c pp : pprel){
            if(pp.ICIX_V1__Product1__c!=null){
                //Adding All Assortment Product Id's in Set
                AsstId.add(pp.ICIX_V1__Product1__c);
                if(MpRetailItemMap.containskey(pp.ICIX_V1__Product2__c)){
                    list<Id> Aid=MpRetailItemMap.get(pp.ICIX_V1__Product2__c);
                    Aid.add(pp.ICIX_V1__Product1__c);
                    MpRetailItemMap.put(pp.ICIX_V1__Product2__c,Aid);
                }else{
                    list<Id> Aid=new List<Id>();
                    Aid.add(pp.ICIX_V1__Product1__c);
                    MpRetailItemMap.put(pp.ICIX_V1__Product2__c,Aid);
                }
            }
        }
        //Removing if any null is present
        AsstId.remove(null);
        //Querying the Attributes of the Retail Item Upr's
        list<ICIX_V1__UP_Relationship_Attribute__c> UpattrRetailItems=[select id,ICIX_V1__Attribute__c,ICIX_V1__Attribute__r.name,ICIX_V1__UP_Relationship__c,ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c from ICIX_V1__UP_Relationship_Attribute__c where ICIX_V1__UP_Relationship__c in:RetailUPRId and ICIX_V1__Attribute__r.ICIX_V1__type__c='Tag'];
        //Querying the Attributes of the Retail Item Upr's 
        list<ICIX_V1__UP_Relationship_Attribute__c> UpattrAst = new List<ICIX_V1__UP_Relationship_Attribute__c>();
        
       
         UpattrAst=[select id,ICIX_V1__Attribute__c,ICIX_V1__UP_Relationship__c,ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c,ICIX_V1__Attribute__r.name from ICIX_V1__UP_Relationship_Attribute__c where ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c in:AsstId and ICIX_V1__UP_Relationship__r.ICIX_V1__Related_Account__r.Facility_Name__c=:TradingPartnerName  and ICIX_V1__Attribute__r.ICIX_V1__type__c='Tag'];
           
       
            //Adding All the Assortment Id and list of Attribute names in the Map..Since an Assortment UPR contains only one Tag..unlike a Retail Item upr which can have multiple Tags of Responsible QE's.
        system.debug('Attribute Retail Items'+UpattrRetailItems);
        system.debug('Attribute Assortments==>'+UpattrAst);
        map<Id,List<String>> MpTag=new map<Id,List<String>>();
        for(ICIX_V1__UP_Relationship_Attribute__c uat :  UpattrAst){
            if(MpTag.containskey(uat.ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c)){
                List<String> Strglst=MpTag.get(uat.ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c);
                Strglst.add(uat.ICIX_V1__Attribute__r.name);
                MpTag.put(uat.ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c,Strglst);
            }else{
                List<String> Strlst=new list<String>();
                Strlst.add(uat.ICIX_V1__Attribute__r.name);
                MpTag.put(uat.ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c,Strlst);
            }
        }
        //Checking to Delete the Responsible QE tags of the Retail Items Upr's when it is not present in the Assortment UPR's of the Related Trading Partner..
        List<ICIX_V1__UP_Relationship_Attribute__c> ToDelete=new List<ICIX_V1__UP_Relationship_Attribute__c>();
        //Looping throgh the Retail Items UPR..
        for(ICIX_V1__UP_Relationship_Attribute__c uar : UpattrRetailItems){
            //Getting the Assortment under which the Retail Items are present..
            if(MpRetailItemMap.containskey(uar.ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c)){
                List<Id> Aid=MpRetailItemMap.get(uar.ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c);
                if(Aid.size()>0){
                    set<String> Strg=new set<String>();
                    system.debug('Assortment Ids==>'+Aid);
                    system.debug('Assortment Tags==>'+MpTag);
                    for(Id Ids : Aid){
                        if(MpTag.containskey(Ids)){
                            Strg.addAll(MpTag.get(Ids));
                        }
                    }
                    system.debug('Assortment Attributes==>'+Strg);
                    Strg.remove(null);
                    if(!Strg.contains(uar.ICIX_V1__Attribute__r.name)){
                        system.debug('To delete==>'+uar);
                        ToDelete.add(uar); 
                    }
                }
            }
        }
        system.debug('To delete the Attribytes==>'+ToDelete);
        //Deleting the Responsible QE tags which are not under any related Assortment Tags..
        Delete ToDelete;
    }
 
AbhishekAbhishek (Salesforce Developers) 
Hi,

Try the suggestions as mentioned in the below developer discussion,

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines


I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Thanks.
santhiya duraisanthiya durai
Hi @Abhishek, Thanks for your response. I already gone through the link which you shared but it is not helping in my case