You need to sign in to do that
Don't have an account?
Updating records on cousin custom object
We have an app that is made up of almost entirely custom objects. Creating a smoothe workflow for our users is vital for this app. I'm new to apex, but familiar with basic code writting.
Here are the relationships between the objects: (--< = 1 to many, M/D = Master/Detail)
Object_a --< Object_b --M/D--< Object_c
Object_a --M/D--< Object_d
When a user populates a date field on Object_c and saves, we'd like a date field on all related records on Object_d (i.e. all Object_d's records for that specific Object_a record) to be updated with the same value.
Any help is appreciated.
So if I'm reading your schema right:
D looks up to A with a Master Detail
C looks up to B with a master detail
B loops up to A with a lookup
So to write a trigger to do this AND make sure it's batchable - because you could easily hit limits doing this I would do the following:
1) Create a formula field on object C that references object A's id (example: ObjectAId__c).
The formula would be something like: "CASESAFEID(ObjectB__r.ObjectA__c)"
This will minimize soql queries and make maintenace easier
2) create a batchable trigger as follows:
That should do it
PS: On a side note: when you are using multiple MD relationships and are updating cross relationship like this, you will notice that updates will be called multiple times. This is just how MD relationships work (especially when there are roll up fields involved) so it is EXTREMELY important to be very EXPLICIT about your conditions where the updates on the related object occurr - otherwise you will hit stack depth exceptions
All Answers
So if I'm reading your schema right:
D looks up to A with a Master Detail
C looks up to B with a master detail
B loops up to A with a lookup
So to write a trigger to do this AND make sure it's batchable - because you could easily hit limits doing this I would do the following:
1) Create a formula field on object C that references object A's id (example: ObjectAId__c).
The formula would be something like: "CASESAFEID(ObjectB__r.ObjectA__c)"
This will minimize soql queries and make maintenace easier
2) create a batchable trigger as follows:
That should do it
PS: On a side note: when you are using multiple MD relationships and are updating cross relationship like this, you will notice that updates will be called multiple times. This is just how MD relationships work (especially when there are roll up fields involved) so it is EXTREMELY important to be very EXPLICIT about your conditions where the updates on the related object occurr - otherwise you will hit stack depth exceptions
I was able to get this to work. Your solution works perfectly given how I described the issue.
Unfortunately for me, I need this to work with both the After Insert & After Update. trigger.oldMap does not work with After Insert. Is there a way around this?
Thanks again,
-Bryan
You can do a check against trigger.isInsert
Try that, should work