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


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;
        ol.Converted_to_Asset__c = true;
   update OLI; 
   insert ast;

1 Answer 1


If Operator__c holds partner account Id then use,

a.AccountId = ol.Operator__c;

In your code a.AccountId = ol.Operator__c.Id if you try to use relationship then use __r.Id instead of __c.id


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.