2

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:

EditBothAC

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(){


        con.save();

        update contacts;


        PageReference result = ApexPages.currentPage();
        result.setRedirect(true);
        return result;
    }

}

And here's my vf page:

EditBothACPage

<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>
                <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:repeat>
                </apex:pageBlockSection>
            </apex:pageBlock>
            <apex:commandButton value="Save" action="{!save}"/>
    </apex:form>

</apex:page>

Any help would be greatly appreciated.

2
ApexPages.currentPage()

returns current page url.

This would get back you to account detail page:

PageReference result = new PageReference('/'+con.getId());
  • Thanks @Mr. Frodo it works! By the way, does this approach also works when there's a wrapper class involved? – beep Jun 26 '16 at 5:34
  • It will always work.You just have to keep the id of the account from where you are coming. – Mr.Frodo Jun 26 '16 at 5:36
  • Okay I'll try that with wrapper class, too. Thanks again. – beep Jun 26 '16 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.