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
Mahesh KGMahesh KG 

How to Create an Account in Salesforce using REST API

Can you please let me know how to create an Account in Salesforce using the REST API.
i also need to know where i can find my clientId and Client Secret to get the access token
Sankaranarayanan VenkatachalamSankaranarayanan Venkatachalam

Hi Mahesh,

Please find the steps below to create an Account using REST API:

* Login to Workbench (https://workbench.developerforce.com/login.php).
* Navigate to 'Utilities -> REST Explorer'
* Select the 'Method' as 'POST'
* Paste the below line in the text box:

/services/data/v46.0/sobjects/account

* Click on 'Headers' and paste the below:

Content-Type:application/json
Accept: application/xml

* In the Request Body, paste the below content:

{
  "Name" : "Account from REST API",
  "ShippingCity" : "Chennai"
}
* Click 'Execute'. The Account will be created.

Refer Trailhead (https://trailhead.salesforce.com/en/content/learn/modules/api_basics/api_basics_rest) module for more information about REST API.
Refer this link  (https://stackoverflow.com/questions/12794302/salesforce-authentication-failing/29112224#29112224) for the steps to know your Client ID and Client Secret.

Please mark this answer as 'Best Answer' if this answers your question.

Best Regards.
Mahesh KGMahesh KG
Can you tell me how to Authentication is working? are you using Ouath?  i need to implemen the same with my java code? can u tell me the flow for the account creation?
 
Sankaranarayanan VenkatachalamSankaranarayanan Venkatachalam
If you want to hit Salesforce using REST API and want to create an Account, then you have to follow the below steps:

* Create a Connected App (for Authentication). Reference. (https://trailhead.salesforce.com/content/learn/modules/connected-app-basics/learn-about-connected-apps)
* Create a REST service Apex Class in Salesforce, which gets the necessary parameters (like Name, Country etc.) for Account creation, as shown in the 'Create a Record with a POST Method' section here (https://trailhead.salesforce.com/en/content/learn/modules/apex_integration_services/apex_integration_webservices).
* Perform the callout using the Client ID and Client Secret you will get from the Connected App.
* Once the connection is established, call the REST Service class, which will create the record in Salesforce.

Please mark this answer as 'Best Answer' if this answers your question.

Best Regards.
Mahesh KGMahesh KG
Should we create a new Apex class for the Account? i think there is already a controller mapping for Account can i use the same? 
Sankaranarayanan VenkatachalamSankaranarayanan Venkatachalam
Yes, a new class should be created for Account creation in Salesforce. Something like below:
@RestResource(urlMapping='/Account/*')
global with sharing class MyRestResource {
    @HttpPost
    global static Id createAccount(String name, String county) {
        Account restAcc = new Account(Name=name, ShippingCountry=country);
        insert restAcc;
        return restAcc.Id;
    }
}
Reference: https://trailhead.salesforce.com/en/content/learn/modules/apex_integration_services/apex_integration_webservices
 
Mahesh KGMahesh KG
Thank you for the Help. 

i have below fields that we are using today on the salesforce for creating the order. Can you please let me know the corresponding objects that i need to pass them to my controller. i would like to know what is the object that i need to send to create the account in salesforce.

Please find the below list of fields highlighted. User-added imageUser-added image
i have below fields like AccountName,ParentAccount etc,. i need to know how i need to send them as the object to create the account and how the values should be sent to the dropdown?

 
Deepali KulshresthaDeepali Kulshrestha
Hi Mahesh,

Supply the required field values in the request data, and then use the POST method of the resource. The response body will contain the ID of the created record if the call is successful.

Example of creating a new Account

Method :- Post
URL:- /services/data/v36.0/sobjects/Account/

Example Request Body for creating an account:- 

{ "Name" : "Express Logistics and Transport" }

Example response body after successfully creating a new Account

{ "id" : "0019000001hE8apAAC", "success" : true, "errors" : [ ] }

I suggest to visit these links, it will help you in understand how to create a record using API

https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_create.htm

https://www.youtube.com/watch?v=ry_j7tnx298

There is a specific Trailhead for this:-

https://trailhead.salesforce.com/en/content/learn/modules/api_basics/api_basics_soap?trail_id=force_com_dev_intermediate


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Sankaranarayanan VenkatachalamSankaranarayanan Venkatachalam

Hi Mahesh,

Based on your screenshots, you want to create 'Account' object in Salesforce. The Object API name is 'Account' itself. You can pass the values to your REST Service method (refer my last comment). For dropdown values, you can pass as 'String' and assign them directly. If they are constants, then you can assign them in the REST Service class directly.

Please mark this answer as 'Best Answer' if this answers your question.

Best Regards.

Mahesh KGMahesh KG
I need to integrate the REST service Apex class created with java for creating the account object? can u insist with the right APIS?