function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sam Wilson 16Sam 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;
     }
    }
}

 
 
Best Answer chosen by Sam Wilson 16
LBKLBK
Is Operator__c a lookup field in OpportunityLineItem object?

If yes, you just need to change the following line.

existing line
a.AccountId = ol.Operator__c.Id;
new line
a.Account = ol.Operator__c;

If it is part of Opportunity object, then you need to change the following lines.

existing lines
 
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];





        a.AccountId = ol.Operator__c.Id;
new lines.
 
OpportunityLineItem[] OLI = [Select Opportunity.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];





        a.Account = ol.Opportunity.Operator__c;
Let me know if this helps.

 

All Answers

LBKLBK
Is Operator__c a lookup field in OpportunityLineItem object?

If yes, you just need to change the following line.

existing line
a.AccountId = ol.Operator__c.Id;
new line
a.Account = ol.Operator__c;

If it is part of Opportunity object, then you need to change the following lines.

existing lines
 
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];





        a.AccountId = ol.Operator__c.Id;
new lines.
 
OpportunityLineItem[] OLI = [Select Opportunity.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];





        a.Account = ol.Opportunity.Operator__c;
Let me know if this helps.

 
This was selected as the best answer
Sam Wilson 16Sam Wilson 16
Changing to a.Account = ol.Operator__c; was what I needed. Thank you very much!