function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
qvisionqvision 

Update Standard Controller Field from Extension

I am driving crazy...I created a controller extension for a custom object and a page that overrides the Edit button...except one feature the page is working as expected:

I have a picklist field Status__c in the custom object that is not displayed on VF page, but should be updated when the user saves. Basically, when the record is created it has status 'New', once updated it should be 'Resolved'.

Now, it saves all fields that are on the page or any additional methods. But how can I override the set method for Status__c?

I have a save method, that does update a specific account...but I cannot get the Status__c field set to resolved.

Do you have any idea?

public class MyExtension {

private final MyCustomObject__c myObj;
private Account account;

private final ApexPages.standardController theController;

public MyExtension (ApexPages.StandardController theController) {
MyCustomObject__c myObj = (MyCustomObject__c) theController.getRecord();
this.theController = theController;

this.myObj = [Select Id, TestField__c, Status__c from MyCustomObject__c where Id = :myObj.Id];

}

...

public PageReference save () {

update this.account; // works fine

// This one does not work
this.myObj.Status__c = 'Resolved';
update this.myObj;

// Method does not exist...
// this.myObj.setStatus__c('Resolved');

return theController.save();
}

}
jwetzlerjwetzler
You're getting myObj by asking for it from the controller via the getRecord() method, and then you're overwriting it two lines down by querying for it again.

I think if you get rid of this line:
this.myObj = [Select Id, TestField__c, Status__c from MyCustomObject__c where Id = :myObj.Id];

and also this line from your save method:
update this.myObj;

it should work.

The update is not necessary, because the update happens in standard controller save method.  As long as you get the record directly from the standardController and edit its fields directly, the standardController should take care of the rest for you.
qvisionqvision
Jill,

thanks for your help. Reason why I queried for the record after the getRecord call, is that I read and update fields that are not displayed on the page itself. And if I don't show them, I understood, I need to query for them...

Now, it put them into hidden fields and it works with getRecord and to set the field values...without a specific update call...

Is there any other solutions than posting them into hidden fields?

Best regards,
jwetzlerjwetzler
I think we eventually want to have a way for you to add a reference to the standard controller from inside your extension, but for now the way to do it is as a hidden or non-rendered field on your page, so you did the right thing.
qvisionqvision
All I need to know ;-) Many thanks