I have a VS page embedded in the standard Account page, using Tabs/pageBlockTables to separate Active Contacts from Inactive Contacts. Now I need a 'New Contact' button to navigate to the standard New Contact page, and to auto-relate the new Contact to the Account. It also should return the user to the Account page that called 'New Contact'. I am new to Apex/VisualForce and I can't get anything I've found so far to work.

  • You should provide the code you have so far and where specifically you are stuck – Adrian Larson Jan 24 '17 at 12:25
  • My VS page looks like this. the button opens a new contact page, but the Account Id is not filled in/related. <apex:tabPanel switchType="Ajax"> <apex:tab label="Active Contacts" name="ActiveContacts"> <apex:pageBlock title="Contacts"> <apex:commandButton value="New Contact" action="{!URLFOR($Action.Contact.NewContact)}" /> <apex:pageBlockTable value="{!ActiveContacts}" var="a"> <apex:column headervalue="View"><apex:outputLink value="/{!a.id}"> – Gary Fitz Jan 24 '17 at 15:12
  • @GaryFitz You can edit your question at any time. So better to update the question with all required details rather than putting in comments whenever possible. – Saroj Bera Jan 25 '17 at 11:23

Here's a solution :

Make your New Contact button(or link) call this action :

<apex:pageBlockButtons location="top">
    <apex:commandLink value="New Contact" action="{!newContact}" styleClass="btn"/>

and then in newContact Action put this code :

public PageReference newContact(){
    PageReference pg = new PageReference('/003/e');

    pg.getParameters().put('saveURL', '/'+ currentAccount.Id);
    pg.getParameters().put('cancelURL', '/'+ currentAccount.Id);
    pg.getParameters().put('accid', currentAccount.Id);

    return pg;


the currentAccount class variable you should instantiate it in your class Constructor :

currentAccount = [select Id, Name from Account where Id = :<yourAccountId>];
  • you're welcome, can you please mark the answer as the best one – ihssan Jan 24 '17 at 16:02

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.