+ Start a Discussion
Angela SchloederAngela Schloeder 

How to add controller extension - receiving error

I'm trying to add this extension that a friend wrote, but receive Error: Compile Error: Type already defined: UpdateChangedFieldsController at line 3 column 15

I removed "class", but then received Error: Compile Error: Variable does not exist: record.Id at line 8 column 57

I was told:"You'll just add it as an extension on the page: apex:page standardController="blah" extensions="UpdateChangedFieldsController. And just call the savechanges method to save the changes."

Did I mention I am NOT a developer, I try, but my brain just doesn't seem to understand coding.

I'm on a deadline to make this work, so any help is much appreciated.

UpdateChangedFieldsController.cls

public class UpdateChangedFieldsController {
 SObject oldRecord, currentRecord;
 public class UpdateChangedFieldsController(ApexPages.StandardController controller) {
  oldRecord = controller.getRecord().clone();
  currentRecord = controller.getRecord();
 }
 public PageReference saveChanges() {
  SObject newClone = record.getSObjectType().newSObject(record.Id);
  Map<String, Object> 
   oldValues = oldRecord.getPopulatedFieldsAsMap(),
   newValues = currentRecord.getPopulatedFieldsAsMap();
  for(String key: newValues) {
   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;
  }
 }
}

SandhyaSandhya (Salesforce Developers) 
Hi,

I do not see any declaration for the record here as you have 
oldRecord = controller.getRecord().clone();
  currentRecord = controller.getRecord();

You need to declare something like above for record also based on your requirement.

Hope this helps you!

If this helps you mark it as solved.

Thanks and Regards
Sandhya