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
Richard Houston 20Richard Houston 20 

Visualforce custom save returns pagereference null

Hi,

I have a custom visualforce page with a working save and insert button for a custom child object of opportunities. 

However, I'm running into an issue with the save and redirect when going back in to view/edit the record. I've overriden the custom object's view button to direct to a copy of the existing visualforce page. This one only has a button to update the record instead of inserting a new one. Looking at the debug logs the parent opportunity ID never seems to get passed through and then the page reference returns null. 

How do I go about getting that ID so I can return users to the page they came from?

Thanks!

Controller Extension: 
public with sharing class SurveyParentIDExt{
    public Pursuit_Survey__c OpportunityParent{get;set;}
    
    public SurveyParentIDExt(ApexPages.StandardController controller){
        OpportunityParent=(Pursuit_Survey__c)controller.getRecord();
        system.debug(OpportunityParent);
        String parentID = System.currentPagereference().getParameters().get('parentid');
        OpportunityParent.Opportunity__c=parentid;        
    system.debug(parentid);
    
    }
       public PageReference doSaveWithRedirect(){
        Upsert OpportunityParent;
           system.debug(opportunityparent);
        PageReference ref = new PageReference('/' + System.currentPagereference().getParameters().get('parentid'));
           system.debug(ref);
        ref.setRedirect(true);
        return ref;
    }
    public PageReference saveandUpdate(){
        Update OpportunityParent;
        system.debug(OpportunityParent);
        PageReference ref = new PageReference('/' + System.currentPagereference().getParameters().get('parentid'));
        system.debug(ref);
        ref.setRedirect(true);
        return ref;
       
    }
}

Visualforce Page
 
<apex:page standardController="Pursuit_Survey__c" extensions="SurveyParentIDExt">

<apex:form >
..
..
..
 [Skipping the body to keep this shorter]

  <apex:commandButton  value="Update Responses" action="{!saveandUpdate}" />
</apex:form>
</apex:page>

 
ClintLeeClintLee
How are you setting the 'parentid' parameter when you direct the user to the visualforce page?

You may want to try storing the parentid value in the constructor.  Perhaps it's being cleared before the saveandUpdate method can grab it.  

Try this:
public with sharing class SurveyParentIDExt{
    public Pursuit_Survey__c OpportunityParent{get;set;}
    private String parentID; // add this
    
    public SurveyParentIDExt(ApexPages.StandardController controller) 
    {
        OpportunityParent=(Pursuit_Survey__c)controller.getRecord();
        system.debug(OpportunityParent);
        // store the id in the instance variable
        parentID = System.currentPagereference().getParameters().get('parentid');
        OpportunityParent.Opportunity__c=parentid;        
        system.debug(parentid);
    }

    public PageReference doSaveWithRedirect()
    {
        Upsert OpportunityParent;
        system.debug(opportunityparent);
        PageReference ref = new PageReference('/' + parentID);  // use the instance variable here
        system.debug(ref);
        ref.setRedirect(true);
        return ref;
    }

    public PageReference saveandUpdate()
    {
        Update OpportunityParent;
        system.debug(OpportunityParent);
        PageReference ref = new PageReference('/' + parentID);  // use the instance variable here
        system.debug(ref);
        ref.setRedirect(true);
        return ref;
    }
}

Hope that helps,

Clint
Richard Houston 20Richard Houston 20
Clint,

Thanks for the quick reply. I'm passing parentid through a custom button on the opportunity.

/apex/Pursuit_Survey?parentid={!Opportunity.Id}

I'm not sure how I would pass that through on a related list view override though. 

Thanks again for the help!
Amit Chaudhary 8Amit Chaudhary 8
You need to pass two parameter in you page on is ID {!Pursuit_Survey__c.id} and parentid Id.
/apex/Pursuit_Survey?parentid={!Opportunity.Id}&id={!Pursuit_Survey__c.id}
Vishal_GuptaVishal_Gupta
Hi Richard,

You need to keep your custom Button on the detail page of Opportunity instead of related list, you will get opportunity Id after that in your  
Pursuit_Survey Vf page.

Please let me know if need more inputs.

Thanks,
Vishal