function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDCDevQASFDCDevQA 

Open new opportunity when lead is converted

I can't believe no one out there has wanted to do this but I've searched and searched and haven't found anything similar.  Specifically when converting a lead with an opportunity we'd like it to immedietly go to the add product page for the opportunity or at least just the page for the opportunity that was just created.  It seems like there should be some way to do this.  Something must tell it that it was just at the lead page and is now at the account page even if it's an existing account.  I just can't find how.  If anyone has any ideas on this I would love to hear them.

Thanks,
Amanda
gokul bharatigokul bharati
Hi Amanda,

Please find the below snippet.

1) Create a global apex class 

2) Define a static webservice method with logic to convert the lead 

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;
    }
}

3) Create a custom convert Lead button

Now use the button to call the below javascript

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")} 
var opportunityId=sforce.apex.execute("convertLead","convertLeadtoAcc", {id:"{!Lead.Id}"});

Load the window with the Opportunity Id.

Thanks,
Gokul





SFDCDevQASFDCDevQA
Thank you Gokul but wouldn't this mean that I would need to define all field mappings and everything in the webservice method?  I'm hoping to stay away from something with long term coding maintenance required.  I'd like to be able to use the standard convert but somehow tell it when it opens the account page to switch to the opportunity instead.  It should be able to see something within that account page showing that a lead was just converted correct?  A semi complex option I'm thinking of would be to have a checkbox on the account that would trigger when an opp was created to say that a lead was converted and give the opp id.  Then a VF page would be imbedded in the Account Page and look to see if that was filled in and push to open the opp page and clear out the field.  I think that's doable and would not require long term coding but it seems like there should be a simpler way.
Gokul  BharatiGokul Bharati
Hi Amanda,

System will do the mapping automatically,Please have a look at this link.

https://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_dml_examples_convertlead.htm

Thanks,
Gokul
gokul bharatigokul bharati
Hi Amanda,

P.S. If this answers you question, please mark it as "Best Answer" so it will help other community members too.

Thanks,
Gokul
PinkeyPinkey
This is very heplful. thank you i am looking for the same functionality.
Melissa Shook 8Melissa Shook 8
So I used 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;

It creates the Opportunity but it won't open it. I get a URL doesn't exist error message page at the following address. https://cs51.salesforce.com/undefined/e
Any ideas?