Scenario:
When an Opportunity is closed/won, we are creating an Order record with the corresponding OrderItem records based on the OpportunityLineItems of the Opportunity. When the OrderItems are created, we want to trigger Assets to be created on the Account. We currently have an integration with NetSuite where we send the Opportunity & OpportunityLineItem data over to NetSuite, they create a NetSuite Sales Order, and then communicates back to Salesforce to create the Salesforce Order record with the OrderItems as well.
Trigger Code:
trigger OrderLineItem_AI_TRG on OrderItem (before insert, before update, after insert) {
if(Trigger.isBefore){
for(OrderItem lineItemObj:Trigger.new){
String oppId = [Select OpportunityId From Order Where Id = :lineItemObj.OrderId].OpportunityId;
for(OpportunityLineItem oppItem: [Select Id, Discount, League_LP_new__c, Per_Item_Fee__c, Upfront__c, Term__c, Fee__c, Registration_Flat_Fee__c, Size__c, Is_Flat_Fee__c, cc_Rate__c, CC_Price__c, BSS_Price__c, Description From OpportunityLineItem Where Product2Id = :lineItemObj.Product2Id and OpportunityId = :oppId and Opportunity.StageName = 'Closed Won' Order By CreatedDate DESC limit 1]){
lineItemObj.Discount__c = oppItem.Discount != null ? oppItem.Discount : null;
lineItemObj.League_LP__c = oppItem.League_LP_new__c;
lineItemObj.Per_Item_Fee__c = oppItem.Per_Item_Fee__c;
lineItemObj.Upfront__c = oppItem.Upfront__c;
lineItemObj.Term__c = oppItem.Term__c;
lineItemObj.Fee__c = oppItem.Fee__c;
lineItemObj.Registration_Flat_Fee__c = oppItem.Registration_Flat_Fee__c;
lineItemObj.Size__c = oppItem.Size__c;
lineItemObj.Is_Flat_Fee__c = oppItem.Is_Flat_Fee__c;
lineItemObj.CC_Rate__c = oppItem.cc_Rate__c;
lineItemObj.CC_Price__c = oppItem.CC_Price__c;
lineItemObj.BSS_Price__c = oppItem.BSS_Price__c;
lineItemObj.Description = oppItem.Description;
}
}
}
if(Trigger.isInsert && Trigger.isAfter){
for(OrderItem lineItemObj:Trigger.new){
if(Recursivehandler.runOnce()){
String orderObjId = lineItemObj.OrderId;
OrderItem[] OLI = [Select Order.Account.Id, Product2Id, Product2.Name, Quantity, UnitPrice, Order.Opportunity.CloseDate, Order.Opportunity.Contract_End_Date__c, Discount__c, League_LP__c, Per_Item_Fee__c, Upfront__c, Term__c, Fee__c, Registration_Flat_Fee__c, Size__c, Is_Flat_Fee__C, CC_Rate__c, CC_Price__c, BSS_Price__c, Description/*, Converted_to_Asset__c*/ From OrderItem Where OrderId = :orderObjId/* and Converted_to_Asset__c = false*/];
Asset[] ast = new Asset[]{};
Asset a = new Asset();
for(OrderItem oi: OLI){
a = new Asset();
a.AccountId = oi.Order.Account.Id;
a.Product2Id = oi.Product2Id;
a.Name = oi.Product2.Name;
a.Quantity = oi.Quantity;
a.Price__c = oi.UnitPrice;
a.PurchaseDate = oi.Order.Opportunity.CloseDate;
a.UsageEndDate = oi.Order.Opportunity.Contract_End_Date__c;
a.Status = 'Active';
a.Discount__c = oi.Discount__c;
a.League_LP__c = oi.League_LP__c;
a.Per_Item_Fee__c = oi.Per_Item_Fee__c;
a.Upfront__c = oi.Upfront__c;
a.Term__c = oi.Term__c;
a.Fee__c = oi.Fee__c;
a.Registration_Flat_Fee__c = oi.Registration_Flat_Fee__c;
a.Size__c = oi.Size__c;
a.Is_Flat_Fee__c = oi.Is_Flat_Fee__c;
a.CC_Rate__c = oi.CC_Rate__c;
a.CC_Price__c = oi.CC_Price__c;
a.BSS_Price__c = oi.BSS_Price__c;
a.Description = oi.Description;
ast.add(a);
}
insert ast;
}
}
}}
Issue:
The trigger is not finding the Product2Id in the SOQL query within the "isBefore" portion of the code. This results in the OpportunityLineItem field data not being added to the Asset being created within the "isAfter" portion of the code. The Assets are being created, it's just not adding in that OpportunityLineItem data.