I've got a custom Visualforce Page that overrides the standard record view on an object called Opportunity__c.

I've got a Custom Button on Account with the following code:

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
var result = sforce.apex.execute("AccountAction","convertToOpportunity",{a:"{!Account.Id}"});
parent.location.href = '/'+result[0].Id+'/e';

What this does is that when a User is on an Account record, it invokes the following code below:

WebService static Opportunity__c convertToOpportunity(Id a) {
  Account account = [SELECT Id, Name FROM Account WHERE Id = :a];

  Opportunity__c opp = new Opportunity__c(
    Name = account.Name,
    OwnerId = UserInfo.getUserId(),
    Account__c = account.Id
    // Other stuff...
  );

  INSERT opp;

  return opp;
}

This code create an Opportunity__c record, as expected, and redirects the user to the Standard edit page.

The problem I've got is what happens when the User clicks Save. Rather than redirect to the record, it redirects to the home page.

I feel like the problem is in the following line:

parent.location.href = '/'+result[0].Id+'/e';

I've tried the following:

parent.location.href = '/'+result[0].Id+'/e?retUrl='+result[0].Id;

And

parent.location.href = '/'+result[0].Id+'/e?saveUrl='+result[0].Id;

Whilst these successfully go into the Url, they seem to make no difference to where the page actually redirects to.

How can I get the page to redirect to the record page, once the user has finished editing it?

Edit

I tried disabling the custom Visualforce page, but it still redirected to the Home page, rather than the record page.

  • Hi Dan. Did you check that your variable result[0].Id contains the right value? – Martin Lezer Jan 16 '17 at 12:16
  • @MartinLezer It does. All the convertToOpportunity() function does is creates a brand new record, the intention is to then redirect the User to the Edit Page of that new record. That all works fine, it's just when the User clicks "Save" on the standard Edit Page does it redirect to the Home Page, rather than the Record Page. – Dan Jones Jan 16 '17 at 12:19
  • It seems that you can use the window.location.replace(url) method to simulate an http redirect. I think you should try that. – Martin Lezer Jan 16 '17 at 12:52
  • @MartinLezer I've got it and posted an answer below, it was just a case-sensitivity problem... Do'h! – Dan Jones Jan 16 '17 at 13:00
up vote 2 down vote accepted

I've got it.

I was reading this article and although I was right to be using:

parent.location.href = '/'+result[0].Id+'/e?saveUrl='+result[0].Id;

The problem was just a case of, case-sensitivity.

parent.location.href = '/'+result[0].Id+'/e?saveURL='+result[0].Id;

Note the capitalised saveURL.

Once I made that little change, it worked as expected.

According to that article, Salesforce behaves by prioritising in the following:

  1. Is there a saveURL URL parameter set? If so, redirect to that.
  2. Is there a retURL URL parameter set? If so, redirect to that.
  3. Redirect to /home/home.jsp

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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