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 in SalesForce by surbhi
• 3,810 points
50 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();
    }

}
answered Apr 5 by CoolCoder
• 4,360 points

Related Questions In SalesForce

0 votes
1 answer

Json response to be deserialized in Apex salesforce lightning

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

answered Mar 2 in SalesForce by surbhi
• 3,810 points
237 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 in SalesForce by anonymous
93 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 in SalesForce by CoolCoder
• 4,360 points
38 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 in SalesForce by CoolCoder
• 4,360 points
109 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
1,858 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,010 points
301 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,010 points
181 views
0 votes
1 answer

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

Hi @Vardhan, I took Edureka's Salesforce Training that covers all ...READ MORE

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

edited Dec 22, 2021 by Soumya 213 views
0 votes
1 answer

Creating Many to Many relationship between the accounts records in salesforce

In Salesforce, you'll need a joiner object ...READ MORE

answered Mar 17 in SalesForce by CoolCoder
• 4,360 points
25 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 in SalesForce by CoolCoder
• 4,360 points
429 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP