Step By Step Guide To Getting Started with Salesforce REST API using Java In VS Code

Salesforce REST API using Java In VS Code

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…

NOTE: Certain sections of the guide will appear as locked in the free preview. You can download the unlocked version of the guide in PDF format by subscribing to our “All Access” Pass through the link below.

Not an “All Access” Pass Member Yet?

Get Download Access to this & 150+ More Step-by-Step Guides with “All Access” Pass. A simple and single plan to access our entire library of courses, guides, workshops & masterclasses on Salesforce.

Introducing Mastering Salesforce APIs Training Course

Liked this post? We are now offering a complete self-paced, video-based training course on Mastering Salesforce APIs. The course comes with downloadable step-by-step PDF guides, unlimited access, free upgrades, community discussion area, free preview and more. For more details, please click here.

Here is the URL to get the code used in this guide

  1. Java program to connect to test connecting to Salesforce 
  2. 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

References & Useful URLs

  1. 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’)

19 thoughts on “Step By Step Guide To Getting Started with Salesforce REST API using Java In VS Code”

  1. Hi,
    Thanks for writing this helpful article. I tried running the connection code in VS Code. I get the following error. Any suggestions? Thanks
    Caused by: java.lang.ClassNotFoundException: Main
    PS C:\Users\artof\salesforce_rest_api\REST_API> c:; cd ‘c:\Users\artof\salesforce_rest_api\REST_API’; & ‘C:\Program Files\Eclipse Adoptium\jdk-11.0.19.7-hotspot\bin\java.exe’ ‘@C:\Users\artof\AppData\Local\Temp\cp_1vd9jhou9mfy484emnchrjuve.argfile’ ‘Main’
    Error: Could not find or load main class Main
    Caused by: java.lang.ClassNotFoundException: Main
    PS C:\Users\artof\salesforce_rest_api\REST_API>

  2. 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!

  3. Hello,
    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?

  4. 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?

    1. 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

      Regards, Ashish

  5. 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?

  6. 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?

    1. Dear Chris,

      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,
      Ashish

      1. Hi,

        .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

        1. Ashish Agarwal

          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.

  7. I am getting below error:
    Project ‘SalesforceResttest’ is missing required library: ‘httpcore5-5.0.jar’

    Please help

  8. Good article
    If anybody having issue with Login / Authentication then try this:
    PASSWORD = URLEncoder.encode(PASSWORD, “UTF-8”);

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
Introducing All Access Pass