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 

Auto generate opportunity line items

Has anyone auto generated opportunity line items from a custom field in Opportunity.


Ex. Custom Field in Opportunity is linearfeet__c ; want to automatically insert 5 different product line items that are standard and use linearfeet__c in the quantity calculation?


I am working off of as a starting point but lost.


Any help would greatly be appreciated.




Are you having problems with a particular part of this, or just general trouble getting started?
  1. To be honest BOB, just getting started, APEX is all new to me. I think I misunderstood how to set up a trigger. Do I make an apex class that calls a trigger?


What I tried to do so far is;


trigger OpportunityPLIStandard Add on Opportunity (after update) {

// ChuteLinearFeet__c is defaulted to 0
      if (Opportuntiy.ChuteLinearFeet=0) {

// add an opportunity line item
        OpportunityLineItem oli = new OpportunityLineItem();
        oli.OpportunityId = opp.Id;
        oli.Quantity = Opportunity.ChuteLinearFeet__C;
        oli.PricebookEntryId = [Select p.Id From PricebookEntry p Where PricebookEntry.Product2.Name = '24" Standard Chute' And IsActive = true limit 1].Id;
        insert oli;


An apex trigger is much like a database trigger - its a section of code that exists in isolation and gets invoked in response to something happening to one or more records.


In your case, the trigger will only be executed when an opportunity is updated, rather than when it is inserted - is that what you are looking for?


Also, this trigger will have an affect each time the opportunity is updated, so you'd add a product each time.  You might want to check if there are any products associated with the opportunity before adding one, or setting a flag on the record to indicate that the autogenerated product has been added.


Did you ever find a solution to this. I'd love to know how you accomplished it. Thanks in advance for your response.