Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Salesforce by (11.9k points)

We have to convert Leads to accounts via REST -OAuth calls. We are able to create, update(Edit) and Detail Lead fields but not able to convert them.

We found the same is possible via SOAP API but we are following REST OAuth only.


1 Answer

0 votes
by (32.1k points)
selected by
Best answer

This can be resolved by creating an Apex class for REST call. 

A sample code is this:


global with sharing class RestLeadConvert {            


    global static String doGet() {

        String ret = 'fail';

        RestRequest req = RestContext.request;

        RestResponse res = RestContext.response;

        String leadId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);              

        Database.LeadConvert lc = new Database.LeadConvert();


        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];


        Database.LeadConvertResult lcr ;


            lcr = Database.convertLead(lc);


            ret = 'ok';


        catch(exception ex){

            system.debug('***NOT CONVERTED**');           


        return ret;



And you can use this call by

<Your Instance URL>/services/apexrest/Lead/<LeadId>

You will get around 93% of coverage with this test.


public class RestLeadConvertTest{

    static testMethod void testHttpGet() {

        Lead l = new Lead();

        l.FirstName = 'First';

        l.LastName = 'Last';

        insert l;


        RestRequest req = new RestRequest();

        RestResponse res = new RestResponse();

        req.requestURI = '/Lead/' + l.Id;

        req.httpMethod = 'GET';

        RestContext.request = req;

        RestContext.response= res;





 Go for this in-depth job-oriented Salesforce admin certification now!

Related questions

+1 vote
1 answer
0 votes
1 answer

Browse Categories