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
PaulMacPaulMac 

Approach for automatically inserting records

Hi,

 

I was looking for some opinion for my approach. The goal is: when a user is entering sales order line items, if they enter certain product code, other related products should get entered. For instance, they enter a line for Product A, then ProductB, ProductC, and ProductD would automatically get entered. Normally I would put this code in the data entry form, but I don't think anything like that is available to me. The only thing I could think of was to use an AfterInsert trigger. My original try at this didn't work because the trigger had too many characters. I could probably code it better, but I want to make sure this Trigger approach is the way to go. One immediate downside is that I have to hardcode the particular product codes inside my trigger code. I'd rather do it with some kind of lookup table.

 

Any suggestions would be greatly appreciated.

 

thanks!!

EnthEnth

Let me make sure I understand this. If a user adds Product A to an Opportunity as a Line Item you want to automatically add additional products too? (e.g. If I buy a new automobile, you want to also add in the license plate, tank of gas and warranty)

 

If this is correct, then yes, you want to use an Apex trigger on OpportunityLineItem. Surprised you're getting a too many characters message, this sounds very strange unless you have over 1 million chars of code in your org????

 

Also, you could easily set up a  custom object (AdditionalProducts) to define the rules for this, this would just need to be junction object between Product and PriceBookEntry. Then in your Apex code:

 

 

  1. Create a List of type OpportunityLineItem:  
  2. Iterate through trigger.new - for (OpportunityLineItem oli : trigger.new) { ... } 
  3. Check if the line item has additional products (you'll need to go via PriceBookEntry to Product, then AdditionalProducts, unless your new junction object is related at the PriceBookEntry level) [Non Trivial!]
  4. If there are additional products add them to your List, populating the values, prices etc you want.
  5. At the end of your for loop, check if there are any entries in your local List, if so insert them.
Without writing the full code can't give much more away than the approach, good luck!