[SalesForce] Redirect to Parent Detail Page After Updating Both Parent and Child Objects

How can I redirect my page to parent detail page upon updating both parent and child objects? Both records are saving successfully, but everytime I click the save button, it doesn't go back to the master detail record page.

Here's my controller:


public class EditBothAC{

    private ApexPages.StandardController con;
    public List<Contact> contacts {get;set;}

    public EditBothAC(ApexPages.StandardController controller){
        con = controller;

        contacts = [SELECT Id, FirstName, LastName, Email FROM Contact WHERE accountid=:con.getId() ORDER BY FirstName ASC LIMIT 5];


    public PageReference save(){


        update contacts;

        PageReference result = ApexPages.currentPage();
        return result;


And here's my vf page:


<apex:page standardController="Account" extensions="EditBothAC">

    <apex:form >

            <apex:pageBlock title="Account Detail">
                <apex:pageBlockSection title="Account">
                    <apex:inputField value="{!Account.Name}"/>
                    <apex:inputField value="{!Account.Description}"/>
                <apex:pageBlockSection title="Contacts">
                    <apex:repeat value="{!contacts}" var="c">
                        <apex:inputField value="{!c.FirstName}"/>
                        <apex:inputField value="{!c.LastName}"/>
                        <apex:inputField value="{!c.Email}"/>
            <apex:commandButton value="Save" action="{!save}"/>


Any help would be greatly appreciated.

Best Answer


returns current page url.

This would get back you to account detail page:

PageReference result = new PageReference('/'+con.getId());
Related Topic