Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

salesforce

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.

like image 308
Hitesh Avatar asked Oct 29 '13 15:10

Hitesh


People also ask

How do I convert leads to accounts in Salesforce?

Click the Lead tab. Open the Lead record which needs to be converted and click Convert. In the Account Name field, select Attach to Existing Account (for example: Big cars Account) Complete the other details on the page as per the requirement and click Convert.

Does Salesforce have a REST API?

REST API is one of several web interfaces that you can use to access your Salesforce data without using the Salesforce user interface. With API access, you can perform operations and integrate Salesforce into your applications as you like.

Can you mass convert leads in Salesforce?

Salesforce should provide a mass lead conversion button/tool as standard functionality, as it is currently only possible to convert one Lead at a time. The only workaround for this is to export and reimport records using DataLoader or write an Apex Trigger, both which are time consuming.


1 Answers

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

}
like image 104
Hitesh Avatar answered Jan 03 '23 23:01

Hitesh