Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Updating only changed fields in a standard controller
public class UpdateChangedFieldsController {
SObject oldRecord, currentRecord;
public UpdateChangedFieldsController(ApexPages.StandardController controller) {
oldRecord = controller.getRecord().clone();
currentRecord = controller.getRecord();
public PageReference saveChanges() {
SObject newClone = currentRecord.getSObjectType().newSObject(currentRecord.Id);
Map<String, Object>
oldValues = oldRecord.getPopulatedFieldsAsMap(),
newValues = currentRecord.getPopulatedFieldsAsMap();
for(String key: newValues.keySet()) {
if(newValues.get(key) != oldValues.get(key)) {
newClone.put(key, newValues.get(key));
try {
upsert newClone;
return new ApexPages.StandardController(newClone).view();
} catch(Exception e) {
return null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment