2

I'm using Apex to update the value of a field in an existing record for a particular object. I got it to work by changing the existing record and upsert it. The catch is I don't want to overwrite the existing record, but instead want to create a new record as a way of preserving history. So, what I did was:

// Get the latest record
String query = 'SELECT ';
for(Schema.FieldSetMember f : SObjectType.LicenseKey__c.FieldSets.Edit_Fields.getFields()) {
   query += f.getFieldPath() + ', ';
}
query += 'Name FROM objName__c WHERE Id=\'' + objID + '\'' + 'ORDER BY LastModifiedDate DESC LIMIT 1';
origRec = Database.query(query);

...
// Create new record & update fields
objName__c newRec;
newRec = origRec.clone(false,true,true,true);
newRec.Id = null;
newRec.updatedField__c = true;
insert newRec;

This works excepts for 1 major and 1 minor problem:

Major problem: Fields with picklists are not copied from the original record. I set the 2nd parameter to true hoping a deepClone would preserve the value but apparently not.

Minor problem: Although I used 'false' for the first parameter, I still had to set Id=null.

Based on the Sobject clone() instance method documentation I think I understood the API correctly but it seems not.

Help is appreciated, Scott

  • I believe (based on a quick experiment) that Clone() will only clone the fields it has in memory, ie, the fields you queried into origRec, which you don't show in your sample code. Also, you don't need to null out the record Id, but the new one won't be created until you Insert the clone. – Thomas Taylor Jan 3 '14 at 2:17
  • If you want to look at ways of preserving history, have you looked at the track changes feature? It only works on a limited number of fields per object, but works great for tracking key fields. – Daniel Hoechst Jan 3 '14 at 2:24
  • @ThomasTaylor - I updated the code sample to clarify how origRec got created. I hadn't considered the in-memory issue but alas the picklist fields are set correctly after the Database.query() is done. – Scott Jan 3 '14 at 2:35
  • @DanielHoechst - Unfortunately, there's a bunch of other business logic that needs to happen so manually manipulating the record & tracking changes doesn't quite suffice... – Scott Jan 3 '14 at 2:39
  • I just tested in my org and I didn't have either of your problems. What version API is your class saved as? – Daniel Hoechst Jan 3 '14 at 5:33
0

My bad. Turns out the clone works correctly. Instead, the insert mucked up because the problematic fields use default values. I set the fields to null, but the default values are not. So, upon insert, the default values were written in. If I set the field to a non-null and do the insert the default value does not overwrite them. I guess a work around is do the insert, change the fields of interest to null and do an upsert. Smells like a hack to me but hopefully I can then move on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.