+ Start a Discussion
vinod kumar 164vinod kumar 164 

lead conversion possible using HttpWebRequest(Rest Api)

Hi

We are new to the salesforce and we are implementing to integrate our app data to salesforce.And we are in confuse to work with SOAP API or REST API which is better because when we try to work with Rest API we are succesed in creating an Account, Lead but when we try to convert a lead to account but we failed.And also we read few post reagarding Rest Api saying that Rest Api doesnot support lead conversion is it right?
If it support lead conversion would you please let us know how to work on it using httpwebrequest.

And also let us know which Api is the best one to work with salesforce to integrate to our App.

Regards
Vinod



 
Best Answer chosen by vinod kumar 164
Krishna SambarajuKrishna Sambaraju
You need to create above mentioned class in Salesforce, ask your salesforce developers to do it for you and use the end point and body as described in the solution to set it to HttpWebRequest in your c#.net.
 

All Answers

Krishna SambarajuKrishna Sambaraju
You need to create a custom class to convert the lead using Rest API. Here is an example.
@RestResource(urlMapping='/convertlead/*')
global class LeadConvert{
  @HttpPost
    global static string convertLead(string leadId)
    {
        Lead lead = [select Id from Lead where Id = :leadId];
        Database.LeadConvert lc = new Database.LeadConvert();
        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
        lc.setLeadId(lead.Id);
        lc.setConvertedStatus(convertStatus.MasterLabel);
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        if (lcr.isSuccess())
        {
            return 'success';
        }
        else
        {
            return 'failed';
        }
    }
}
Your endPoint should be your_instance_url/services/apexrest/convertlead, and the request body should be {"leadId" : lead_id_goes here}.
Hope this is helpful.
 
vinod kumar 164vinod kumar 164
Hi

Thanks for Quick Response.In the above code as you shown we are unable to get how to create a custom class because as we are work with c#.net code to inetgrate the Sales force to our App.Could please tell us briefly to work with above code in c#.net.

And One more Question we asked regarding working with Salesforce which one is best Api that is SOAP API or REST API?

Regards
Vinod
Krishna SambarajuKrishna Sambaraju
You need to create above mentioned class in Salesforce, ask your salesforce developers to do it for you and use the end point and body as described in the solution to set it to HttpWebRequest in your c#.net.
 
This was selected as the best answer
Krishna SambarajuKrishna Sambaraju
Here is a documentation on different APIs supported by salesforce. You have to decide which one is best for you.
https://developer.salesforce.com/blogs/tech-pubs/2011/10/salesforce-apis-what-they-are-when-to-use-them.html

I prefer REST API for its ease of integration and development.
Chris ZiebaChris Zieba
What is the difference between using the custom class as you describe and just setting isConverted via the rest api on the lead?
Krishna SambarajuKrishna Sambaraju
The field IsConverted is not updatable. So you cannot set this field via rest api. on lead.
Karim BenabdallahKarim Benabdallah
Thanks Krishna for the code snippet.
I am able to use it in a sandbox but when deploying to prod its complaining about missing code coverage. Could you assist me on that end as well?
Thanks,
Krishna SambarajuKrishna Sambaraju
Hi Karim,

Here is the test class for code coverage.
@IsTest
public class LeadConvertTest {
	static testMethod void convertLeadTest()
    {
        Lead lead = new Lead(FirstName = 'Test', LastName = 'Name', Company = 'TestCompany');
        insert lead;
        string status = LeadConvert.convertLead(lead.Id);
        lead = [select IsConverted from Lead where Id = :lead.Id];
        system.assertEquals(true, lead.IsConverted);
    }
}
Deploy this test class along with the other class. Hope this helps.

Regards,
Krishna.