Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...