You need to sign in to do that
Don't have an account?
Alan Ziegler
Getting a record using a private instance of standard controller in a standard controller extension
All but the last few lines of code below are copied from other discussions/documentation about how to override a save action using a private instance of a standard controller for a selected record in a standard controller extension. I am trying to provide a way to return the selected record back to page. My getLeadRecord below gets message "System.NullPointerException: Attempt to de-reference a null object"
Controller extension code is below. I have simplified select statement to just get ID of first Lead record:
public with sharing class LeadNameLookupConExt {
public LeadNameLookupConExt(ApexPages.StandardController controller){ }
private ApexPages.StandardController ctrl;
public Lead guest {get;set;}
public LeadNameLookupConExt(){
try{
guest = [select ID from Lead limit 1];
//where name = : ApexPages.currentPage().getParameters().get('guest')];
} catch (QueryException e) {
guest = new Lead();
}
ctrl = new ApexPages.StandardController(guest);
}
public PageReference save(){
return ctrl.save();
}
// my added get record code trying to get record object of the specific controller instance
public Lead getLeadRecord(){
return (Lead)ctrl.getRecord();
}
}
my page code is
<apex:page standardController="Lead" extensions="LeadNameLookupConExt">
<form >
<input value="{!LeadRecord.ID}"/>
</form>
</apex:page>
Incidentally, I can retrieve the record if I convert this into a custom controller, but I need a standard controller extension.
Controller extension code is below. I have simplified select statement to just get ID of first Lead record:
public with sharing class LeadNameLookupConExt {
public LeadNameLookupConExt(ApexPages.StandardController controller){ }
private ApexPages.StandardController ctrl;
public Lead guest {get;set;}
public LeadNameLookupConExt(){
try{
guest = [select ID from Lead limit 1];
//where name = : ApexPages.currentPage().getParameters().get('guest')];
} catch (QueryException e) {
guest = new Lead();
}
ctrl = new ApexPages.StandardController(guest);
}
public PageReference save(){
return ctrl.save();
}
// my added get record code trying to get record object of the specific controller instance
public Lead getLeadRecord(){
return (Lead)ctrl.getRecord();
}
}
my page code is
<apex:page standardController="Lead" extensions="LeadNameLookupConExt">
<form >
<input value="{!LeadRecord.ID}"/>
</form>
</apex:page>
Incidentally, I can retrieve the record if I convert this into a custom controller, but I need a standard controller extension.
There are multiple things missing.
1) What is the use case of this code? Are you trying to embed the vf in a page layout. If so the standardController.getRecord() will fetch you the information.
2) Select using Name in where clause has possibility of returning more than 1 value. How do you want to handle this scenario.
3) Passing the Lead name in get parameter and querying the information and displaying the value is not the right use case for an extension. Extension by defenation should extend the capabilities for standard controller.
However if you just want to see how it will work please try the below code. I have tried it in my dev sandbox and it works. Hope this helps.
in URL use
https://c.na30.visual.force.com/apex/LeadView?guest=Norm%20May
Please note that salutation is not part of the Lead Name. To get the exact lead name query the lead name in query editor.
All Answers
You will not need your getLeadrecord method and in your visualforce page instead of {!LeadRecord.Id} use {!guest.Id}
There are multiple things missing.
1) What is the use case of this code? Are you trying to embed the vf in a page layout. If so the standardController.getRecord() will fetch you the information.
2) Select using Name in where clause has possibility of returning more than 1 value. How do you want to handle this scenario.
3) Passing the Lead name in get parameter and querying the information and displaying the value is not the right use case for an extension. Extension by defenation should extend the capabilities for standard controller.
However if you just want to see how it will work please try the below code. I have tried it in my dev sandbox and it works. Hope this helps.
in URL use
https://c.na30.visual.force.com/apex/LeadView?guest=Norm%20May
Please note that salutation is not part of the Lead Name. To get the exact lead name query the lead name in query editor.
The limited documentation I could find on when to use controller extensions versus custom controllers seems to indicate that if I want to use both standard controller methods plus my own custom method, I should use an extension, but the advice seems to be very rule of thumb. I put much more faith in the developer community advice.
Other limited documentation seems to indicate that if I create a custom controller I can still access standard controller methods if I want to redefine all of them within the custom controller but suggests if that is what I am doing, I should just use an extension.
I did not try embedding the "controller" logic in vf page, but I bet your syntax or my custom controller syntax would work there. Limited documentation again suggested that data access should be put in a controller versus vf page as a rule of thumb.
I simplified the example and explanation with regard to making sure I get only one record but I appreciate your reminder that I must handle this.
Again thanks a bunch!
Yes i would agree that you would use extension when you want to leverage the standard controller funcitonality and enhance it with custom code. Since your code is a snippet of your actual code it makes sense to use extension. From my experience in working on force.com platform I would say there is no rule of thumb there are recommendations and best practices but they could vary based on each use case.