• Alan Ziegler
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 7
    Replies
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.
I am a newbie so any advice is most welcome as my whole approach may be wrong.  For a new web guest where I don't yet want to require the guest to reveal name and contact information, I have a page that creates a unique ID, saves this as a cookie, and uses web-to-lead to create a new Lead record with this name and his interests from a form multi-select list.  This all seems to work.  When this lead comes back in a new session, this page sees that he has a unique ID in cookie and assigns a new visualforce page with a URL parameter containing the existing unique ID.  The new page attempts to look up his Lead record interests using a controller extension with a select statement.  I have looked through many examples online of how to code the controller (some of which gave me syntax errors) and picked what seems like the simplest example for a template that "compiles".  From the start, the controller query is returning nothing.  So I have simplified the controller query to only try to find the first lead record to return and this also returns nothing.  My controller extension code is:

public class LeadNameLookupTest 
{
    public LeadNameLookupTest(ApexPages.StandardController controller) {}
    
    private Lead guest;
    public LeadNameLookupTest()
    {
         guest = [select ID, name from Lead limit 1];
    }
    public Lead getLeadRecord()
    {
        return guest;
     }
}

The visualforce page below third alert does display the unqiue ID being passed via URL but the earlier alerts display nothing in alert popup.  The  page code is:

<apex:page standardController="Lead" extensions="LeadNameLookupTest">
<script>
alert({!LeadRecord.ID});
alert({!LeadRecord.name});
var guest_name = "{!$CurrentPage.parameters.guest}";
alert(guest_name);
</script>
</apex:page>

Maybe the controller example I used is flawed.  I did see other more complicated templates. I even tried some of these more complicated examples but they would not "compile" or the page script reference to controller result failed.  Or maybe my alerts are a problem.
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.
I am a newbie so any advice is most welcome as my whole approach may be wrong.  For a new web guest where I don't yet want to require the guest to reveal name and contact information, I have a page that creates a unique ID, saves this as a cookie, and uses web-to-lead to create a new Lead record with this name and his interests from a form multi-select list.  This all seems to work.  When this lead comes back in a new session, this page sees that he has a unique ID in cookie and assigns a new visualforce page with a URL parameter containing the existing unique ID.  The new page attempts to look up his Lead record interests using a controller extension with a select statement.  I have looked through many examples online of how to code the controller (some of which gave me syntax errors) and picked what seems like the simplest example for a template that "compiles".  From the start, the controller query is returning nothing.  So I have simplified the controller query to only try to find the first lead record to return and this also returns nothing.  My controller extension code is:

public class LeadNameLookupTest 
{
    public LeadNameLookupTest(ApexPages.StandardController controller) {}
    
    private Lead guest;
    public LeadNameLookupTest()
    {
         guest = [select ID, name from Lead limit 1];
    }
    public Lead getLeadRecord()
    {
        return guest;
     }
}

The visualforce page below third alert does display the unqiue ID being passed via URL but the earlier alerts display nothing in alert popup.  The  page code is:

<apex:page standardController="Lead" extensions="LeadNameLookupTest">
<script>
alert({!LeadRecord.ID});
alert({!LeadRecord.name});
var guest_name = "{!$CurrentPage.parameters.guest}";
alert(guest_name);
</script>
</apex:page>

Maybe the controller example I used is flawed.  I did see other more complicated templates. I even tried some of these more complicated examples but they would not "compile" or the page script reference to controller result failed.  Or maybe my alerts are a problem.