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
stollmeyerastollmeyera 

VF Extension - PageReference Save() - Not saving page

I have two VF pages in a wizard.  The idea is when the user hits Save on the first page, it grabs a field I have titled OppID__c from the Account and redirects the user to the second VF page ("/apex/closeopp2?id=" + OppID).  The redirect works great, but the extension doesn't actually save any of the fields entered on the first page of the wizard.  Code below:

 

public class CloseOppRedirect{

    private final Account a;
    public CloseOppRedirect(ApexPages.StandardController controller){
        this.a = (Account)controller.getRecord();
     }            
    
    //hoping that this will get me the OppID
    public PageReference save() {
        
    Account a = [select ID, OppID__c from Account where ID =:ApexPages.currentPage().getParameters().get('id')];
        String Oppid = a.OppID__c;
        
        PageReference contPage = new PageReference('/apex/closeopp2?id=' + Oppid);
        contPage.setRedirect(true);
        return contPage;
        
    }
 }

 

I am super new to APEX so any help would be greatly appreciated.  

DannyK89DannyK89

Could I see the code for the visual force page. That might help. 

stollmeyerastollmeyera

Surely, attached:

 

<apex:page standardController="Account" extensions="CloseOppRedirect" title="Close Opportunity - {!Account.Name}">
    <apex:form >
        <apex:pageBlock mode="edit" title="Close Opportunity - Step 1">
            <b><apex:messages style="color:red" id="error"/></b>
            <apex:pageblockSection title="Account Info" collapsible="false">
                <apex:inputfield value="{!Account.Name}" required="true" label="Company Name"/>
                <br/>
                <br/>
                <br/>
                <apex:inputfield value="{!Account.Industry}" required="true" style="{!if(Account.Industry = null,"color:red; font-weight:bold", null)}" />
                <apex:inputfield value="{!Account.Account_Size__c}" required="true"  style="{!if(Account.Type_of_Business__c = null,"color:red; font-weight:bold", null)}" />
                <apex:inputfield value="{!Account.Secondary_Market__c}"  style="{!if(Account.Secondary_Market__c = null,"color:red; font-weight:bold", null)}" />
                <apex:inputfield value="{!Account.Region__c}" required="true" style="{!if(Account.Region__c= null,"color:red; font-weight:bold", null)}" />
                <apex:inputfield value="{!Account.Type_of_Business__c}" required="true" style="{!if(Account.Type_of_Business__c= null,"color:red; font-weight:bold", null)}" />
                <apex:inputfield value="{!Account.Timezone__c}" required="true"  style="{!if(Account.Timezone__c= null,"color:red; font-weight:bold", null)}" />
                <apex:inputfield value="{!Account.Competitor__c}"  />
                <apex:inputfield value="{!Account.Lead_Type__c}" required="true" style="{!if(Account.Lead_Type__c = null,"color:red; font-weight:bold", null)}" />
                <br/>
                <apex:inputfield value="{!Account.Contact_Method__c}" required="true" style="{!if(Account.Contact_Method__c= null,"color:red; font-weight:bold", null)}" />
            </apex:pageblockSection>
            <apex:pageblockSection title="Contact Info" collapsible="false">
                <apex:inputfield value="{!Account.Phone}" taborderhint="1" />
                <apex:inputfield value="{!Account.BillingStreet}"  taborderhint="5" />
                <apex:inputfield value="{!Account.Phone_2__c}" taborderhint="2" />
                <apex:inputfield value="{!Account.BillingCity}"  taborderhint="6" />
                <apex:inputfield value="{!Account.Email__c}"  taborderhint="3" />
                <apex:inputfield value="{!Account.BillingState}"  taborderhint="7" />
                <apex:inputfield value="{!Account.Website}"  taborderhint="4" />
                <apex:inputfield value="{!Account.BillingPostalCode}"  taborderhint="8" />
                <br/>
                <apex:inputfield value="{!Account.BillingCountry}"  taborderhint="9" />
            </apex:pageblockSection>
            
            <center>
                <apex:commandButton value="Next Step" action="{!save}" style="width:20%" />
                <apex:commandbutton value="Cancel" action="{!cancel}" />
            </center>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

aballardaballard

You don't seem to be actually doing the save.   You extension class save method needs to call the standard controller save method to get the save done.

stollmeyerastollmeyera

I actually did notice that I was missing the save method.  However, upon adding it I was still having issues.  When I access my VF page and save the record, I get the below error:

 

Visualforce Error

System.NullPointerException: Attempt to de-reference a null object 

Class.CloseOppRedirect.save: line 15, column 9 External entry point

 

Line 15, Col 9 points to the save method.  I am unsure what it means to de-referene a null object when that object is the save method.  Updated code below.  

 

public class CloseOppRedirect{

    ApexPages.StandardController controller;
    private final Account a;
    public CloseOppRedirect(ApexPages.StandardController controller){
        this.a = (Account)controller.getRecord();
     }            
    
    //hoping that this will get me the OppID
    public PageReference save() {
        
    Account a = [select ID, OppID__c from Account where ID =:ApexPages.currentPage().getParameters().get('id')];
        String Oppid = a.OppID__c;
        
 controller.save();
        PageReference contPage = new PageReference('/apex/closeopp2?id=' + Oppid);
        contPage.setRedirect(true);
        return contPage;
        
    }
 }

 

aballardaballard

You need to set the controller field of the class so it is available in the save method.  i.e. in the constructor, set

this.controller = controller;

to save the object that is passed to the constructor for later use.

stollmeyerastollmeyera

Thank you, aballard!!!  Not having much experience, it is great to come to these boards and utilize the great salesforce minds out there!!!!

 

Final code below (let's hope this is final :smileyhappy: ):

 

public class CloseOppRedirect{

    ApexPages.StandardController controller;
    private final Account a;
    public CloseOppRedirect(ApexPages.StandardController controller){
        this.a = (Account)controller.getRecord();
        this.controller = controller;
     }            
    
    //hoping that this will get me the OppID
    public PageReference save() {
        
    Account a = [select ID, OppID__c from Account where ID =:ApexPages.currentPage().getParameters().get('id')];
        String Oppid = a.OppID__c;
        
        controller.save();
        PageReference contPage = new PageReference('/apex/closeopp2?id=' + Oppid);
        contPage.setRedirect(true);
        return contPage;
        
    }
 }