+ Start a Discussion
santhiya duraisanthiya durai 

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


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.AfterInsert: line 202, column 1 Trigger.UPCustomTrigger: line 9, 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 BeforeUpdate(list< ICIX_V1__UP_Relationship__c> newRecords, Map<ID, ICIX_V1__UP_Relationship__c> oldRecordMap){ RecursivePreventer.recursiveFlag=false; set<ICIX_V1__UP_Relationship__c> uprInsert=new set<ICIX_V1__UP_Relationship__c>(); set<Id> Aid=new Set<Id>(); set<String> AlreadyExts=new set<String>(); map<Id,Id> uprAccmp=new map<Id,Id>(); Id InternalAccId=[select Id from Account where ICIX_V1__Internal__c=true].Id; system.debug('Trigger.new map'+JSON.serialize(newRecords)); system.debug('Trigger.Old map'+JSON.serialize(oldRecordMap)); for(ICIX_V1__UP_Relationship__c upc : newRecords){ system.debug('New Related Account==>'+ upc.ICIX_V1__Related_Account__c); system.debug('Old Related Account==>'+ oldRecordMap.get(upc.Id).ICIX_V1__Related_Account__c); if(upc.ICIX_V1__Related_Account__c!=null && oldRecordMap.get(upc.Id).ICIX_V1__Related_Account__c==null){ Aid.add(upc.ICIX_V1__Product__c); uprAccmp.put(upc.ICIX_V1__Product__c,upc.ICIX_V1__Related_Account__c); } if(upc.ICIX_V1__Related_Account__c!=null && oldRecordMap.get(upc.Id).ICIX_V1__Related_Account__c!=null && upc.ICIX_V1__Related_Account__c==oldRecordMap.get(upc.Id).ICIX_V1__Related_Account__c){ AlreadyExts.add(upc.ICIX_V1__Product__c+'-'+upc.ICIX_V1__Related_Account__c); } if(upc.ICIX_V1__Related_Account__c!=null && oldRecordMap.get(upc.Id).ICIX_V1__Related_Account__c!=upc.ICIX_V1__Related_Account__c && oldRecordMap.get(upc.Id).ICIX_V1__Related_Account__c!=null){ system.debug('New Related Account==>'+ upc.ICIX_V1__Related_Account__c); system.debug('Old Related Account==>'+ oldRecordMap.get(upc.Id).ICIX_V1__Related_Account__c); ICIX_V1__UP_Relationship__c secondUpRec=new ICIX_V1__UP_Relationship__c(); secondUpRec.Name=upc.Name; secondUpRec.ICIX_V1__Related_Account__c=upc.ICIX_V1__Related_Account__c; secondUpRec.ICIX_V1__Product__c=upc.ICIX_V1__Product__c; secondUpRec.ICIX_V1__Status__c='Active'; secondUpRec.ICIX_V1__Type__c='Buy'; secondUpRec.ICIX_V1__UBE__c=InternalAccId; uprInsert.add(secondUpRec); Aid.add(upc.ICIX_V1__Product__c); uprAccmp.put(upc.ICIX_V1__Product__c,upc.ICIX_V1__Related_Account__c); upc.ICIX_V1__Related_Account__c=oldRecordMap.get(upc.Id).ICIX_V1__Related_Account__c; } } if(!Aid.isempty()){ system.debug('Assortment Ids==>'+uprInsert); set<Id> AllIds=new set<Id>(); List<ICIX_V1__PP_Relationship__c> pplst=[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 :Aid and ICIX_V1__Status__c='active' and ICIX_V1__Product2__c!=null]; for(ICIX_V1__PP_Relationship__c pp: pplst ){ AllIds.add(pp.ICIX_V1__Product2__c); } system.debug('Retail Item Ids==>'+AllIds); //Upr Records map<Id,Id> mpUpr=new map<Id,Id>(); List<ICIX_V1__UP_Relationship__c> uprlstRe=[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]; for(ICIX_V1__UP_Relationship__c upr:uprlstRe ){ mpUpr.put(upr.ICIX_V1__Product__c,upr.Id); } system.debug('Retail Item Ids Blank UPR==>'+JSON.serialize(mpUpr)); map<Id,Set<Id>> ExistingUPRs=new map<Id,Set<Id>>(); List<ICIX_V1__UP_Relationship__c> AlAssgned=[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]; for(ICIX_V1__UP_Relationship__c upt : AlAssgned ){ if(ExistingUPRs.containskey(upt.ICIX_V1__Product__c)){ Set<Id> TpID=ExistingUPRs.get(upt.ICIX_V1__Product__c); TpID.add(upt.ICIX_V1__Related_Account__c); ExistingUPRs.put(upt.ICIX_V1__Product__c,TpID); }else{ Set<Id> TpID=New set<Id>(); TpID.add(upt.ICIX_V1__Related_Account__c); ExistingUPRs.put(upt.ICIX_V1__Product__c,TpID); } } system.debug('Retail Item Ids Populated UPR==>'+JSON.serialize(ExistingUPRs)); system.debug('Retail Item Ids Populated UPR==>'+JSON.serialize(uprAccmp)); //Attributes List<ICIX_V1__UP_Relationship__c> uprUpdateset=new List<ICIX_V1__UP_Relationship__c>(); List<ICIX_V1__UP_Relationship__c> uprUpdatelst=new List<ICIX_V1__UP_Relationship__c>(); //For Retail Items for(ICIX_V1__PP_Relationship__c ppt: pplst ){ If(mpUpr.containskey(ppt.ICIX_V1__Product2__c)){ system.debug('Inside Blank'); ICIX_V1__UP_Relationship__c uprUp=new ICIX_V1__UP_Relationship__c(); uprUp.Id=mpUpr.get(ppt.ICIX_V1__Product2__c); uprUp.ICIX_V1__Related_Account__c=uprAccmp.get(ppt.ICIX_V1__Product1__c); uprUpdateset.add(uprUp); mpUpr.remove(ppt.ICIX_V1__Product2__c); }else{ system.debug('No Blank Upr'); Set<Id> Asid=ExistingUPRs.get(ppt.ICIX_V1__Product2__c); system.debug('No Blank Upr Asid='+Asid); system.debug('No Blank Upr uprAccmp.get(ppt.ICIX_V1__Product1__c)='+uprAccmp.get(ppt.ICIX_V1__Product1__c)); if(Asid != null && uprAccmp != null && uprAccmp.get(ppt.ICIX_V1__Product1__c) != null && !Asid.contains(uprAccmp.get(ppt.ICIX_V1__Product1__c))){ 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=uprAccmp.get(ppt.ICIX_V1__Product1__c); 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; uprInsert.add(uprIn); } else{ 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=uprAccmp.get(ppt.ICIX_V1__Product1__c); 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; uprInsert.add(uprIn); } } } if(!uprUpdateset.IsEmpty()){ system.debug('Updated Records==>'+uprUpdateset); for(ICIX_V1__UP_Relationship__c uprts : uprUpdateset){ if(uprts.ICIX_V1__Related_Account__c!=null){ uprUpdatelst.add(uprts); } } //Update uprUpdatelst;//Assigning to Existing TPR's Database.Update(uprUpdatelst, false); } if(!uprInsert.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 : uprInsert){ if(uprt.ICIX_V1__Related_Account__c!=null){ if(!AlreadyExts.contains(uprt.ICIX_V1__Product__c+'-'+uprt.ICIX_V1__Related_Account__c)){ uprInsertLst.add(uprt); } } } system.debug('Inserted Records==>'+uprInsertLst); //Insert uprInsertLst;//Inserting New TPR's database.Insert(uprInsertLst,false); } } } Public static void AfterInsert(list<ICIX_V1__UP_Relationship__c> newUpr){ 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); } 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); } } 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 void updateUprName(List<ICIX_V1__UP_Relationship__c> Newrecordlist){ set<Id> Pid=new set<Id>(); for(ICIX_V1__UP_Relationship__c Upr :Newrecordlist){ If(Upr.Name.contains('_*')){ Pid.add(Upr.ICIX_V1__Product__c); } } Pid.remove(null); if(!Pid.Isempty()){ map<Id,ICIX_V1__ICIX_Product__c> Mproduct=new map<Id,ICIX_V1__ICIX_Product__c>([Select id,ICIX_V1__Description__c,Product_Number__c from ICIX_V1__ICIX_Product__c where id in :Pid ]); List<ICIX_V1__UP_Relationship_Attribute__c> UpAttlst=[Select Id,ICIX_V1__Attribute_Value__c,ICIX_V1__UP_Relationship__c from ICIX_V1__UP_Relationship_Attribute__c where ICIX_V1__Attribute__r.Name='Product Number' and ICIX_V1__UP_Relationship__r.ICIX_V1__product__c in:pid]; map<Id,String> MpAttr=new map<Id,String>(); for(ICIX_V1__UP_Relationship_Attribute__c upr : UpAttlst){ MpAttr.put(upr.ICIX_V1__UP_Relationship__c,upr.ICIX_V1__Attribute_Value__c); } List<ICIX_V1__ICIX_Product_Attribute__c> productAttributeList = [SELECT Id,Name, ICIX_V1__ICIX_Product__c, ICIX_V1__Attribute__c, ICIX_V1__Attribute__r.Name, ICIX_V1__Attribute_Value__c FROM ICIX_V1__ICIX_Product_Attribute__c WHERE ICIX_V1__Attribute__r.Name = 'Product Number' AND ICIX_V1__Attribute_Value__c != null AND ICIX_V1__ICIX_Product__c IN: pid]; Map<Id, String> productIdVsAttributeValueMap = new Map<Id, String>(); if(productAttributeList.size()>0 && !productAttributeList.isEmpty()){ for(ICIX_V1__ICIX_Product_Attribute__c pa: productAttributeList){ productIdVsAttributeValueMap.put(pa.ICIX_V1__ICIX_Product__c, pa.ICIX_V1__Attribute_Value__c); } } for(ICIX_V1__UP_Relationship__c Upr : Newrecordlist){ if(Mproduct.containskey(Upr.ICIX_V1__Product__c)){ system.debug('Available Attributes'); if(MpAttr.containskey(Upr.Id)){ Upr.Name=MpAttr.get(Upr.Id)+'_'+Mproduct.get(Upr.ICIX_V1__Product__c).ICIX_V1__Description__c; } else if(productIdVsAttributeValueMap.containsKey(Upr.ICIX_V1__Product__c)){ Upr.Name = productIdVsAttributeValueMap.get(Upr.ICIX_V1__Product__c)+'_'+Mproduct.get(Upr.ICIX_V1__Product__c).ICIX_V1__Description__c; } else { if(Mproduct.get(Upr.ICIX_V1__Product__c).Product_Number__c!=null){ Upr.Name=Mproduct.get(Upr.ICIX_V1__Product__c).Product_Number__c+'_'+Mproduct.get(Upr.ICIX_V1__Product__c).ICIX_V1__Description__c; } } } } } } }
AbhishekAbhishek (Salesforce Developers) 
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.


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

Thanks.