You need to sign in to do that
Don't have an account?
willard
Merge SObjects with the same Id, but different fields populated
Is there an easy way to merge two SObjects? Right now I have one method that spits out some opportunities with certain values filled in, i.e., CloseDate, Stage, etc.. I have another method that spits out some opportunities with other values filled in, ie. Amount, UpgradeAmount, etc.
Is there an easy way to combine these two SObjects into one with the new SObject having all the fields populated?
Something like a copyFieldsTo method:
Id oppId = '<someid>'; Opportunity opp1 = new Opportunity(Id = oppId, closedate = today, ...); Opportunity opp2 = new Opportunity(Id = oppId, amount = 100, ...); opp1.copyFieldsTo(opp2); // opp2 will now have amount AND closedate populated
Out of the box, no, but you can do this:
Edit: updated code.
Thanks for the start sfdcfox.
First, after some tweaking, I was able to get the merged sobject, but it was setting all the fields that weren't supposed to be set to null, which is definitely not good. Here's the final solution in case anyone needs it: