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
JillDJillD 

Please Help Me with Triggers

We have a custom Inventory table that is updated nightly. I have created a link table (Inventory Product) that connects Inventory to Product and Opportunity.

 

What I need is a trigger that will automatically create an Inventory Product record every time that a line item is added to an opportunity.

 

The trouble is, I do not understand triggers at all. I can't figure out how to create one that will automatically fill in the  Opportunity__c, Product__c and Inventory__c fields when a line item is created. Can somebody please help me? I have been reading sample triggers, but I just don't understand how they are constructed.

 

Thank you in advance,

 

J.

bob_buzzardbob_buzzard
EnthEnth

You want to create an After Insert Trigger on OpportunityLineItem, the trigger should contain something like:

 

List<Inventory_Product__c> ipList = new List<Inventory_Product__c>();

for (OpportunityLineItem oli : trigger.new) {
   Inventory_Product__c invProd = new Inventory_Product__c();
   invProd.OpportunityLineItem__c = oli.id;
   // Products are linked to OLI via PriceBookEntry records
   // It would be much easier to link to the PBE but you need to get
   invProd.Product__c = oli.PricebookEntry.Product2Id; 

   // You may have to retrieve the set of pricebookentrys first as the above
   // may not be populated during the trigger execution.

   ipList.add(invProd);
}

insert ipList;

 

 

JillDJillD

Thank you Bob and Enth! You've given me a great start on this!

JillDJillD

Hi Enth!

 

I'm stuck again. You have:

 

"invProd.OpportunityLineItem__c = oli.id;"

 

But when I create a lookup field, I am not given the option to link to OpportunityLineItem. Can you tell me how to create a link to the OpportunityLineItem?

 

 

EnthEnth

Hmm, I hadn't thought of that. OK, the options are to either store the relationship in a normal field (i.e. not a Lookup but a simple text field - this will only be useful in Apex) or link to the Opportunity instead - but you won't know which line item it relates to. You could do both!

 

In terms of the Apex, you can get the OpportunityId from the oli record.

 

JillDJillD

Hi!

 

Thanks for answering and for the tips. I can now insert the opportunity ID like:

 

invProd.Opportunity__c = oli.OpportunityID;

 

I don't understand what you mean when you talk about storing the relationship in a normal field. I confess, I am ignorant about Apex programming in general and triggers in particular.

 

Would I need a new field for that? Something like: InvProd.NCustField__c = oli.id? 

 

If so, how do I use that in the trigger?

 

Thanks in advance for your kind assistance!

EnthEnth

That's right, but the only reason to do that is if you want to use the information in Apex for some other functionality. For example, if you need to re-synchronise between the objects if one of them changes you can use InvProd.NCustField__c to find the right line item.

 

It would also help with debugging or troubleshooting operational issues - but it's totally optional.