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
JN22JN22 

Redirect to New Record Just Created

Hello,

 

I have a VF page on a custom object "Test_Object__c"  (child of Account) with a custom controller.  At the moment, when the user enters their data in the VF page and saves it, they are returned to the Account page where they originated.  I would, however, like them to be re-directed to the detail page of the record they just saved.  I know this is probably a fairly simple thing to do, but everything I've tried just keeps returning me to the Accoun.  Below is my controller.  I would appreciate any insight.  Thanks,

 

 

public class TestController3{

public List<Test_Object__c> test {get; set;}
    private final Account acct;
    public TestController3(ApexPages.StandardController myController) {
        acct=(Account)myController.getrecord();
                  
        test= new List<Test_Object__c>();
        Test_Object__c test2 = new Test_Object__c();
               test2.Company_Name_Test__c = acct.id;
        test.add(test2);}


    public PageReference save() {
        insert test;
        PageReference acctPage = new ApexPages.StandardController(acct).view();
        acctPage.setRedirect(true);
        return acctPage; }

}

Best Answer chosen by Admin (Salesforce Developers) 
kranjankranjan
My Apolgies JN22. I did not noticed that your test variable is a list. In this case either you can just make that variable as a normal Test_Object__c object variable. But you may need to change lot of code for that. So simple solution is tat you should use the following line at line 16.

PageReference acctPage = new PageReference('/' + test[0].id);

All Answers

kranjankranjan

Hi JN22,

You need to change your code in the save method as below:

public PageReference save() {
insert test;
PageReference acctPage = new PageReference('/' + test.id);
acctPage.setRedirect(true);
return acctPage;
}


JN22JN22

Thanks for the reply.  When I change that line (line 16) to what you propose, I get the error below:

 

Error: TestController3 Compile Error: Initial term of field expression must be a concrete SObject: LIST<Test_Object__c> at line 16 column 57

 

Any suggestions?  Thanks,

kranjankranjan
My Apolgies JN22. I did not noticed that your test variable is a list. In this case either you can just make that variable as a normal Test_Object__c object variable. But you may need to change lot of code for that. So simple solution is tat you should use the following line at line 16.

PageReference acctPage = new PageReference('/' + test[0].id);
This was selected as the best answer
JN22JN22

Excellent!  That did the trick!  Thanks for your help!!  Final code is below for anyone interested:

 

VF Page:

 

<apex:page standardController="Account" tabStyle="Account" extensions="TestController3">
   <apex:form >
    <apex:pageBlock title="Test" >
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}" rerender="detail" />
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!test}" var="a" id="table">
                <apex:column headerValue="Company Name">
                   <apex:outputField value="{!a.Company_Name_Test__c}" />
                </apex:column>                
                <apex:column headerValue="Input 1">
                    <apex:inputField value="{!a.Input_1__c}"/>
                </apex:column>                
                <apex:column headerValue="Input 2">
                    <apex:inputField value="{!a.Input_2__c}"/>
                </apex:column>
            </apex:pageBlockTable>
    </apex:pageBlock>
    </apex:form>
</apex:page>

 

 

Controller:

 

public class TestController3{

public List<Test_Object__c> test {get; set;}
    private final Account acct;
    public TestController3(ApexPages.StandardController myController) {
        acct=(Account)myController.getrecord();
                  
        test= new List<Test_Object__c>();
        Test_Object__c test2 = new Test_Object__c();
               test2.Company_Name_Test__c = acct.id;
        test.add(test2);}

    public PageReference save() {
        insert test;
        PageReference RetPage = new PageReference('/' + test[0].id);
        RetPage.setRedirect(true);
        return RetPage; }
}

kranjankranjan
Good to know JN22. :-)