+ Start a Discussion
Always ThinkinAlways Thinkin 

Seeking Cross Object Field Update Design Pattern

I'm seeking a design pattern for performing cross-object field updates on multiple, independent and potentially cumulative fields. If either of two (or more) fields on a child object are changed, the corresponding fields on the parent need to be updated. I've tried this a couple ways without finding an efficient design.

 

My first approach fails because the same record is added to a list twice and causes an exception for duplicate IDs. (psuedo code follows)

 

List<object> myList
if (nw.field1 != old.field1){
myList.add (new object(id = nw.ParentId,
field1 = nw.field1));

if (nw.field2 != old.field2){
myList.add (new object(id = nw.ParentId,
field2 = nw.field2));

update myList;

I've tried this with Sets and Maps but the record you get from the Set or Map for the second field value change overwrites the first field value change. The only way I've succeeded is to update all the fields every time even one of them changes which is not desirable and nor particularly scalable.