i have trigger on custom child object seasonAddress and it has Masterdetail relationship with contact object. if current season address record is marked as invalid, parent contact record is get updated with the address fields of earlier valid address field values of contact. So i need get the old version of contact records in 'before update trigger' on seasonaddress object.. how can we achive this please help me..
-
How are you marking the address as invalid, programmatically or manually?– sfdcJamesonApr 11, 2016 at 21:01
-
manually.. we have a checkbox in seasonaddress object.. if user select that checkbox, address become invalid then system automatically updates the address fileds in contact with its previous addressfileds(Assuming previous address fileds are valid)..– user30563Apr 12, 2016 at 1:24
-
That's what I assumed. See my answer below.– sfdcJamesonApr 12, 2016 at 4:31
2 Answers
Assuming you're manually determining whether the address is valid or not, you'd need to fire a trigger based on a "invalid address" flag on the Contact record and access the Contact__Histories object to access the previous value of that field.
This is of course assuming that you are tracking the history of that field on the Contact object.
So, to Summarize, you need to make sure you're tracking the field history of the address field on the Contact object and then fire your trigger on the "Address is invalid" action and query Contact__Histories and loop over that collection to get the most recent address value and then update your Contact record with that address.
In the update context, you can access the
trigger.old
or
trigger.oldMap
system variables to get the fields from the object before the update occurs. An example of using this is
for (SObject s: trigger.new){
SObject oldS = trigger.oldMap.get(s.id);
//do stuff
}
After getting the old object, you can get the Contact that matches this object and set the field to that old value.
-
i have trigger on child object season address.. not contact. i hope trigger.old provides old version record which it belongs to.. Apr 12, 2016 at 1:25
-
No, it doesn't. Trigger.old just contains the value before the change was made during the act of changing. The old value does not exist after the trigger action is complete, it's a temporary variable. Apr 12, 2016 at 5:27