How do you connect to Salesforce from an external application, say, for example Java? If you Google for the information, you will quickly learn that Salesforce provides different methods and different APIs. But what you will probably not find is a step-by-step guide teaching you exactly how to do it. In this blog post, I have stitched together a complete step-by-step instructions with screenshots on how to download, install, configure and use Salesforce REST API to connect from a java program and execute some basic transactions (query, insert, update, delete records). In less than 45 minutes, you will have a fully functional java program that will connect to Salesforce and execute transactions in Salesforce.
The guide includes downloading & installing necessary HttpClient and JSON frameworks that is required to work with Salesforce REST APIs. This is followed by referencing the JAR file in your java program, logging on to Salesforce from the java program, querying and manipulating data. Here is an overview of steps that needs to be completed and is covered in the guide
- Install Visual Studio Code
- Install Java
- Install HTTP Client
- Install JSON Framework
- Create Connected App in Salesforce
- Prepare VS Code for Executing Java Program
- Prepare Java Environment for REST API Execution
- Test Connecting to Salesforce from Java
- Execute Transactions From Java Using REST API
Even if you are not a developer, you should be able to follow the guide and have working java program at the end of it. Sounds interesting? Download the following presentation and let’s get started…
Here is the URL to get the code used in this guide
- Java program to connect to test connecting to Salesforce
- Java program to query, create, update & delete records in Salesforce
Not only this, you can go a step further and use other RESTful APIs that Salesforce provides to extract and manipulate data and metadata. For a list of different APIs along with a brief explanation on which API should be used when, please navigate to URL https://help.salesforce.com/HTViewHelpDoc?id=integrate_what_is_api.htm
|Liked this guide? We are now also offering a complete course on Mastering Salesforce APIs. This is a training course designed for all Salesforce Professionals (Admins, Consultants, Developers & Architects) to understand how Salesforce APIs work. For more details please click here.|
References & Useful URLs
- Salesforce REST API Developer Guide – https://www.salesforce.com/us/developer/docs/api_rest/
(Note: This step-by-step guide is created based on my earlier blog post https://www.asagarwal.com/step-by-step-guide-to-get-started-with-salesforce-rest-api-using-java/, which showed how to execute Java program from Eclipse. In this guide, I have replaced Eclipse with VS Code as Salesforce has discontinued its Force.com IDE on Eclipse and now recommends developers to use VS Code with ‘Salesforce Extension Pack’)
17 thoughts on “Step By Step Guide To Getting Started with Salesforce REST API using Java In VS Code”
I am getting the error. 400
“Error authenticating to Force.com: 400”
Can you try connecting through Postman and see if you are able to do that? Here is a blog post on how to do that – https://www.asagarwal.com/a-60-minutes-step-by-step-diy-guide-to-salesforce-rest-api-for-non-developers/
I must have read a dozen different sites (including at salesforce.com) and tried at least 2 or 3 libraries to get this working. This was the first article that helped me actually get everything working correctly (with no extra junk I didn’t want/need). Thanks!
You are welcome, Mike. Thanks for your feedback. Glad to note that you were able to get it working following the post.
I am facing one more issue: While setting up the VS Code with a Maven project for Java, when I do this from my personal laptop, it works fine, because the dependencies (org.apache.httpcomponents.httpclient and org.json.json) are directly downloaded from Internet. However, when using such Java Maven project from my office network, I am getting certificate error in VS Code.
After I set the appropriate java.cacerts file in the java/lib/security folder, I am getting error trustanchors parameter must be non-empty. After googling online, I see that this error is because of password not specified anywhere for the truststore (cacerts file).
Could you please suggest how do we configure the truststore (cacerts) and its password in VS Code?
Apologies Vaibhav, I don’t have much idea about it. You can try posting this query in Salesforce Developers Forum at URL https://developer.salesforce.com/forums#!/feedtype=RECENT&criteria=ALLQUESTIONS& or in one of the Trailblazer Community Groups at URL https://trailblazers.salesforce.com/_ui/core/chatter/ui/ChatterPage
This is a brilliant tutorial! To the point, and indeed able to create a working Java project that performs CRUD operations in Salesforce.
Couple of questions:
1. could you provide the link of the salesforce documentation where we can check these REST operations in more detail, and also check what all other operations are available?
2. Also, are there any SOAP services of Salesforce which also can be triggered by Java project to perform similar CRUD operations? If yes, is there any blog that you have on it, or any documentation of Salesforce?
Hi Vaibhav, thanks for your feedback and happy to note that you found the tutorial helpful.
1. With regards to Salesforce documentation on REST operations in more details, you may want to check out the Salesforce REST API Developer Guide at URL https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_what_is_rest_api.htm . You can also download the PDF copy of this guide from the mentioned URL
2. With regards to SOAP Services, I do have another step-by-step guide on the same at URL https://www.asagarwal.com/step-by-step-guide-to-get-started-with-salesforce-soap-api-using-java/. Though this guide is a little old and uses Eclipse & Force.com IDE that Salesforce has discontinued now (I hope to the update this guide in near future). But this should still give you a good idea about how to make SOAP API calls from Java to Salesforce and query, insert, update and delete the records. You may also want to refer to Salesforce’s SOAP API Developers guide at URL https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_quickstart_intro.htm
This may be the best set up guide ever. Every person who wants to use the Salesforce REST API and Visual Studio Code should get your blog.
I have a question re .classpath in a Java project. How can I ask you about it?
Wow! Finally all the steps in one place. I wish I had found this earlier. Thank you for taking the time to so clearly display all the steps. I hope that everyone who wants to connect to Salesforce with the REST API finds your blog.
I have one problem, however, concerning the .classpath. When I create a Java project in Visual Studio (No build tools option) there is no .classpath. In the src folder there is App.java. But there is no .classpath anywhere. Any thoughts?
Thanks for your feedback.
If don’t see the classpath in the directory, you may want to try cleaning up the Java workspace by executing the “Java: Clean the java language server workspace” command from the Command Palette (⇧⌘P) and try creating the Java project again. See if this helps.
You may also want to refer to tutorial “Getting Started with Java in VS Code” at URL URL https://code.visualstudio.com/docs/java/java-tutorial to ensure that your VS Code is setup correctly for Java.
With warm regards,
.classpath is not creating while creating project from “No buld”.
Also, when cleaning java project extension not finding the command.
an error is coming as java.clean.workspace not found
Hi Jai, have you tried cleaning the Java workspace by executing the “Java: Clean the java language server workspace” command from the Command Palette and try creating the Java project again? If this still hasn’t resolved your problem, you will have to reach out to other folks in a VS Code forum. Sorry, I won’t be of much help here.
Thanks Ashish for taking time and sharing your knowledge.
I am getting below error:
Project ‘SalesforceResttest’ is missing required library: ‘httpcore5-5.0.jar’
If anybody having issue with Login / Authentication then try this:
PASSWORD = URLEncoder.encode(PASSWORD, “UTF-8”);
Thanks for the tip Aslam.