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
Ben Merton 15Ben Merton 15 

Confusion on my first trigger

Merry Christmas!

I am trying to write my first trigger, and am really stuck with the method.  I have three custom objects:  Product__c, BOM__c, and BOM_Items__c

Product is the grandparent, BOM is the parent and BOM Items is the child.

I also have a lookup from BOM Items to Product, so effectively the Product is also the parent of BOM items.  It is a strange, incestous relationship.

When I create a BOM Item, I want to trigger the ID of the Product in the BOM field, so that both BOM Items and BOM mention the Product. (ie trigger from the Product__c lookup field in the BOM__c object TO the Product__c lookup field in the BOM_Item__c field.  This doesn't have to happen for ALL records ALL the time, only for the records as and when they are created.

As a novice, my initial thought on the methodology would be something like this:
trigger ProductName on BOM_Item__c (before insert, before update) {
BOM.Product__c ProductIDBOM;
Product__c ProductIDBOMItem;

for (BOM_Item__c trigger : trigger.new)
{
ProductIDBOM=BOM__c.Product__c;
ProductIDBOMItem=ProductIDBOM;
}
}


However, when I read about triggers on these pages, everyone seems to start with having to build a map of the IDs and then selecting from the map to find the ID etc etc.  I can't work out whether this is because they want to update more than one record at a time, or just because this is best practice.  Please help!!

Ben





From what I can see on the boards, the methodology should be:

1.  Build a map
Best Answer chosen by Ben Merton 15
Andy BoettcherAndy Boettcher
Is the "Product" lookup different between the BOM and BOM Item record?  If not, a far easier way to do this declaratively would be to make a Formula Field on the BOM Item that just references the BOM product field.

All Answers

Andy BoettcherAndy Boettcher
Is the "Product" lookup different between the BOM and BOM Item record?  If not, a far easier way to do this declaratively would be to make a Formula Field on the BOM Item that just references the BOM product field.
This was selected as the best answer
Ben Merton 15Ben Merton 15
Ooh this is interesting. Thanks. However, I want the formula to show a hyperlink to the product record AND this has to work in a managed package, so the hyperlink must always link to the correct product regardless of namespace. Is this possible?
Andy BoettcherAndy Boettcher
Yup!  Just wrap the HYPERLINK formula around the text.  You'll want to prefix the formula fields with your namespace - good catch.
Ben Merton 15Ben Merton 15
Boom!  Thanks. But I actually do also want to know the answer to my question anyway as i am trying to actually learn apex. Suppose I did want to write apex. Which would have been the best route as per my question above?