+ Start a Discussion
Denise CrosbyDenise Crosby 

commandbutton action return to original page

Hello Salesforce exports,
Newbie question here. I have a VF page using a custom controller with a commandbutton navigating to the standard New Contact page. After the user saves a contact there, I need to return to my VF page with the data refreshed. I'm hoping there is a simple way to do this, but I haven't come up with anything yet. Help is greatly appreciated.
<apex:commandButton value="New Contact" action="{!URLFOR($Action.Contact.NewContact)}"  />

 
Best Answer chosen by Denise Crosby
Alain CabonAlain Cabon
Ok, I understand. The navigation is very different on SF1/Lightning and the URL hack doesn't work. Sorry but while you posted your code you also gave the technical "detail" of SF1 which actually changes everything. As you already noticed, you can't get the URL anymore (/apex/ something) like before in Classics because this kind of URL hack for the VFP is now impossible with SF1/Lightning.

The real bad news is that the only solution if a VFP/Lex component for replacing the entire standard edition of your contact creation (difficult to do) just because you want your own custom "Save" button and your custom return.

I know very well the problem because I have had to recode the standard edition of the account just for this problem of URL hack on the save button.

If the layout for your contact is simple, you can use a free AppExchange tool for generating an entire VF page automatically from your standard layout but it is bad for your end users to get a VF page instead of the standard nice SF1 layout (perhaps) just for this problem of navigation after the save. You can change the graphic charter of this new VF page for a better illusion but the lookup component will be like the one used in Classics.

Sorry but the hack of the "Save" button is still a very big problem with SF1/Lex (possible but expensive).

Regards
Alain

All Answers

Alain CabonAlain Cabon
Hello Denise,

I am not a Salesforce export (not yet, nor import).

If you create a contact from a VP page with a standard controller Account, the command button will be like that:

 <apex:commandButton value="New Contact" action="{!URLFOR($Action.Contact.NewContact,null,[saveURL='/apex/myVFPname?id=' + account.Id]) }" /> 

or just change the value for the id used for the return after the save.

 <apex:commandButton value="New Contact" action="{!URLFOR($Action.Contact.NewContact,null,[saveURL='/apex/myVFPname?id=' + myObject.Id]) }" /> 
 
<apex:commandButton value="New Contact" action="{!URLFOR($Action.Contact.NewContact,null,[saveURL='/apex/myVFPname?id=' + myObject.Id]) }" />

(the trick: keep the brackets).

Regards
Denise CrosbyDenise Crosby
Hi Alain,
Thanks for your witty response. Made me smile. I'll apologize in advance for the newbie follow up questions. My VF page is used as a global quick action only. How do I get the url of the page? Or is it just \apex\NewEvent? I hardcoded it with an event id that I know, but it doesn't work. After saving, it navigates to the contact that was created instead of back to the NewEvent VF page. Can you see anything I'm doing wrong with this code?
<apex:commandButton value="New Contact" action="{!URLFOR($Action.Contact.NewContact,null,[saveURL='/apex/NewEvent?id=00U0x0000016BF4EAM'])}"  />

Thanks for helping
Alain CabonAlain Cabon
Hi Denise,

Is /apex/NewEvent the name of your vf page?  If it is correct, that should work but there could be a problem with the controller and extension you are using.

Could you post the source code of your VF page (or at least the first line <apex:page> ?

Regards
Denise CrosbyDenise Crosby
Here is the code. I don't know what the url is... I'm using it as a global action on SF1, so I can't see the url. Hopefully, I'm missing something very simple. Thank you! :)
 
<apex:page controller="NewEventController" showHeader="true" sidebar="true" standardStylesheets="true">
<apex:form >
    
<apex:slds >
        
<div class="slds-scope"> 
    
<apex:pageBlock title="New Meeting">

<apex:pageMessages />
<apex:pageBlockSection columns="1" id="eventInformation">
    <apex:inputfield value="{!ev.subject}"/> 
    <apex:inputfield value="{!ev.startdatetime}" />     
    <apex:inputfield value="{!ev.description}"/> 

    <apex:inputfield label="{!accountLabel}" value="{!dummyContact.AccountId}">
        <apex:actionSupport action="{!changeAccount}" event="onchange" rerender="eventInformation" />
    </apex:inputField>
    <apex:selectList label="{!contactLabel}" value="{!contactId}" size="1" rendered="{!showContactOptions}">
        <apex:selectOptions value="{!contactOptions}" />
        <apex:actionSupport action="{!changeContact}" event="onchange" rerender="eventInformation" />
    </apex:selectList>
    <apex:pageBlockSectionItem rendered="{!!showContactOptions}"/>
</apex:pageBlockSection>
        
<apex:pageBlockbuttons >
   <apex:commandbutton value="Save" action="{!save}"/>  
   <apex:commandbutton value="Cancel" action="{!cancel}"/>  
    
    <apex:commandButton value="New Contact" action="{!URLFOR($Action.Contact.NewContact,null,[saveURL='/apex/NewEvent?id=00U0x0000016BF4EAM'])}"  /> 
            
</apex:pageBlockbuttons>    
    
</apex:pageBlock>
</div>
</apex:slds>       
</apex:form>
</apex:page>



public class NewEventController {

public event ev { get; set; }
    
public NewEventController (){
    ev = new event();
    ev.CurrencyIsoCode = UserInfo.getDefaultCurrency();
    ev.OwnerId = UserInfo.getUserId();
    ev.StartDateTime = Datetime.now().addMinutes(-Datetime.now().minute());
    ev.EndDateTime = Datetime.now().addMinutes(60-Datetime.now().minute());  
    ev.Subject = 'New Meeting';
    
    if ( ev.WhatId != null && ev.WhatId.getSObjectType() == Account.sObjectType )
        {
            dummyContact.AccountId = ev.WhatId;
        }
}

public Contact dummyContact
    {
        get
        {
            if ( dummyContact == null ) dummyContact = new Contact();
            return dummyContact;
        }
        private set;
    }

    public List<SelectOption> contactOptions
    {
        get
        {
            if ( contactOptions == null && dummyContact.AccountId != null )
            {
                contactOptions = new List<SelectOption>();
                contactOptions.add( new SelectOption( 'null', '--None--' ) );
                for ( Contact contact :
                    [   SELECT  Id, Name
                        FROM    Contact
                        WHERE   AccountId = :dummyContact.AccountId
                    ]
                    )
                {
                    contactOptions.add( new SelectOption( contact.Id, contact.Name ) );
                }

            }
            return contactOptions;
        }
        private set;
    }

    public Boolean showContactOptions
    {
        get { return contactOptions != null && contactOptions.size() > 1; }
    }

    public String contactId { get; set; }
    
	public String accountLabel
    {
        get { return Account.sObjectType.getDescribe().getLabel(); }
    }

    public String contactLabel
    {
        get { return Contact.sObjectType.getDescribe().getLabel(); }
    }
    
	public void changeAccount()
    {
        ev.WhatId = dummyContact.AccountId;
        ev.WhoId = null;
        contactOptions = null;
    }

    public void changeContact()
    {
         
        ev.WhoId =
        (   ( contactId != null && contactId != 'null' )
        ?   Id.valueOf( contactId )
       :   null
        ); 
    }    
    
public PageReference save() {
      return new ApexPages.StandardController(ev).save();
  }   
    
public PageReference cancel() {
      return new ApexPages.StandardController(ev).cancel();
  }     
}

 
Alain CabonAlain Cabon
Ok, I understand. The navigation is very different on SF1/Lightning and the URL hack doesn't work. Sorry but while you posted your code you also gave the technical "detail" of SF1 which actually changes everything. As you already noticed, you can't get the URL anymore (/apex/ something) like before in Classics because this kind of URL hack for the VFP is now impossible with SF1/Lightning.

The real bad news is that the only solution if a VFP/Lex component for replacing the entire standard edition of your contact creation (difficult to do) just because you want your own custom "Save" button and your custom return.

I know very well the problem because I have had to recode the standard edition of the account just for this problem of URL hack on the save button.

If the layout for your contact is simple, you can use a free AppExchange tool for generating an entire VF page automatically from your standard layout but it is bad for your end users to get a VF page instead of the standard nice SF1 layout (perhaps) just for this problem of navigation after the save. You can change the graphic charter of this new VF page for a better illusion but the lookup component will be like the one used in Classics.

Sorry but the hack of the "Save" button is still a very big problem with SF1/Lex (possible but expensive).

Regards
Alain
This was selected as the best answer
Alain CabonAlain Cabon
"Salesforce Visualforce Code Generator" NOVEMBER 28, 2016 / DIEFFREI

URL Hacking is not a good choice. In the most of the cases, the right choice is to create a visualforce.
It’s the best choice, but not the fastest choice.

To turn it fast, Dieffrei Tiepo de Quadros (Brazil) created a visualforce code generator:

https://visualforce-generator.herokuapp.com

http://dieffrei.com/en_US/salesforce-visualforce-code-generator/
 
Denise CrosbyDenise Crosby
Thank you Alain for all the great advice and for spending time looking at this. I am now trying to use a pageblock at the bottom of the page to add the contact (everything would be on a single VF page). The only real issue I have now is after insertion, I need my contact select list to refresh. I haven't been able to figure it out. I opened a new question about this. Thanks again for your wonderful help. I am new at this, so just trying to find something that will work.

https://developer.salesforce.com/forums/ForumsMain?id=9060G000000MPW6QAO