You need to sign in to do that
Don't have an account?
APEX trigger CreateAssetonClosedWon
I downloaded the APEX trigger to create assets from Opportunity Line Items. How can I then update the opportunity line items with the ID of the newly created Asset? Any help is appreciated!
trigger CreateAssetonClosedWon on Opportunity (after insert, after update) {
for(Opportunity o: trigger.new){
if(o.isWon == true && o.HasOpportunityLineItem == true && o.type == 'Purchase'){
String opptyId = o.Id;
OpportunityLineItem[] OLI = [Select UnitPrice, Quantity, PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Description, Converted_to_Asset__c, DOM__c, Asset__c
From OpportunityLineItem
where OpportunityId = :opptyId and Converted_to_Asset__c = false];
Asset[] ast = new Asset[]{};
Asset a = new Asset();
for(OpportunityLineItem ol: OLI){
a = new Asset();
a.AccountId = o.AccountId;
a.Product2Id = ol.PricebookEntry.Product2Id;
a.Quantity = ol.Quantity;
a.Price = ol.UnitPrice;
a.PurchaseDate = o.CloseDate;
a.Status = 'Purchased';
a.Description = ol.Description;
a.Name = ol.PricebookEntry.Product2.Name;
a.Original_Opportunity__c = opptyID;
a.In_Inventory__c = true;
a.Date_of_Manufacturer__c = ol.DOM__c;
ast.add(a);
ol.Converted_to_Asset__c = true;
}
update OLI;
insert ast;
}
}
}
trigger CreateAssetonClosedWon on Opportunity (after insert, after update) {
for(Opportunity o: trigger.new){
if(o.isWon == true && o.HasOpportunityLineItem == true && o.type == 'Purchase'){
String opptyId = o.Id;
OpportunityLineItem[] OLI = [Select UnitPrice, Quantity, PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Description, Converted_to_Asset__c, DOM__c, Asset__c
From OpportunityLineItem
where OpportunityId = :opptyId and Converted_to_Asset__c = false];
Asset[] ast = new Asset[]{};
Asset a = new Asset();
for(OpportunityLineItem ol: OLI){
a = new Asset();
a.AccountId = o.AccountId;
a.Product2Id = ol.PricebookEntry.Product2Id;
a.Quantity = ol.Quantity;
a.Price = ol.UnitPrice;
a.PurchaseDate = o.CloseDate;
a.Status = 'Purchased';
a.Description = ol.Description;
a.Name = ol.PricebookEntry.Product2.Name;
a.Original_Opportunity__c = opptyID;
a.In_Inventory__c = true;
a.Date_of_Manufacturer__c = ol.DOM__c;
ast.add(a);
ol.Converted_to_Asset__c = true;
}
update OLI;
insert ast;
}
}
}
Hi Kate,
Please change line 26 to mapOfAsset.put(ol.id,a);
All Answers
How is opportunity line items is related to Asset ? Did you create any master detail or lookup relation between these two?
Could you try the modified code ,
trigger CreateAssetonClosedWon on Opportunity (after insert, after update) {
for(Opportunity o: trigger.new){
if(o.isWon == true && o.HasOpportunityLineItem == true && o.type == 'Purchase'){
String opptyId = o.Id;
Map<ID,Asset> mapOfAsset = new Map<ID,Asset>();
OpportunityLineItem[] OLI = [Select UnitPrice, Quantity, PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Description, Converted_to_Asset__c, DOM__c, Asset__c
From OpportunityLineItem
where OpportunityId = :opptyId and Converted_to_Asset__c = false];
Asset[] ast = new Asset[]{};
Asset a = new Asset();
for(OpportunityLineItem ol: OLI){
a = new Asset();
a.AccountId = o.AccountId;
a.Product2Id = ol.PricebookEntry.Product2Id;
a.Quantity = ol.Quantity;
a.Price = ol.UnitPrice;
a.PurchaseDate = o.CloseDate;
a.Status = 'Purchased';
a.Description = ol.Description;
a.Name = ol.PricebookEntry.Product2.Name;
a.Original_Opportunity__c = opptyID;
a.In_Inventory__c = true;
a.Date_of_Manufacturer__c = ol.DOM__c;
ast.add(a);
ol.Converted_to_Asset__c = true;
mapOfAsset.add(ol.id,a);
}
insert ast;
for(OpportunityLineItem ol: OLI)
{
ol.Asset__c =mapOfAsset.get(ol.id).id;
}
update OLI;
}
}
}
Hi Kate,
Please change line 26 to mapOfAsset.put(ol.id,a);
Could you tell how did you fix the error for line number 26 ?
mapOfAsset.put(ol.id,a);
Hope that helps!