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
Swapnil PatneSwapnil Patne 

Trigger to auto populate lookup field on opportunity product lineitem

Hi,

I hope someone can help me with this-

Auto populate a custom lookup field "Account_Name__C " on opportunity product lineitem with an "Account Name" from the opportunity object or possibliy from Account Object itself, needs to happen when a user selects the opportunity product and saves the record.

Please can someone advise?

Thanks,
Swapnil
Best Answer chosen by Swapnil Patne
harsha__charsha__c
Hoping that your "Account_name__c" is a look-up field to Account on OpportunityProduct. If yes, your trigger logic would go something like this:

trigger updateOLI on OpportunityLineItem (after insert) {
      List<OpportunityLineItem> OpportunitiesToUpdate = new List<OpportunityLineItem>();
      for(OpportunityLineItem OLI : [Select Id, Opportunity.Account.Id From OpportunityLineItem Where Id IN : trigger.newMap.keyset()]) {
            OLI.Account_Name__c = OLI.Opportunity.Account.Id;
             OpportunitiesToUpdate.add(OLI);            
       }   
       if(!OpportunitiesToUpdate.isEmpty())
                update OpportunitiesToUpdate;


If the "Account_Name__c" field is just a Text field and you just wanted to store the Account Name of the Opportunity in there, then better to Opt for a formula field instead the trigger as @gm_sfdc_powerde suggested.

Regards,
- Harsha

All Answers

gm_sfdc_powerdegm_sfdc_powerde
You could certainly write a trigger on OpportunityProduct for this.  But if this is a permanent reference to the parent account, did you consider creating a formula field instead?  You could create a formula field in Opportunity product that points to the parent account name (with something like Opportunity.Account.Name).  Much better from data integrity and maintenance perspective.
harsha__charsha__c
Hoping that your "Account_name__c" is a look-up field to Account on OpportunityProduct. If yes, your trigger logic would go something like this:

trigger updateOLI on OpportunityLineItem (after insert) {
      List<OpportunityLineItem> OpportunitiesToUpdate = new List<OpportunityLineItem>();
      for(OpportunityLineItem OLI : [Select Id, Opportunity.Account.Id From OpportunityLineItem Where Id IN : trigger.newMap.keyset()]) {
            OLI.Account_Name__c = OLI.Opportunity.Account.Id;
             OpportunitiesToUpdate.add(OLI);            
       }   
       if(!OpportunitiesToUpdate.isEmpty())
                update OpportunitiesToUpdate;


If the "Account_Name__c" field is just a Text field and you just wanted to store the Account Name of the Opportunity in there, then better to Opt for a formula field instead the trigger as @gm_sfdc_powerde suggested.

Regards,
- Harsha
This was selected as the best answer
Swapnil PatneSwapnil Patne
@gm_sfdc_powerde thank you for your suggestion, however Account_Name is a look up field and I need to have the look up relationship built so all products can be visible on the account level. 

@Harsha- thank you for your response and the code, I think this is what will work for me,  I'll test it. Thanks a ton once again. 
Swapnil PatneSwapnil Patne
Hey Harsha- is it possible to add a criteria i.e. populate the account name only if stage = "closed won" ?
Swapnil PatneSwapnil Patne
can we twik this trigger in such a way that when and only if an opportunity is closed won it auto populates the Account Name on opp product line?
 
harsha__charsha__c
No, this trigger can not do it. This needs a separate trigger on Opportunity as we need to fire your logic when Opportunity Stage is updated to "Closed-Won"

Regards,
- Harsha
Swapnil PatneSwapnil Patne
Hey Mate, any thoughts what that code would be.. I am not a developer :(