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
SFDC coderSFDC coder 

How to display the detail page after clicking on save in Visualforce?

hi

I have a custom controller in which i have placed the logic of the save method.
However after clicked on save i want to display the detail page of the newly inserted record to the user

How do i do it?

Any help would be appreciated

Thanks
Best Answer chosen by SFDC coder
sfdc Beginnersfdc Beginner
Hi SFDC CODER,

TRY THIS YOU WILL GET THE SOLUTION.


VISUALFORCE PAGE

<apex:page controller="accountdetailcon" >
    <apex:form >
    <apex:pageBlock >
    <apex:pageBlockButtons location="Top" >
     <apex:commandButton value="Save" action="{!saverecord}"/> <br/><br/>
    </apex:pageBlockButtons>
        Account Name &nbsp;&nbsp;<apex:inputText value="{!name}"/><br/><br/>
        Phone &nbsp;&nbsp;<apex:inputText value="{!phone}"/>
    </apex:pageBlock>   
    </apex:form>
</apex:page>


CONTROLLER

public class accountdetailcon {

    public String name { get; set; }
   
    public String phone { get; set; }

   
    public PageReference saverecord() {
       
        Account a = new Account();
        a.name = name;
        a.phone = phone;
        insert a;
       
        pagereference pr = new pagereference('/' + a.id);
       
        return pr;
    }


   
}


Regards,
sfdc beginner

All Answers

Sonam_SFDCSonam_SFDC
Make the save method return a Pagereference and this pagereference will be of the detail page of the record inserted..something similar to :

http://www.faqoverflow.com/salesforce/3907.html
SFDC coderSFDC coder
hi sonam,

Thanks for your reply..
I just wanted to know whether the above piece of code  mentioned in ur link would work for custom controllers?

Thanks
SFDC coderSFDC coder
Also i have customized my save method
SFDC coderSFDC coder
hi sonam,

private ApexPages.StandardController controller;

    public SaveAndReturnController(ApexPages.StandardController controller)
    {
        this.controller = controller;
    }

this thing is throwing error.i have used it this way:

   public Product2 pro{get;set;}
  
   private ApexPages.StandardController controller;

    public OuterClass(ApexPages.StandardController controller)
    {
        this.controller = controller;
    }

  
    public PageReference save() {
       
        controller.save();
        PageReference view = controller.view();
        return view ;
    }
Sonam_SFDCSonam_SFDC
aah, you are using a custom controller, sorry slipped my mind - this piece of code would not work in this condition as it has a ApexPages.StandardController instance. Will check and let you know if I find a workaround.
Madhura BMadhura B

Try making these changes to your save method

 

public PageReference save()
    {
        PageReference cancel = controller.cancel();
        controller.save();
        return cancel;
    }

SFDC coderSFDC coder
Hi madhura,

I wonder ,how will it wrk?controller is an instance of ApexPages.standardController and my class is a custom controller
Madhura BMadhura B

I might have misunderstood because of this part of your code

private ApexPages.StandardController controller;

    public OuterClass(ApexPages.StandardController controller)
    {
        this.controller = controller;
    }

If you have customised your Save method, and I assume you have written insert/ upsert/ update a record can you try

PageReference pRef= new new PageReference('/'+recordId);
pRef.setRedirect(true);
return pRef;

sfdc Beginnersfdc Beginner
Hi SFDC CODER,

TRY THIS YOU WILL GET THE SOLUTION.


VISUALFORCE PAGE

<apex:page controller="accountdetailcon" >
    <apex:form >
    <apex:pageBlock >
    <apex:pageBlockButtons location="Top" >
     <apex:commandButton value="Save" action="{!saverecord}"/> <br/><br/>
    </apex:pageBlockButtons>
        Account Name &nbsp;&nbsp;<apex:inputText value="{!name}"/><br/><br/>
        Phone &nbsp;&nbsp;<apex:inputText value="{!phone}"/>
    </apex:pageBlock>   
    </apex:form>
</apex:page>


CONTROLLER

public class accountdetailcon {

    public String name { get; set; }
   
    public String phone { get; set; }

   
    public PageReference saverecord() {
       
        Account a = new Account();
        a.name = name;
        a.phone = phone;
        insert a;
       
        pagereference pr = new pagereference('/' + a.id);
       
        return pr;
    }


   
}


Regards,
sfdc beginner
This was selected as the best answer