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
Paulo CastroPaulo Castro 

Strange behavior using Apexpages.Standardcontroller.getRecord

Hi guys,

 

 I have this Visualforce Page using an extension controller, and in it's constructor I get the standard controller instance to access the current record, as bellow:

 

public class MyControllerExtension{

    private final Custom_Object__c customRecord;    
    private final Another_Object anotherObject;
    
private final Apexpages.Standardcontroller stdController; public MyControllerExtension(Apexpages.Standardcontroller sc){ stdController = sc; customRecord= (Custom_Object__c)stdController.getRecord(); anotherObject = new Another_Object(customRecord); } }

 

 I have to pass the customRecord instance to this anotherObject, since anotherObject will refer (lookup) it.

 

 So far, so good.

 But I'm using this Visualforce page to create a new record of this object Custom_Object__c. So, when I call stdController.getRecord() it returns a Custom_Object__c instance with Id property = null. And it is ok, since at this time it doesn't have a record yet...

 Then I  created a custom Save() method. this way I could save customRecord and anotherObject when my user click on save button.

 

public class MyControllerExtension{ private final Custom_Object__c customRecord; private final Another_Object anotherObject; private final Apexpages.Standardcontroller stdController; public MyControllerExtension(Apexpages.Standardcontroller sc){ stdController = sc; customRecord = (Custom_Object__c)stdController.getRecord(); anotherObject = new Another_Object(customRecord); } public PageReference save(){ PageReference result = stdController.save(); anotherObject.Save(); return result; } }

 

 

 After the first line execution of this method (PageReference result = stdController.save()) I thought that the customRecord would have his Id updated with some value. But it didn't happen! So, when I called anotherObject.Save(); it didn't created the relationship, because anotherObject have a reference to customRecord with Id = null, even after called the save() method.

 

 Then I tried (using debug) to get another stdController.getRecord() reference after call save() method. And then it worked !! The Custom_Object__c returned had his Id value.

 

 So the only thing I can imagine is when I call save, it creates another Custom_Object__c instance inside the controller class... Am I right? What I'm missing here?

 

 Thanks in advance!

 

 Best regards

 

 PH

Ron HessRon Hess
does it work if customRecord is not final?
Paulo CastroPaulo Castro
Unfortunately not. I tried it just after send this message, but still doesn't work...
Paulo CastroPaulo Castro

Now I'm confused...


I inserted this few lines bellow, to try to discover what is happening:

 

 

 

public PageReference save(){

PageReference result = stdController.save();

System.debug(Logginglevel.DEBUG, '---------- isEqual: ' + (customRecord == stdController.getRecord()));

System.debug(Logginglevel.DEBUG, '---------- customRecord.id: ' + customRecord.Id);

System.debug(Logginglevel.DEBUG, '---------- customRecord.Note_c: ' + customRecord.Note__c);

System.debug(Logginglevel.DEBUG, '---------- stdcontroller.id: ' + ((Custom_Object__c)stdController.getRecord()).Id);

anotherObject.Save();

return result;
}

 

 and got these results:

 

 isEqual: false (so, we really have 2 differents objects?)

 

 customRecord.id: null (as I already had found)

 

 customRecord.Note__c: my note test (Note__c is a text field, and I filled with random text)

 

 stdcontroller.id: a valid Id value

 

 

 So, what I didn't understand is how customRecord object reference could have the Note__c field filled correctly and the Id field not updated after save method? The controller use one object reference to bind the fields and another to insert the new record?

 

 

 

 

Message Edited by Paulo Castro on 06-11-2009 09:52 PM
Ron HessRon Hess

take a look at this wizard, it shows how to save two different objects in one save method

 

http://www.salesforce.com/us/developer/docs/pages/Content/pages_quick_start_wizard.htm

 

you deal with the local class variables mostly.

 

your save would look something like this :

 

public PageReference save(){

insert customRecord;

anotherObject.customrecord__c = customRecord.id;

insert anotherObject; // or anotherObject.save() return stdController.cancel(); }

 

 

Paulo CastroPaulo Castro

Hi Ron,

 

first of all thank you very much for your support.

 

I did something like it you suggested, but passing the standard controller (and not the record) reference. This way my anotherObject can retrieve the customRecord reference after I save it.

 

 Anyway, it doesn't make sense to me... In my opnion it is a kind of bug, since this standard controller should manage the same object record instance, before and after I call the save() method.

 

 Thank you again..

 

 Best regards

pmozzpmozz

Hi Ron, I built a wizard for a custom object (Invoice) much like the Opportunity wizard shown in the link example however, I cannot figure out for the life of me how I go about creating a button that calls that wizard.  After reading through all I could find, I am really confused!  Can you please help me with what I need to create to call up the wizard?

 

Thanks much - I am very much a beginner in VF and Apex, so please keep it simple.