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
sgkmillssgkmills 

Standard Controller is not being initialized

 

I have written a custom controller extension on a custom object and am trying to retrieve the current record/id by using the getRecord and getId methods of the StandardController class but both are coming back null. How is that possible?

part of the controller extension is below. The custom object is called SC__c:

public SC_Controller_Ext (ApexPages.StandardController stdController) {
// this will kickoff you main page
controller = stdController;
system.debug('stdController=' + stdController + ', stdController.getRecord()=' + stdController.getRecord() + ', stdController.getId()=' + stdController.getId());

 

The system.debug shows the getRecord and the getId are null.  The controller extension gets kicked off from an overwritten button on a Standard Page.  The button that is overwritten is the 'new' button of the custom object.  

 

I don't understand how the standardcontroller object can be null because I am viewing the record and then just clicking the edit button, of the custom object.  This button is not overwitten, it is using the default Salesforce action.  Then I click the 'Save and New' button from the 'edit' screen and the controller isn't initialized.

 

Any info/ideas will be greatly appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
sgkmillssgkmills

I figured out a different approach to this problem.  I use the 'retURL' parameter and parse it to figure out the referring page.  Since the 'getRecord method is null, I then query Salesforce using the Id in the retURL and then I am able to set my variables within the controller.  It is one extra query, but now it gives me the parameters I need passed onto the new page after 'Save and New' button is clicked.

 

Note:  I spoke with Salesforce and they explained it is by design that the new page is blank when a user clicks 'Save and New'.  I wanted to prepopulate some fields and that is why I was in this dilema!

 

IMO, it's great when you find the solution for yourself, cause you learn so much more.

All Answers

Shashikant SharmaShashikant Sharma

you must have a parameter id in you page URL only then stdController.getRecord will show you anything otherwise it will remain null. When you click edit do you have id parameter in UR

sgkmillssgkmills

I omitted something by mistake, but edited my initial post:

 

I don't understand how the standardcontroller object can be null because I am viewing the record and then just clicking the edit button, of the custom object.  This button is not overwitten, it is using the default Salesforce action.  Then I click the 'Save and New' button from the 'edit' screen and the controller isn't initialized.

 

To be clear, my steps are as follows:

 

  1. Click on the 'Edit' button of the custom object
  2. Edit the record and then click 'Save and New' button

The custom controller extension gets kicked off after the validation/workflow rules are checked and the record has been saved, but the standardcontroller object isn't initialized.  Since, I am not overwriting the 'Edit' button, I cannot supply the 'id' field as a parameter.



Edwin VijayEdwin Vijay

Do you mean to say that you have overridden the "New" button and not the "Edit" button?

 

And, when you click on "New" everything works fine and when you click on "Save and New" it does not work as expected?

 

I doubt that the getrecord and getId methods would fail because you dont have a record actually, when the record is not actually saved i guess you wouldn't be able to get the Id

sgkmillssgkmills

Yes, I have overwitten the 'New' button and that works fine, but when I click on the 'Save and New' button it doesn't work as expected. I understand that the id will be null because it is a new record, but when I click the 'New' button, the getRecord gives me back the Opportunity Id.  I inserted the code that retrieves the Opportunity Id below:

 

 

       this.SC = (SC__c)stdController.getRecord();
        cOpportunityId = this.SC.Opportunity__c;


 

When the 'Save and New' button is clicked, the cOpportunityId field above is null because the stdController.getRecord is not initialized.

sgkmillssgkmills

I figured out a different approach to this problem.  I use the 'retURL' parameter and parse it to figure out the referring page.  Since the 'getRecord method is null, I then query Salesforce using the Id in the retURL and then I am able to set my variables within the controller.  It is one extra query, but now it gives me the parameters I need passed onto the new page after 'Save and New' button is clicked.

 

Note:  I spoke with Salesforce and they explained it is by design that the new page is blank when a user clicks 'Save and New'.  I wanted to prepopulate some fields and that is why I was in this dilema!

 

IMO, it's great when you find the solution for yourself, cause you learn so much more.

This was selected as the best answer