Custom button to open opportunity after lead is converted

I have a global class that creates an Opp from a custom lead conversion button.
The problem is that the button works to convert the lead to an Opp but it does not open the Opp. I get an error message that “that URL does not exist”.

Ideas?

global class convertLead{
    WebService static Id  convertLeadtoAcc(string id){
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(id);
        LeadStatus convertStatus = [SELECT Id, MasterLabel 
                                    FROM LeadStatus 
                                    WHERE IsConverted=true LIMIT 1];                
        lc.setConvertedStatus(convertStatus.MasterLabel);
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        Id OpportunityId=[select ConvertedOpportunityId from Lead where id=:id].ConvertedOpportunityId;
        return OpportunityId;
    }
}

My button is an OnClick Javascript button with this code:

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
var opportunityId=sforce.apex.execute("ConvertLeadCustom","convertLeadtoAcc", {id:"{!Lead.Id}"});
var newURL = "/"+opportunityId[0].id+"/e";
window.location.href=newURL;

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Since your method returning opportunity Id means var opportunityId contains the actual opportunity Id

You just need redirect to

var newURL = "/"+opportunityId+"/e";
window.location.href=newURL;


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x