[SalesForce] Custom Button Redirect

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.

Best Answer

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