You need to sign in to do that
Don't have an account?
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.
There's a good starting point at: http://sfdcdownunder.blogspot.com/2009/02/how-to-write-trigger-in-apex.html
You want to create an After Insert Trigger on OpportunityLineItem, the trigger should contain something like:
Thank you Bob and Enth! You've given me a great start on this!
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?
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.
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!
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.