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
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

Saving record and returning to Tabbed view with record selected

Hey there,

 

I have a tabbed Account visualForce page with the related lists appearing as tabs. I Have inserted new record buttons in each tab. My question is:

 

Is it possible to make it so that after creating a new record and clicking save, the user will be redirected back to the accounts tabbed visualforce page, with the tab opened which the user just created the record for?

 

This is my code:

 

<apex:page standardController="Account" showHeader="true" 
tabStyle="account"  >

  
<style>
.activeTab {background-color: #892034; color:White;
background-image:none}
.inactiveTab { background-color: #00204E; color:white;
background-image:none}




</style>
<apex:tabPanel switchType="client" selectedTab="tabdetails"
id="AccountTabPanel" tabClass="activeTab"
inactiveTabClass="inactiveTab">
<apex:tab label="Details" name="AccDetails" id="tabdetails">
<apex:detail inlineEdit="True" relatedList="false" title="true"/>
</apex:tab>
<apex:tab label="Contacts" name="Contacts" id="tabContact">
<apex:pageBlock title="Hello {!$User.FirstName}!">
You are displaying contacts from the {!account.name} account.
Click a contact's name to view his or her details.
</apex:pageBlock>
<apex:pageBlock title="Contacts">
<apex:form >
<apex:dataTable value="{!account.Contacts}" var="contact" cellPadding="4"
border="1">
<apex:column >
<apex:commandLink rerender="detail">
{!contact.Name}
<apex:param name="cid" value="{!contact.id}"/>
</apex:commandLink>
</apex:column>
</apex:dataTable>
</apex:form>
</apex:pageBlock>
<apex:outputPanel id="detail">
<apex:detail subject="{!$CurrentPage.parameters.cid}" relatedList="True" inlineEdit="True"
title="false"/>
</apex:outputPanel>
</apex:tab>





<apex:tab label="Destiny Products" rendered="{!$ObjectType.Destiny_Products__c.Accessible}" name="Destiny Products" id="tabDestinyProducts" >
<apex:pageBlock title="Destiny Products" >
<apex:form >
<apex:outputlink value="/a0E/e?CF00N90000005dLWU={!Account.name}&CF00N90000005dLWU_lkid=                                                                                                                           {!Account.id}">Create New Fact Finder</apex:outputlink> 
<apex:pageBlockTable value="{!account.Destiny_Products_and_services__r}" var="Pro" cellPadding="4"  border="4">
<apex:column headerValue="Destiny Products" >
<apex:commandLink rerender="Prodetails"> {!Pro.Name}
<apex:param name="Pid" value="{!Pro.id}"/>
</apex:commandLink>
</apex:column>
<apex:column value="{!Pro.Product_Name__c}"/>
<apex:column value="{!Pro.Product_Stage__c}"/>
<apex:column value="{!Pro.Payment_Choice__c}"/>
<apex:column value="{!Pro.Amount_Owed__c}"/>
<apex:column value="{!Pro.CreatedDate}"/>
</apex:pageBlockTable>
</apex:form>
</apex:pageBlock>
<apex:outputPanel id="Prodetails">
<apex:detail subject="{!$CurrentPage.parameters.Pid}" relatedList="False" inlineEdit="True" title="false"/>
</apex:outputPanel>

 please help, I have no idea what to do.

Best Answer chosen by Admin (Salesforce Developers) 
ericmonteericmonte
You might want to look at this

http://boards.developerforce.com/t5/Apex-Code-Development/Redirect-to-visual-force-page-after-new-record-save-on-site/td-p/267769

The best way to solution for you is to create an extension and a new reference class that allows you to save the page and do a redirect. You would need to have a new button and reference a custom action instead of using {!save}.

Let me know if this works for you or if you have follow up questions.

All Answers

ericmonteericmonte
You might want to look at this

http://boards.developerforce.com/t5/Apex-Code-Development/Redirect-to-visual-force-page-after-new-record-save-on-site/td-p/267769

The best way to solution for you is to create an extension and a new reference class that allows you to save the page and do a redirect. You would need to have a new button and reference a custom action instead of using {!save}.

Let me know if this works for you or if you have follow up questions.
This was selected as the best answer
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

Thank you for the reply mate. 

 

Please do not feel obligated, but I was wondering if you could possibly provide some example code on how I may acheive this. Otherwise, thank you for your time and you have pointed me in the right direction and it will help me I am sure.

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

Soo basically the idea is to create a button which will re-direct to my visualforce page. Once that is done will it be possible to add more to it in order for it to open at the right tab with the record opened or is that another thing all together?

 

Thank you so much for your time.

ericmonteericmonte

We do this a lot of times for a project where we have a custom button in the objects that redirects them to a VF Page. The VF Page is a custom form that you build. Then when they click save, the record get's inserted and redirects user back to a specific page.

 

Here is a sample code:

 

<apex:page standardController="Contact" extensions="extSaveButton">
    <apex:form >
        <apex:pageBlock >
            <apex:commandButton value="Save" action="{!SaveContact}"/>
            <apex:pageBlockSection >
                <apex:inputField value="{!Contact.FirstName}"/>
                <apex:inputField value="{!Contact.LastName}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>


</apex:page>


here is the Extension

public class extSaveButton {
 
    public Contact con {get;set;}
    public extSaveButton(ApexPages.StandardController controller) {
        this.con = (Contact)controller.getRecord();

    }
    
    Public PageReference saveContact(){
    
        insert con;
        // Send the user to the detail page for the new account.
       PageReference pageRef = new PageReference('/003/o');
        
        return pageRef;
    
    }

}

 

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

So basically correct me if I am wrong here.The first bit of code is a form which will create a new contact. The second is an extension added to the form which will redirect to the Accounts tabbed VF page? Forgive me if I am way off, I am still learning.

So I would change the create new contacts record page with the visualforce page, after creating the extension which will work alongside it and asist in the re-direction to my visualforce page?

ericmonteericmonte

So basically extensions are basically leverage extra functionality based off custom codes. For the snippet code i gave you, it uses the standard controller to use the Out of the box functionality. The Extension basically allows you to customize it, so i created a page reference SaveContact will be called during the action on the Save button. The Action basically retrieves the OOB (out of box) functionality and insert then redirects the page based off the PageReference and it is returned.

 

Does that make sense?

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

I think so, does this mean that I add the controller to my TabbedAccounts VF page, the new record page or am I creating my own button?

 

I understand the first 8/10s of what you say, I get a little lost on the very last line.

 

 

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

Or is the controller the new action which will reference the page...and the code is the button which will reference the action and take us back to the page?

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

I think I understand what everything is and how it works...I am just not sure on where to place it...If that makes sense...

ericmonteericmonte
So in your vf page you are already calling a standard controller in the apex page add extension and call it whatever you want. If you are creating the vf page in the ui there should be an error message asking you to create a call with sharing or not. I usually use a public class (no sharing) so it runs system mode. Then there should be another error asking to create a controller method for the class. Go ahead and add this. Now you have an extension controller for your vf page.


You can either use ui to edit the controller or use IDE. I usually use the IDE.

As you can see in my class I called the contact as a class with a get set, go ahead and do that for your vf page but reference your object you are using for your standard controller.

Next under apex pages.standardcontroller reference a this method and set it get controller method similar to what I have above. This line basically gets the information from the vf page and do something with it.

Next you create a pageref method that will be used when the button is clicked. The page ref basically has an insert function that inserts the record with values in your vf page. Then you have anothe pageref that allows you to set where your page redirects. Then setting the return to the value in the page ref will redirect the page to that URL.

Now you have built the extension you need to add a custom button in your vf page button. In a standard controller, there are different actions that can be used without necessary code and one of them is the {!save} which saves your record and redirects you to the detail page of the record. Now for you redirect the page, you would need to call the pageref method from your controller.

And I think that explains it all.
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

You have helped me so much. Thank you!

 

I will give it a try today and let you know how I go.

 

Mikie

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

Hey Eric,

 

I Feel like I am quite close now...but I am not sure if I have done it right..I have decided to test it out and so I have changed your example code around for my Fact Finder, as shown below: 

 

<apex:page standardController="Fact_Finder__c" extensions="extSaveButton">
    <apex:form >
        <apex:pageBlock >
            <apex:commandButton value="Save" action="{!SaveFactFinder}"/>
            <apex:pageBlockSection >
                <apex:inputField value="{!Fact_Finder__c.Name}"/>
    
                <apex:inputField value="{!Fact_Finder__c.Account__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
    
    </apex:page>
                  


Extension___



public class extSaveButton {
 
    public Fact_Finder__c fac {get;set;}
    public extSaveButton(ApexPages.StandardController controller) {
        this.fac = (Fact_Finder__C)controller.getRecord();

    }
    
    Public PageReference saveFactFinder(){
    
        insert fac;
        // Send the user to the detail page for the new account.
       PageReference pageRef = new PageReference('/001/o');
        
        return pageRef;
    
    }

}


 

I then...swapped the Fact_Finder__c new record page for the visualforce page and changed the page reference to that of the account search.......Now when I click New fact finder, from my Tabbed account VF page, my newly created form comes up and upon saving it I am re-directed to the search rather than the created record. Which is definitely a move in the right direction.

 

My questions:

 

What am I missing to be re-directed back into the visualForce page?

 

Have I done everything right so far?

 

Were I to add the same functionality to other objects, would I have to create an extension and a VF page for each of them indivually....or can I just add more code to the current extension?

 

Thank you

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

I attempted to make my own page reference:

 

PageReference pageRef = new PageReference('/apex/DestinyAccountTest?id=' + 'AccountId' + '&sfdc.override=1');

 

That is....'page name" + "'(attempt to add autopopulated accountId + 'the random stuff at the end when I open my VF page whilst it is connected to an account''...

 

But rather than autofilling the AccountID....it just come sup with.....

 

/apex/DestinyAccountTest?id=AccountId&sfdc.override=1'

ericmonteericmonte

PageReference pageRef = new PageReference('/apex/DestinyAccountTest?id=' + 'AccountId' + '&sfdc.override=1');

You almost had the page Reference correct but i see some error and issues.

 

1. For the AccountId, you are not referenceing any id on this. How are you populating the AccountId?

2. You encapsulated AccountId in single quotes therefore you will get the "AccountID" instead of populated Id. So if you have a variable of of accId that holds the Id information you can do 

 

PageReference pageRef=newPageReference('/apex/DestinyAcountTest?id='+accId+'&Sfdc.override=1');

 

Do you see the difference between my page ref and your page ref?

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

I do see the difference... except when i tried to change it I got an:

 

Error: Compile Error: Variable does not exist: accId at line 13 column 77

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

This is the full code for my extension:

 

public class extSaveButton {
 
    public Fact_Finder__c fac {get;set;}
    public extSaveButton(ApexPages.StandardController controller) {
        this.fac = (Fact_Finder__C)controller.getRecord();

    }
    
    Public PageReference saveFactFinder(){
    
        insert fac;
        // Send the user to the detail page for the new account.
       PageReference pageRef=newPageReference('/apex/DestinyAcountTest?id='+accId'&Sfdc.override=1');
        
        return pageRef;
    
    }

}

 Am i missing something....

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

Do I add the extension to my tabbed account visualforce page too maybe?

ericmonteericmonte
I forgot to mention accId is a variable i created. If you want to pass an Account Id, you need to pass that.

For example if I know that the account Id i can set it into a variable like
String accId = 'insertidhere';
then you just add this to your pageRef.

So in your VF page and your Fact_Finder__c object contains a lookup and you are doing a look up from that VF then you dont have to have a variable.
Example
PageReference pageRef=newPageReference('/apex/DestinyAcountTest?id='+fac.Account__c'&Sfdc.override=1');


So in the end, where are you pulling the Account Id from?
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

Hahahaha Yes!!!!

 

You are a genius!! Thank you soo much for your help. It now redirects to the details page of my tabbed visualforce page. The answer was fac.account__c.  

 

Real quick, I know I have to create a VF page for each object, but do I have to create and extension for each object as well or am I able to extend the one I have?

 

I am very appreciative as I am now one step closer to the vision. I do not want to take anymore of your time, so rather than asking for help. If it is possible at all, would you point me in the right direction of how I may get it to open to the of the record that just got created?

 

Eric, once again thankyou so much for all your help. You really have saved me alot of stress.

 

Mikie

ericmonteericmonte
You can create one extension and reference it it in many VF page. Extensions can be used in various VF Pages, and a VF Page can reference multiple extensions but can only contain either a standard controller or custom controller.

To create a dynamic apex you should start with this:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic.htm

This will allow you to learn create a dynamic extension that can be used for your VF pages.

Here is the Page Ref Documentation:
http://www.salesforce.com/us/developer/docs/pages/Content/pages_quick_start_controller_navigation_methods.htm

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_system_pagereference.htm


To answer your question on redirecting back to the detail page:


Public PageReference saveContact(){

insert con;
// Send the user to the detail page for the new account.
PageReference pageRef = new ApexPages.StandardController(con).view();
pageRef.setRedirect(true);
//PageReference pageRef = new PageReference('/003/o');

return pageRef;

}

Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student

Thank you Eric, I really appreciate it.