Is there any REST service available in Salesforce to Convert Leads into Accounts

0 votes
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.
Apr 1, 2022 in SalesForce by surbhi
• 3,820 points
1,624 views

1 answer to this question.

0 votes

Yes and we resolved this by creating an Apex class for REST call. 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>

This test will give you around 93% of coverage.

@isTest
public class RestLeadConvertTest{

    static testMethod void testHttpGet() {
        Lead l = new Lead();
        l.FirstName = 'First';
        l.LastName = 'Last';
        l.Company = 'Unit Test';
        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();
    }

}

Hope this helps!

Check out Salesforce online training and become certified.

Thanks!

answered Apr 5, 2022 by CoolCoder
• 4,420 points

edited Jun 27, 2023 by Khan Sarfaraz

Related Questions In SalesForce

0 votes
1 answer

Json response to be deserialized in Apex salesforce lightning

Because some fields in Apex Salesforce are ...READ MORE

answered Mar 2, 2022 in SalesForce by surbhi
• 3,820 points
3,531 views
0 votes
1 answer

Apex Class and Apex Trigger is invisible in Salesforce Developer Console

You'll need to create a new developer ...READ MORE

answered Mar 8, 2022 in SalesForce by anonymous

edited Jun 19, 2023 by Khan Sarfaraz 2,677 views
0 votes
1 answer

In Salesforce what is the difference between Page Layouts and Visual force Pages?

A built-in standard mechanism for displaying information ...READ MORE

answered Mar 9, 2022 in SalesForce by CoolCoder
• 4,420 points
763 views
0 votes
1 answer

Trigger to Count records in self relationship salesforce

You should only use triggers if you ...READ MORE

answered Mar 9, 2022 in SalesForce by CoolCoder
• 4,420 points
1,257 views
+2 votes
2 answers

Salesforce Interview questions

Here are some questions very important for ...READ MORE

answered Jan 11, 2019 in Career Counselling by Suresh
• 720 points
3,347 views
0 votes
1 answer

How to connect to salesforce from tableau?

Hi, follow these steps to connect to Salesforce: 1. ...READ MORE

answered Mar 25, 2019 in Tableau by Cherukuri
• 33,030 points
1,019 views
0 votes
1 answer

Power BI - Salesforce

Hi, Follow below steps: 1. Go to Data source. 2. ...READ MORE

answered Mar 25, 2019 in Power BI by Cherukuri
• 33,030 points
714 views
0 votes
2 answers

What is the best training for Salesforce ADM-201 Exam?

Hi @Vardhan, I took Edureka's Salesforce Online Training, ...READ MORE

answered Jun 3, 2021 in Others by Jaya
• 140 points

edited Dec 22, 2021 by Soumya 708 views
0 votes
2 answers

Creating Many to Many relationship between the accounts records in salesforce

In Salesforce, it is not possible to ...READ MORE

answered Jun 19, 2023 in SalesForce by Khan Sarfaraz
• 700 points
1,279 views
0 votes
1 answer

How to Update RecordTypeId field in Lightning record form in salesforce?

A critical action that messes everything up ...READ MORE

answered Mar 3, 2022 in SalesForce by CoolCoder
• 4,420 points
3,316 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP