You need to sign in to do that
Don't have an account?
sumit d
trigger to delete assets from Line item
HI All,
I have a trigger in which i am deleting asset when its related opportunity line item is deleted but its giving me error that list System.QueryException: List has more than 1 row for assignment to SObject
How to solve this error?
My trigger helper is given below:-
public without sharing class OpportunityLineItemTriggerHelper {
public static List<OpportunityLineItem> newOpportunityLineItem = new List<OpportunityLineItem>();
public static List<OpportunityLineItem> oldOpportunityLineItem = new List<OpportunityLineItem>();
public static Map<Id, OpportunityLineItem> newMapOpportunityLineItem = new Map<Id, OpportunityLineItem>();
public static Map<Id, OpportunityLineItem> oldMapOpportunityLineItem = new Map<Id, OpportunityLineItem>();
Public static void deleteAssets( ){
Set<Id> applicableOppIds = new Set<Id>();
for( OpportunityLineItem oli : oldOpportunityLineItem ){
applicableOppIds.add( oli.OpportunityId );
}
Map<String, Asset> mapOfProCodeToAsset = new Map<String, Asset>();
List<Asset> ListAsset = [Select Id, Product2.ProductCode, AccountId From Asset ];
for( Asset ast : ListAsset ){
String uniqueId = ast.Product2.ProductCode + '-' + ast.AccountId;
mapOfProCodeToAsset.put( uniqueId,ast );
}
System.debug('mapOfProCodeToAsset'+mapOfProCodeToAsset);
List<Asset> deleteListAsset = new List<Asset>();
//Collecting all child records related to Parent records
for( Opportunity oppObj : [ SELECT Id, Name, StageName, AccountId, Account.Name,
( SELECT Id, Name, OpportunityId, Current_Year__c,
Product2Id, Product2.Name,Product2.ProductCode, Product2.Family,
Product2.AcctSeedERP__Inventory_Asset__c, Quantity, Description
FROM OpportunityLineItems
Where Id In : oldMapOpportunityLineItem.keySet() )
FROM Opportunity
WHERE Id IN :applicableOppIds
] ){
for( OpportunityLineItem oppLine : oldOpportunityLineItem ){
Id prod = oppLine.Product2Id;
//Opportunity opp = oppLine.Opportunity;
System.debug('product'+prod);
String uniqueId = oppLine.ProductCode + '-' + oppObj.AccountId;
System.debug('UniqueListAsset'+uniqueId);
if(mapOfProCodeToAsset.containsKey(uniqueId)){
System.debug('#####' );
deleteListAsset.add(mapOfProCodeToAsset.values());
System.debug('deleteListAsset'+deleteListAsset.Size());
}
}
}
System.debug('deleteListAsset'+deleteListAsset);
if( deleteListAsset.Size() > 0 ){
delete deleteListAsset;
}
}
}
I have a trigger in which i am deleting asset when its related opportunity line item is deleted but its giving me error that list System.QueryException: List has more than 1 row for assignment to SObject
How to solve this error?
My trigger helper is given below:-
public without sharing class OpportunityLineItemTriggerHelper {
public static List<OpportunityLineItem> newOpportunityLineItem = new List<OpportunityLineItem>();
public static List<OpportunityLineItem> oldOpportunityLineItem = new List<OpportunityLineItem>();
public static Map<Id, OpportunityLineItem> newMapOpportunityLineItem = new Map<Id, OpportunityLineItem>();
public static Map<Id, OpportunityLineItem> oldMapOpportunityLineItem = new Map<Id, OpportunityLineItem>();
Public static void deleteAssets( ){
Set<Id> applicableOppIds = new Set<Id>();
for( OpportunityLineItem oli : oldOpportunityLineItem ){
applicableOppIds.add( oli.OpportunityId );
}
Map<String, Asset> mapOfProCodeToAsset = new Map<String, Asset>();
List<Asset> ListAsset = [Select Id, Product2.ProductCode, AccountId From Asset ];
for( Asset ast : ListAsset ){
String uniqueId = ast.Product2.ProductCode + '-' + ast.AccountId;
mapOfProCodeToAsset.put( uniqueId,ast );
}
System.debug('mapOfProCodeToAsset'+mapOfProCodeToAsset);
List<Asset> deleteListAsset = new List<Asset>();
//Collecting all child records related to Parent records
for( Opportunity oppObj : [ SELECT Id, Name, StageName, AccountId, Account.Name,
( SELECT Id, Name, OpportunityId, Current_Year__c,
Product2Id, Product2.Name,Product2.ProductCode, Product2.Family,
Product2.AcctSeedERP__Inventory_Asset__c, Quantity, Description
FROM OpportunityLineItems
Where Id In : oldMapOpportunityLineItem.keySet() )
FROM Opportunity
WHERE Id IN :applicableOppIds
] ){
for( OpportunityLineItem oppLine : oldOpportunityLineItem ){
Id prod = oppLine.Product2Id;
//Opportunity opp = oppLine.Opportunity;
System.debug('product'+prod);
String uniqueId = oppLine.ProductCode + '-' + oppObj.AccountId;
System.debug('UniqueListAsset'+uniqueId);
if(mapOfProCodeToAsset.containsKey(uniqueId)){
System.debug('#####' );
deleteListAsset.add(mapOfProCodeToAsset.values());
System.debug('deleteListAsset'+deleteListAsset.Size());
}
}
}
System.debug('deleteListAsset'+deleteListAsset);
if( deleteListAsset.Size() > 0 ){
delete deleteListAsset;
}
}
}