+ Start a Discussion
pjbarbats22pjbarbats22 

Trigger that automatically adds opp products based on other Products chosen

We want to automatically add Maintenance Products to our Opportunity Products when a License is chosen.  We dont want sales to have to click on both products, but always have the maintenance product get inherited from the license product.  I am told that apex trigger ios only way for this to happen.

Jake GmerekJake Gmerek

Yes you are correct. You would need a trigger on the opportunity product that checked the type of product (however you assign in). If it is a License the trigger would have to create a new product and insert it.

 

Here is generally what you want to do

 

trigger myTrigger on OpportunityLineItem(before insert)
{

List<OpportunityLineItem> ItemsToInsert = new List<OpportunityLineItem>();

for (OpportunityLineItem oli: trigger.new)
{
   if (oli.type = 'License')
   {
      ItemsToInsert.add(new OpportunityLineItem(unitprice = 1.00, quantity = 1, pricebookid = {some id you have to find for the specific product}, opportunityid = oli.opportunityid));
   }
}

insert ItemsToInsert;
}

 This is just the basic structure and I suspect that the finished product would be way more complicated because I am sure that you have multiple products with a 'license' type and may have to pick a 'maintenance' product based on some criteria, but this should get you started.