You need to sign in to do that
Don't have an account?
Sam Wilson 16
Apex trigger to feed custom account lookup from Opportunity Product to Assets. Invalid foreign key relationship error.
Hi,
We are using the Opportunity Products to Assets app (https://appexchange.salesforce.com/listingDetail?listingId=a0N30000001yKgpEAE) to create assets from opportunity products on closed/won opportunities. I am looking to amend the Apex Trigger component that passes the Account ID from the Opportunity to the Asset.
Our business is a broker, so we have 2 account record types that are attached to an opportunity. The customer account owns the opportunity but we then attach a partner/supplier account to the opportunity product via a custom lookup field. The above app passes the account that owns the opportunity ( the customer in our case) to the asset. We instead would like the partner account ID from the opportunity product (named Operator__c in our org) to be passed over as the owner of the asset.
Below is the standard component from the app with my amendments in bold. I have amended the Account.Id = portion of the below component to reference the opportunity line item Operator__c field but it is not working. I receive the following error –
Error: Compile Error: Invalid foreign key relationship: OpportunityLineItem.Operator__c
Any help would be much appreciated!
trigger CreateAssetonClosedWon on Opportunity (after insert, after update) {
for(Opportunity o: trigger.new){
if(o.isWon == true && o.HasOpportunityLineItem == true){
String opptyId = o.Id;
OpportunityLineItem[] OLI = [Select Operator__c, UnitPrice, Quantity, PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Description, Converted_to_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 = ol.Operator__c.Id;
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;
ast.add(a);
ol.Converted_to_Asset__c = true;
}
update OLI;
insert ast;
}
}
}
We are using the Opportunity Products to Assets app (https://appexchange.salesforce.com/listingDetail?listingId=a0N30000001yKgpEAE) to create assets from opportunity products on closed/won opportunities. I am looking to amend the Apex Trigger component that passes the Account ID from the Opportunity to the Asset.
Our business is a broker, so we have 2 account record types that are attached to an opportunity. The customer account owns the opportunity but we then attach a partner/supplier account to the opportunity product via a custom lookup field. The above app passes the account that owns the opportunity ( the customer in our case) to the asset. We instead would like the partner account ID from the opportunity product (named Operator__c in our org) to be passed over as the owner of the asset.
Below is the standard component from the app with my amendments in bold. I have amended the Account.Id = portion of the below component to reference the opportunity line item Operator__c field but it is not working. I receive the following error –
Error: Compile Error: Invalid foreign key relationship: OpportunityLineItem.Operator__c
Any help would be much appreciated!
trigger CreateAssetonClosedWon on Opportunity (after insert, after update) {
for(Opportunity o: trigger.new){
if(o.isWon == true && o.HasOpportunityLineItem == true){
String opptyId = o.Id;
OpportunityLineItem[] OLI = [Select Operator__c, UnitPrice, Quantity, PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Description, Converted_to_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 = ol.Operator__c.Id;
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;
ast.add(a);
ol.Converted_to_Asset__c = true;
}
update OLI;
insert ast;
}
}
}
If yes, you just need to change the following line.
existing line new line
If it is part of Opportunity object, then you need to change the following lines.
existing lines
new lines.
Let me know if this helps.
All Answers
If yes, you just need to change the following line.
existing line new line
If it is part of Opportunity object, then you need to change the following lines.
existing lines
new lines.
Let me know if this helps.