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
Nathan ANathan A 

Can't seem to access the Lead Object being saved from inside my Controller Extension

Hi,

 

I'm currently trying to accomplish what should be a simple task.  I have a custom object that stores some configuration parameters in it for a Visualforce page.  

 

The Visualforce page has a Controller Extension defined that loads a custom configuration record based on an ID that's passed in on the URL to the controller.  The Visualforce page creates Leads.  Some of the fields on the Lead record need to be set to values that are found on the configuration record. 

 

When I attempt to set field values on the Lead record that comes in from the StandardController I keep getting the following error: System.NullPointerException: Attempt to de-reference a null object

 

I've tried to set the values on the Lead Record both in the constructor of my Controller Extension like this:

 

public with sharing class LeadControllerExtension {
    ApexPages.StandardController stdController;
    private final Web2LeadSettings__c settings;
    private Lead lead {get; set;}
    
    public LeadControllerExtension(ApexPages.StandardController c) {
        this.stdController = c;
        this.lead = (Lead)this.stdController.getRecord();
        this.lead.Web2LeadDeployment__c = this.settings.Web2LeadDeploymentName__c;
    	this.lead.LeadSource = this.settings.Web2LeadSource__c;
    }
    
    public Web2LeadSettings__c getSettings() {
    	return [select id, Web2LeadSource__c, Web2LeadDeploymentName__c, Web2LeadDeployedURL__c,
        				   Web2LeadForwardTo__c, Web2LeadDisplaySalesforceUI__c 
        				from Web2LeadSettings__c where name = :ApexPages.currentPage().getParameters().get('name')];
    }
}

 

 

and by attempting to override the save() method like this:

 

public with sharing class LeadControllerExtension {
    ApexPages.StandardController stdController;
    private final Web2LeadSettings__c settings;
    private Lead lead {get; set;}
    
    public LeadControllerExtension(ApexPages.StandardController c) {
        this.stdController = c;
        
    }
    
    public Web2LeadSettings__c getSettings() {
    	return [select id, Web2LeadSource__c, Web2LeadDeploymentName__c, Web2LeadDeployedURL__c,
        				   Web2LeadForwardTo__c, Web2LeadDisplaySalesforceUI__c 
        				from Web2LeadSettings__c where name = :ApexPages.currentPage().getParameters().get('name')];
    }
    
    public PageReference save() {
    	this.lead = (Lead)this.stdController.getRecord();
        this.lead.Web2LeadDeployment__c = this.settings.Web2LeadDeploymentName__c;
    	this.lead.LeadSource = this.settings.Web2LeadSource__c;
    	
    	this.stdController.save();
    	    	
    	return null;
    }
}

 

 

No matter what I try I can't seem to set the "lead" fields to values from my "settings" object.

 

Any ideas?

aballardaballard

You don't appear to have ever initialized the settings variable.    Somewhere you need a "settings= getSettings(); "

(Or you can define settings using the apex property syntax.)