I want my agents to be able to convert a lead to an account from the lead list view. I have created the custom button but need the appropriate code. I used what was suggested but get a message Missing ; Before startement

Can anyone help me correct this?

Thanks Emerson

  • 3
    Welcome to Salesforce Stackexchange! Please edit your question and include the code which you are using, so that we can help you! – Shumon Saha Jun 9 '13 at 18:26
  • 1
    Sounds like a JavaScript syntax error. Please post your code. – Mike Chale Jun 9 '13 at 21:32

This is how i would have approached this solution

1) Create a global apex class 2) Define a static webservice method with logic to convert the lead 3) call the method from javascript

The apex class would look something like this

global class convertLead{
    WebService static void convertLeadtoAcc(string id){
        Database.LeadConvert lc = new database.LeadConvert();
        LeadStatus convertStatus = [SELECT Id, MasterLabel 
                                    FROM LeadStatus 
                                    WHERE IsConverted=true LIMIT 1];                
        Database.LeadConvertResult lcr = Database.convertLead(lc);

Now use the javascript to call this method

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

Ofcourse you might want to bulkify the methods to process multiple leads at a time. However the basic idea would be the same.

Hope this helps.

| improve this answer | |

Looks like you'll have to combine 2 functionalities.

GETRECORDIDS to fetch ids of records for which checkbox was ticked in the listview. (Caveat: last time I had to use it it didn't work in iSafari).

It's unclear whether you want to use visualforce / apex webservice or would you rather stay within pure JS solution... Since you struggle with JS syntax error I'd recommend involving apex like in @Modem's answer. In future maintenance it's more likely that another developer will understand Apex but his JS skills might be not as good.

But - for the sake of completeness. As far as I know whole SOAP API is implemented (exposed?) in the AJAX toolkit. So the convertLead() call is available too.

Here's the example:

var account = new sforce.SObject("Account");
account.Name = "convert lead sample";
account.Phone = "2837484894";
result = sforce.connection.create([account]);
account.Id = result[0].id;

var lead = new sforce.SObject("Lead");
lead.Country = "US";
lead.Description = "This is a description";
lead.Email = "someone@somewhere.com";
lead.FirstName = "first";
lead.LastName = "last";
lead.Company = account.Name;
result = sforce.connection.create([lead]);
lead.Id = result[0].id;

var convert = new sforce.LeadConvert();
convert.accountId = account.Id;
convert.leadId = lead.Id;
convert.convertedStatus = "Qualified";

result = sforce.connection.convertLead([convert]);
if (result[0].getBoolean("success")) {
  log("lead converted " + result[0]);
} else {
  log("lead convert failed " + result[0]);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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