0

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UPCustomTrigger: execution of AfterInsert

caused by: System.QueryException: List has no rows for assignment to SObject

Class.UPCustomtriggerHandler.afterUprInsert: line 278, column 1 Trigger.UPCustomTrigger: line 10, column 1: []

I am getting this error but not able to figure it out. Due to this am not able to cover 5 test classes. Could anyone please clear me why am getting this error.

The issue am facing is I am not able to insert/update ICIX_V1__UP_Relationship__c records.

Trigger:

trigger UPCustomTrigger on ICIX_V1__UP_Relationship__c (After Insert,Before Update,Before Insert) {
    if(Triggercontrol.check('ICIX_V1__UP_Relationship__c1')){
        if(RecursivePreventer.recursiveFlag){
            if( Trigger.IsBefore &&  Trigger.isupdate){
                UPCustomtriggerHandler.updateUprName(trigger.new);
                UPCustomtriggerHandler.BeforeUpdate(trigger.new,trigger.oldmap);
            }
            if( Trigger.Isafter && Trigger.isInsert ){
              UPCustomtriggerHandler.AfterInsert(trigger.new);
                UPCustomtriggerHandler.afterUprInsert(trigger.new);
               
            }
            if( Trigger.IsBefore && Trigger.isInsert ){
               UPCustomtriggerHandler.updateUprName(trigger.new);
            }
        }
    }
}

Controller:

public class UPCustomtriggerHandler{
    public static void afterUprInsert(List<ICIX_V1__UP_Relationship__c> newUprList){
    RecursivePreventer.recursiveFlag=false;
    System.debug('========newUprList======='+newUprList);
    Set<ICIX_V1__UP_Relationship__c> uprInsertSet = new Set<ICIX_V1__UP_Relationship__c>();
    Set<Id> productIdSet = new Set<Id>();
    Map<Id,List<Id>> productIdVsTpIdMap = new Map<Id,List<Id>>();
    Id InternalAccId = [SELECT Id FROM Account WHERE ICIX_V1__Internal__c = true].Id;
    for(ICIX_V1__UP_Relationship__c upc : newUprList){
         productIdSet.add(upc.ICIX_V1__Product__c);
        if(!productIdVsTpIdMap.containsKey(upc.ICIX_V1__Product__c)){
            productIdVsTpIdMap.put(upc.ICIX_V1__Product__c, new List<Id>());
            productIdVsTpIdMap.get(upc.ICIX_V1__Product__c).add(upc.ICIX_V1__Related_Account__c);
        }
        else{
            productIdVsTpIdMap.get(upc.ICIX_V1__Product__c).add(upc.ICIX_V1__Related_Account__c);
        }
    }
    System.debug('=======productIdVsTpIdMap========'+productIdVsTpIdMap+'======'+productIdVsTpIdMap.size());
    if(productIdSet.size()>0){
        Set<Id> allProductIdsSet = new Set<Id>();
        List<ICIX_V1__PP_Relationship__c> ppList =[SELECT Id ,ICIX_V1__Product1__c,ICIX_V1__Product2__c,ICIX_V1__Product2__r.Name,ICIX_V1__Product1__r.Name FROM ICIX_V1__PP_Relationship__c WHERE ICIX_V1__Product1__c IN :productIdSet and ICIX_V1__Status__c='active' and ICIX_V1__Product2__c!=null];
        for(ICIX_V1__PP_Relationship__c pp: ppList ){
            allProductIdsSet.add(pp.ICIX_V1__Product2__c);
        }
        System.debug('========allProductIdsSet======='+allProductIdsSet); 
        Map<Id,Id> emptyUprMap = new Map<Id,Id>();
        List<ICIX_V1__UP_Relationship__c> emptyUprList = [SELECT Id,ICIX_V1__Product__c,ICIX_V1__Related_Account__c FROM ICIX_V1__UP_Relationship__c WHERE  ICIX_V1__Product__c IN : allProductIdsSet AND ICIX_V1__Related_Account__c=null];
        for(ICIX_V1__UP_Relationship__c upr:emptyUprList ){
            emptyUprMap.put(upr.ICIX_V1__Product__c,upr.Id);
        }
        System.debug('========emptyUprMap======='+emptyUprMap); 
        Map<Id,Set<Id>> existingproductIdVsUPRIdsMap =new map<Id,Set<Id>>();
        List<ICIX_V1__UP_Relationship__c> existingUprList = [SELECT Id,ICIX_V1__Product__c,ICIX_V1__Related_Account__c FROM ICIX_V1__UP_Relationship__c WHERE  ICIX_V1__Product__c IN : allProductIdsSet AND ICIX_V1__Related_Account__c!=null];
        for(ICIX_V1__UP_Relationship__c upt : existingUprList ){
            if(existingproductIdVsUPRIdsMap.containskey(upt.ICIX_V1__Product__c)){
                Set<Id> tpIdsSet = existingproductIdVsUPRIdsMap.get(upt.ICIX_V1__Product__c);   
                tpIdsSet.add(upt.ICIX_V1__Related_Account__c);
                existingproductIdVsUPRIdsMap.put(upt.ICIX_V1__Product__c,tpIdsSet);   
            }else{
                Set<Id> tpIdsSet = New set<Id>();   
                tpIdsSet.add(upt.ICIX_V1__Related_Account__c);
                existingproductIdVsUPRIdsMap.put(upt.ICIX_V1__Product__c,tpIdsSet);   
            }
        }
         System.debug('========existingproductIdVsUPRIdsMap======='+existingproductIdVsUPRIdsMap); 
        Set<ICIX_V1__UP_Relationship__c> uprUpdateSet = new Set<ICIX_V1__UP_Relationship__c>();
        List<ICIX_V1__UP_Relationship__c> uprUpdateList = new List<ICIX_V1__UP_Relationship__c>();
        //For Retail Items
        for(ICIX_V1__PP_Relationship__c ppt: ppList ){
              if(productIdVsTpIdMap.containsKey(ppt.ICIX_V1__Product1__c)){
                  for(Id tpId: productIdVsTpIdMap.get(ppt.ICIX_V1__Product1__c)){
                      If(emptyUprMap.containskey(ppt.ICIX_V1__Product2__c)){
                system.debug('Inside Blank');
                ICIX_V1__UP_Relationship__c uprUp = new ICIX_V1__UP_Relationship__c();
                uprUp.Id = emptyUprMap.get(ppt.ICIX_V1__Product2__c);
                uprUp.ICIX_V1__Related_Account__c = tpId;
                uprUpdateset.add(uprUp);
                emptyUprMap.remove(ppt.ICIX_V1__Product2__c);
            }else{
                system.debug('No Blank Upr');
                Set<Id> tpIdsSet = existingproductIdVsUPRIdsMap.get(ppt.ICIX_V1__Product2__c);
                system.debug('No Blank Upr tpIdsSet='+tpIdsSet);
                if(tpIdsSet != null && tpId != null && !tpIdsSet.contains(tpId)){
                    system.debug('Inside Not Present');
                    ICIX_V1__UP_Relationship__c uprIn = new ICIX_V1__UP_Relationship__c();
                    uprIn.Name = ppt.ICIX_V1__Product2__r.Name;
                    uprIn.ICIX_V1__Related_Account__c = tpId;
                    uprIn.ICIX_V1__Product__c = ppt.ICIX_V1__Product2__c;
                    uprIn.ICIX_V1__Status__c = 'Active';
                    uprIn.ICIX_V1__Type__c = 'Buy';
                    uprIn.ICIX_V1__UBE__c = InternalAccId;
                    uprIn.ICIX_V1__Hub_Id__c = generateUUID();  
                    uprInsertSet.add(uprIn);
                } else{
                    if(tpIdsSet == null ){
                        System.debug('====New Upr=====');
                   ICIX_V1__UP_Relationship__c uprIn = new ICIX_V1__UP_Relationship__c();
                    uprIn.Name = ppt.ICIX_V1__Product2__r.Name;
                    uprIn.ICIX_V1__Related_Account__c = tpId;
                    uprIn.ICIX_V1__Product__c = ppt.ICIX_V1__Product2__c;
                    uprIn.ICIX_V1__Status__c = 'Active';
                    uprIn.ICIX_V1__Type__c = 'Buy';
                    uprIn.ICIX_V1__UBE__c = InternalAccId;
                    uprIn.ICIX_V1__Hub_Id__c = generateUUID();    
                    uprInsertSet.add(uprIn); 
                }
                } 
            }
                  }
              }
        }
          System.debug('========uprUpdateset======='+uprUpdateset);
        if(!uprUpdateset.IsEmpty()){
            system.debug('Updated Records==>'+uprUpdateset);
            for(ICIX_V1__UP_Relationship__c uprts : uprUpdateset){
                if(uprts.ICIX_V1__Related_Account__c!=null){
                    uprUpdateList.add(uprts);
                }
            }
            //Update uprUpdateList;//Assigning to Existing TPR's
            Database.update(uprUpdateList, false);
        }
          System.debug('Inserted Records==>'+uprInsertSet);
        if(!uprInsertSet.IsEmpty()){
           // RecursivePreventer.recursiveFlag=true;
            List<ICIX_V1__UP_Relationship__c>  uprInsertLst=new List<ICIX_V1__UP_Relationship__c>();
            for(ICIX_V1__UP_Relationship__c uprt : uprInsertSet){
                if(uprt.ICIX_V1__Related_Account__c!=null){
                    uprInsertLst.add(uprt);
                }
            }
            System.debug('Inserted Records==>'+uprInsertLst);
            //Insert uprInsertLst;//Inserting New TPR's
            database.Insert(uprInsertLst,false);
       // }
        System.debug('Records==>'+uprInsertLst);
        List<ICIX_V1__UP_Relationship__c> newUpr = uprInsertLst;
        RecursivePreventer.recursiveFlag=false;
    set<Id> AllIds=new set<Id>(); 
    map<Id,List<Id>> MpInsert=new map<Id,List<Id>>();
    Map<Id, Id> uprIdVsProductIdMap = new Map<Id, Id>();
    List<Id> uprIdList = new List<Id>();
    for(ICIX_V1__UP_Relationship__c upc : newUpr){
        if(!MpInsert.containsKey(upc.ICIX_V1__Product__c)){
          MpInsert.put(upc.ICIX_V1__Product__c,new List<Id>());
          MpInsert.get(upc.ICIX_V1__Product__c).add(upc.Id);
        }
        else{
          MpInsert.get(upc.ICIX_V1__Product__c).add(upc.Id);
        }
        //MpInsert.put(upc.ICIX_V1__Product__c,upc.Id);
        AllIds.add(upc.ICIX_V1__Product__c);
        uprIdVsProductIdMap.put(upc.Id, upc.ICIX_V1__Product__c);
        uprIdList.add(upc.Id);
    }
            System.debug('=======uprIdList======'+uprIdList);
            System.debug('====AllIds======'+AllIds);
    List<ICIX_V1__ICIX_Product__c> productsList = [SELECT Id, ICIX_V1__External_Id__c FROM ICIX_V1__ICIX_Product__c WHERE Id IN: AllIds AND ICIX_V1__External_Id__c != null];
    Map<String, String> productIdVsExternalIdMap = new Map<String, String>();
    if(productsList.size()>0){
        for(ICIX_V1__ICIX_Product__c pro: productsList){
            productIdVsExternalIdMap.put(pro.Id, pro.ICIX_V1__External_Id__c);
        }
    }
   System.debug('=========productIdVsExternalIdMap======='+productIdVsExternalIdMap); 
    map<Id,Id> mpUprF=new map<Id,Id>();
    for(ICIX_V1__UP_Relationship__c upr :[select id,ICIX_V1__Product__c,ICIX_V1__Related_Account__c from ICIX_V1__UP_Relationship__c where ICIX_V1__Product__c in : AllIds and ICIX_V1__Related_Account__c!=null and  Id not in : newUpr] ){
        mpUprF.put(upr.ICIX_V1__Product__c,upr.Id);
    }
    List<ICIX_V1__UP_Relationship_Attribute__c> uprlsts=new list<ICIX_V1__UP_Relationship_Attribute__c>();
    list<ICIX_V1__Product_Non_Universal_Id__c> ProdNonId=new List<ICIX_V1__Product_Non_Universal_Id__c>();
    map<string,ICIX_V1__UP_Relationship_Attribute__c> MpAttr=new map<string,ICIX_V1__UP_Relationship_Attribute__c>();
    map<string,ICIX_V1__Product_Non_Universal_Id__c> MpNon=new map<string,ICIX_V1__Product_Non_Universal_Id__c>();
    List<ICIX_V1__UP_Relationship_Attribute__c> UprAlst=[select id,ICIX_V1__Attribute__c,ICIX_V1__Attribute_Value__c,ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c,ICIX_V1__UP_Relationship__c from ICIX_V1__UP_Relationship_Attribute__c where ICIX_V1__UP_Relationship__c in : mpUprF.values() and ICIX_V1__Attribute__r.ICIX_V1__Type__c='Attribute'];
    for(ICIX_V1__UP_Relationship_Attribute__c upra: UprAlst){
        MpAttr.put(upra.ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c+'-'+upra.ICIX_V1__Attribute__c,upra);
    }
    List<ICIX_V1__Product_Non_Universal_Id__c> ProdNonLst=[select id,ICIX_V1__Id_Type__c,ICIX_V1__Id_Value__c,ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c from ICIX_V1__Product_Non_Universal_Id__c where ICIX_V1__UP_Relationship__c in : mpUprF.values()];
    for(ICIX_V1__Product_Non_Universal_Id__c upra: ProdNonLst){
        MpNon.put(upra.ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c+'-'+upra.ICIX_V1__Id_Type__c,upra);
    }
    Hasbro_UpRelationshipTriggerHelper.FirstRun=false;
    for(ICIX_V1__UP_Relationship_Attribute__c uprt: MpAttr.values()){
        if(MpInsert.containskey(uprt.ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c)){
            for(Id uprId: MpInsert.get(uprt.ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c)){
            ICIX_V1__UP_Relationship_Attribute__c Upra=new ICIX_V1__UP_Relationship_Attribute__c();
            Upra.ICIX_V1__Attribute_Value__c=uprt.ICIX_V1__Attribute_Value__c;
            Upra.ICIX_V1__Attribute__c=uprt.ICIX_V1__Attribute__c;
            Upra.ICIX_V1__UP_Relationship__c= uprId;
            uprlsts.add(Upra);
        }
        }
    }
    Insert uprlsts;//Inserting Product Attributes
    List<Id> prodNonUniIdUprIdList = new List<Id>();
    for(ICIX_V1__Product_Non_Universal_Id__c PnID : MpNon.values() ){
        if(MpInsert.containskey(PnID.ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c)){
             for(Id uprId: MpInsert.get(PnID.ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c)){
            ICIX_V1__Product_Non_Universal_Id__c Pid=new ICIX_V1__Product_Non_Universal_Id__c();
            Pid.ICIX_V1__Id_Type__c=PnID.ICIX_V1__Id_Type__c;
            pid.ICIX_V1__Id_Value__c=PnId.ICIX_V1__Id_Value__c;
            pid.ICIX_V1__UP_Relationship__c= uprId;
            prodNonUniIdUprIdList.add(uprId);
            ProdNonId.add(Pid);
        }
        }
    }
             System.debug('========ProdNonId======'+ProdNonId+'===================='+ProdNonId.size());
    ICIX_V1__Id_Type__c idType = [SELECT Id, Name, ICIX_V1__Type__c, ICIX_V1__Unique_Id__c
                                  FROM ICIX_V1__Id_Type__c
                                  WHERE ICIX_V1__Unique_Id__c = 'ITEM_N'
                                  AND ICIX_V1__Type__c = 'NonUniversal' LIMIT 1];
    System.debug('=========idType========'+idType);
    if(idType != null){
        for(Id uprId: uprIdList){
            if(uprId != null && !prodNonUniIdUprIdList.contains(uprId)){
                ICIX_V1__Product_Non_Universal_Id__c PnuId = new ICIX_V1__Product_Non_Universal_Id__c();
                PnuId.ICIX_V1__Id_Type__c = idType.Id;
                PnuId.ICIX_V1__Id_Value__c = productIdVsExternalIdMap.get(uprIdVsProductIdMap.get(uprId));
                PnuId.ICIX_V1__UP_Relationship__c = uprId;
                ProdNonId.add(PnuId);
            }
        }
    }
    System.debug('========ProdNonId======'+ProdNonId+'===================='+ProdNonId.size());
    if(ProdNonId.size()>0){
        insert ProdNonId;//Inserting Product Non Universal Ids
    }
        }    
    }
}

public Static String generateUUID() {
    Blob b = Crypto.GenerateAESKey(128);
    String h = EncodingUtil.ConvertTohex(b);
    String guid = h.SubString(0,8)+ '-' + h.SubString(8,12) + '-' + h.SubString(12,16) + '-' + h.SubString(16,20) + '-' + h.substring(20);
    return guid;
}

}

The Error is at below line what mistake I made? Id InternalAccId = [SELECT Id FROM Account WHERE ICIX_V1__Internal__c = true].Id;

1 Answer 1

0

Your query:

ICIX_V1__Id_Type__c idType = [SELECT Id, Name, ICIX_V1__Type__c, ICIX_V1__Unique_Id__c
                              FROM ICIX_V1__Id_Type__c
                              WHERE ICIX_V1__Unique_Id__c = 'ITEM_N'
                                  AND ICIX_V1__Type__c = 'NonUniversal' LIMIT 1];

Will throw this exception when there are no rows to assign (no record is found). The following code:

        if(idType != null){

Won't protect you from this exception. To fix the problem, add a for-query loop:

        ICIX_V1__Id_Type__c idType;
        for(ICIX_V1__Id_Type__c record:[SELECT Id, Name, ICIX_V1__Type__c, ICIX_V1__Unique_Id__c
                                      FROM ICIX_V1__Id_Type__c
                                      WHERE ICIX_V1__Unique_Id__c = 'ITEM_N'
                                      AND ICIX_V1__Type__c = 'NonUniversal' LIMIT 1]) {
            idType = record;
        }
        if(idType != null){

As far as your unit test goes, you need to manually create the record (see Isolation of Test Data from Organization Data in Unit Tests).

3
  • Hi @sfdcfox. I tried the solution which you have given. its throwing an error. on: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UPCustomTrigger: execution of AfterInsert caused by: System.ListException: List index out of bounds: 0
    – Santhiya
    Sep 16, 2020 at 8:38
  • and getting a new error on the same controller. I have edited my question as per my concern.
    – Santhiya
    Sep 16, 2020 at 8:39
  • @Santhiya The same mistake as before. You need to write your code to accept the possibility that there may be no records present.
    – sfdcfox
    Sep 16, 2020 at 12:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .