1

Similar to a question i asked earlier I need to reset an existing object back to all the default field values in apex.

I tried

object.clear()

but that sets everything to null including the Id which isnt good.

I need to it set back to the default values on the field?

hoping for something like this?

Map<String, Schema.SobjectField> MeetingFields = Schema.SObjectType.Meeting__c.fields.getMap();
Schema.DescribeSObjectResult meetingSchema = Meeting__c.sObjectType.getDescribe();
Map<String, Schema.SObjectField> meetingMap = meetingSchema.fields.getmap();

for(String key : MeetingFields.keySet()) {
     Schema.DescribeFieldResult fieldDescribe  = meetingMap.get(key).getDescribe();
     meeting.put(key,fieldDescribe.getDefaultValue());

}

But for some reason the "getDefaultValue()" is just returning null for everything and it fails on checkboxes (expecting false) and doesn't seem to return the real default value for picklists?

1

You can use sObjectType.newSObject(null, true) to create a prototype meeting and copy the field values from that.

// You may need to pass the record type in here for picklists to get the right value
Meeting__c tempMeeting = (Meeting__c)Meeting__c.sObjectType.newSObject(null, true);

Map<String, Schema.SobjectField> meetingFields =   
    Schema.SObjectType.Meeting__c.fields.getMap();

for(String key : meetingFields.keySet()) {
   // There are other non-writable fields that you will need to skip here
   // E.g. Required fields with no default value
   // Watch out for any unique fields.
   if(key == 'Id') { continue; }
   meeting.put(key, tempMeeting.get(key));
}

How about inserting a new blank Meeting__c record, then retrieving it after insertion and copying the default values back to the current record for each field. Finally, delete the temp record that you inserted.

It isn't very elegant, but is should work in a number of contexts.

Meeting__c tempMeeting = new Meeting__c();
// You may need to populate a minimal set of required fields here.
insert tempMeeting;

// You will need to add the other required fields here. 
// Consider using dynamic SOQL with the meetindFields below.
tempMeeting = [Select Id from Meeting__c where Id = :tempMeeting.Id];

Map<String, Schema.SobjectField> meetingFields =   
    Schema.SObjectType.Meeting__c.fields.getMap();

for(String key : meetingFields.keySet()) {
   // There are other non-writable fields that you will need to skip here
   if(key == 'Id') { continue; }
   meeting.put(key, tempMeeting.get(key));
}

delete tempMeeting;

If the field is a picklist you can get the default value by using Schema.DescribeFieldResult.getPicklistValues(); and then looping through looking for the PicklistEntry where .isDefaultValue() is true.

See also: Ideas - Fix field describe getDefaultValue() for checkboxes and picklists

  • hmm either of these should work, just seems like a lot more roundabout than i was hoping for. Do you know if getDefaultValue() is just a known bug or is it supposed to do something else? seems really strange to have a function like that if its only ever going to return null? – Keith Mancuso Jun 22 '14 at 13:54
  • @KeithMancuso The linked idea implies it hasn't worked for checkboxes and picklists for some time (4+ years). Picklists you can work around and depend on the record type. I don't know what the issue with checkboxes is. – Daniel Ballinger Jun 22 '14 at 20:02

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.