Back

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.

closed

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:

@RestResource(urlMapping='/Lead/*')

global with sharing class RestLeadConvert {            

    @HttpGet

    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();

        lc.setLeadId(leadId);

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

        lc.setConvertedStatus(convertStatus.MasterLabel);           

        Database.LeadConvertResult lcr ;

        try{

            lcr = Database.convertLead(lc);

            system.debug('*****lcr.isSuccess()'+lcr.isSuccess());            

            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.

@isTest

public class RestLeadConvertTest{

    static testMethod void testHttpGet() {

        Lead l = new Lead();

        l.FirstName = 'First';

        l.LastName = 'Last';

        insert l;

        Test.startTest();

        RestRequest req = new RestRequest();

        RestResponse res = new RestResponse();

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

        req.httpMethod = 'GET';

        RestContext.request = req;

        RestContext.response= res;

        RestLeadConvert.doGet();

        Test.stopTest();

    }

}

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

Related questions

+1 vote
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...