function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
raginireddy thoutireddyraginireddy thoutireddy 

How to create Accounts and contacts in salesforce from web page using REST API

Hello,

I have a scenario where if a customer fills out the information on a web page then it should create an Account and contact in salesforce. I have been researching into this and the options  have found are 
1) Create Web-to-lead and then write a trigger to convert them to Accounts and Contacts.
2) Using REST API create accounts in salesforce.

Our Organization looks to go with the second option. I am not sure how to do it exactly. Can anyone please direct me to achieve this? 
Raj VakatiRaj Vakati
You can use the below standard salesforce rest api ..


 
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_create.htm
 
curl https://yourInstance.salesforce.com/services/data/v20.0/sobjects/Lead/ -H "Authorization: Bearer token -H "Content-Type: application/json" -d "@lead.json"

 
NagendraNagendra (Salesforce Developers) 
Hi RaginiReddy,

Go through this sample code to implement lead conversion through REST API Call.

You can achieve this by this by creating an Apex class for REST call.

Sample code is below for your reference. 
@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';
        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.

Kindly mark this as solved if the reply was helpful so that it gets removed from the unanswered queue which results in helping others who are facing a similar issue.

Thanks,
Nagendra


 
raginireddy thoutireddyraginireddy thoutireddy
Thank you Nagendra,

I am sorry if this looks silly but where do I need to map my web page url to get the details and create accounts in salesforce.