CRM developers typically need to integrate Salesforce with Node.js apps. One well-known library for this purpose is jsForce, which provides a comprehensive set of tools for interacting with Salesforce data and performing CRUD tasks effectively.

Let’s dive into the blog, where you will understand the use of jsForce to execute key CRUD tasks within Salesforce, as well as how to make the most of the Composite API to handle multiple actions in one request. Whether you’re new to Salesforce development or an experienced developer, this guide will help you seamlessly integrate Salesforce into your Node.js apps.

Seamless Salesforce API Integration with jsForce

jsForce is a well-known open-source JavaScript library that empowers developers to communicate with various Salesforce APIs, such as REST, Bulk, and Metadata APIs. With jsForce, users can handle Salesforce records, run SOQL queries, and interact with objects directly from their Node.js applications, enhancing the integration process.

Key Features of jsForce:

jsForce offers powerful features that allow developers to integrate Salesforce more easily. It supports easy authentication methods such as OAuth 2.0, allows for complete CRUD operations on Salesforce records, and offers extensive querying capabilities with SOQL. Furthermore, its Composite API functionality improves performance by performing several tasks in a single request.

Below are the key features of jsForce:

Simple Authentication: Support for OAuth 2.0 and basic username-password authentication methods. 

CRUD Functionality: Full CRUD operations—create, read, update, and delete—on Salesforce records. 

Advanced-Data Querying: Efficiently query and retrieve Salesforce data using SOQL.

Composite API: Execute multiple actions in a single request, improving batch processing and overall performance. 

Setting Up jsForce in Node.js: A Step-by-Step Guide

Before delving into the intricacies of CRUD operations, the initial step involves setting up jsForce in your Node.js environment. Ensure you have the required Salesforce account credentials for connecting to your specific Salesforce instance.

Step 1: To integrate jsForce into your Node.js project, initiate the installation process by running the npm command “npm install jsforce.”

npm install jsforce

Step 2: To authenticate with Salesforce using jsForce for OAuth authentication in Node.js, you’ll use the login method, which supports the OAuth 2.0 Web Server Flow. This flow involves redirecting the user to the Salesforce login page, where they log in and grant permissions. Subsequently, Salesforce redirects the user back to your application with an authorization code, which your app exchanges for an access token and refresh token.

Below is a Node.js example demonstrating OAuth authentication using jsForce:

OAuth 2.0 Flow with jsForce

Step-by-Step Guide to Get Client ID and Client Secret in Salesforce

Creating a Connected App in Salesforce is essential for obtaining the Client ID and Client Secret required for OAuth authentication. Follow this step-by-step guide to set up your app and securely access these credentials.

Step 1: First, log into your Salesforce account with an administrator or developer profile.

Step 2: In Salesforce Lightning, click on the gear icon (top right corner) → Setup → In the Quick Find box, type App Manager.

1

Step 3: In App Manager, click on the New Connected App button.

2

Step 4: Fill out the required fields for your new connected app:

3
  • Connected App Name: A name for your app, e.g., “My Node.js App”.

  • API Name: This will be auto-generated, but you can change it if needed.

  • Contact Email: Enter your email address.

OAuth Settings:

Under the API (Enable OAuth Settings) section:

  • Enable OAuth Settings: Check this box to enable OAuth for your connected app.
  • Callback URL: This is the URL Salesforce will redirect to after the user authorizes the app. For local development, it can be something like http://localhost:3000/oauth/callback. Make sure the URL matches what you use in your Node.js app.
  • Selected OAuth Scopes: Select the required OAuth scopes for your application. Common scopes include:
    • Full access (full)
    • Perform requests on your behalf at any time (refresh_token, offline_access)
    • Access and manage your data (api)

You can add multiple scopes based on your requirements, but full and api are typical for most use cases.

Click Save once you’ve completed the form.

Step 5: After creating a Connected App in Salesforce, you will be redirected to the Connected App details page, where you can access the following crucial information:

  • Consumer Key (Client ID): This unique identifier is assigned to your connected app and is utilized in the OAuth flow to differentiate your app.
  • Consumer Secret (Client Secret): This secret key is paired with the Consumer Key and is essential for authentication via OAuth. It should be handled securely to prevent unauthorized access.

Note: It’s important to remember that the Consumer Secret is only visible when you initially create the Connected App or when you view the app after creation. If you lose the Consumer Secret, you’ll need to regenerate it from the app settings.

Once the Connected App is created, it may take a few minutes, typically around 5–10 minutes, for the app to be fully accessible and active across all environments in Salesforce. During this period, you may encounter difficulties when attempting to authenticate with the app until it is fully propagated.

CRUD Operations in Salesforce Using jsForce

Learn how to perform CRUD operations in Salesforce using jsForce, including creating, reading, updating, and deleting records. Explore advanced capabilities like leveraging the Composite API for efficient bulk operations in a single API call.

Step 1: To create a new record in Salesforce, we use the create method on the sobject API. For example, let’s create a new Account record.

Here, we create an Account with the name “Acme Corporation” and the industry set to “Technology.” After the creation, the result.id will provide the unique ID of the newly created record.

Step 2: To read data, you can query Salesforce using SOQL. Here’s how to fetch records of the Account object.

This query will retrieve all Account records where the Industry is “Technology.” You can modify the SOQL query as per your requirements.

Step 3: To update an existing record, you need to know the Salesforce record’s ID. Here’s how you can update an existing Account:

In this case, the Industry field of the Account with the given ID is updated to “Finance.”

Step 4: To delete a record, use the destroy method by passing the record’s ID.

Using the Composite API for Bulk Operations

The Composite API in jsForce allows you to send multiple related requests in a single call, making it ideal for batch operations involving multiple records or dependent operations.
Here’s an example demonstrating how to use the Composite API for bulk operations in order to create a new Account and a related Contact in a single API call:

Where to Find the Correct API Version (vXX.X)

To find the available API versions in Salesforce, follow these steps:

a1

Step 1: In Salesforce Lightning, click on the gear icon located in the top-right corner of the screen. Select “Setup” from the dropdown menu.

Step 2: On the Setup page, locate the Quick Find box.

Step 3: Type “API” into the Quick Find box and select “API” from the available options.

Step 4: On the API page, you will find a section that lists different WSDL (Web Services Description Language) versions.

Step 5: Click on the respective WSDL version to open an XML document.

Step 6: In the XML document, you will find the API version specified at the top of the document.

By following these steps, you can easily locate and identify the available API versions in Salesforce through the Setup menu in Salesforce Lightning.

Breakdown of the Composite API Request:

  • allOrNone: If set to true, all operations must succeed, or none are executed. This ensures transactional integrity.
  • compositeRequest: Contains an array of individual operations. In this case, we’re creating both an Account and a Contact record.
  • referenceId: Used to reference other operations within the composite request. Here, we use @{ref1.id} to associate the Contact with the newly created Account.

Benefits of using Composite API in Salesforce

The Composite API in Salesforce offers a powerful way to streamline operations by combining multiple actions into a single request. This enhances efficiency, ensures data consistency, and simplifies complex workflows for optimized performance.

Below are the benefits of using Composite API in Salesforce:

  • Reduced API Calls: Performing multiple operations in a single request helps reduce the number of API calls required, which can result in lower overhead and improved performance.
  • Atomic Transactions: The Composite API allows you to group multiple operations as a single transaction. This ensures that either all operations succeed or all fail, maintaining data consistency across all related operations.
  • Ease of Use: The Composite API simplifies the process of managing complex, multi-step operations. By combining related requests into a single call, it streamlines the workflow and improves the overall user experience.

By leveraging the Composite API, you can achieve these benefits and optimize your application’s performance and data integrity while simplifying the handling of complex operations in Salesforce.

Related Articles
Expert Guide On Nominal XIRR Calculation Using LWC in Salesforce

When building financial tools within Salesforce, handling irregular cash flows with precision is a must. Whether you’re developing investment trackers, portfolio performance dashboards, or funding monitors, calculating Nominal XIRR (Extended Internal Rate of Return) becomes essential. In this blog, you’ll learn how to build a Lightning Web Component (LWC) in Salesforce that calculates Nominal XIRR […]

Read More
Beginners Guide to Making Your LWC Component Multilingual

If you’re wondering how to make your LWC component multilingual, you’re already ahead of the curve. Global users expect content in their native language, and Salesforce gives you the tools to deliver exactly that. With Custom Labels and the Translation Workbench, you can easily localize your Lightning Web Components without touching a single line of […]

Read More
Hands-On Guide Building a Drag-and-Drop Form Builder Using Lightning Web Components

Drag-and-drop interfaces are a user experience staple, and when paired with the power of Lightning Web Components (LWC), they unlock dynamic, highly interactive Salesforce components. In this blog post, we’ll walk you through how to build a custom drag-and-drop form builder in LWC. Users can drag form fields like text inputs and dropdowns into a […]

Read More
Please Make Blog Banner How to Create an Agentforce Enabled Scratch Org in Salesforce 100 1

Setting up an Agentforce-enabled scratch org is the most important step for developers who want to experiment with AI features such as Prompt Builder and Einstein GPT. Whether you’re developing the next generation CRM software or experimenting with how generative AI may improve user experience, a scratch org equipped with Agentforce is your playground. Follow […]

Read More
Step-by-Step Guide to Enhance Your LWC Development Workflow with the Logger API

Are you looking to streamline your LWC development workflow but confused about how to do it? Then you are in the right place. The Salesforce Logger API is a powerful tool that helps you monitor, debug, and improve code quality in real time.  This step-by-step guide walks you through how to set it up and […]

Read More
Enhancing Your Salesforce Experience with a Custom Activity Component

In today’s fast-paced business landscape, productivity hinges on streamlined workflows and intuitive interfaces. While Salesforce offers robust activity tracking capabilities, sometimes the standard components fall short of specific organizational needs.  That’s where a Custom Activity Component comes in—designed to tailor your activity management experience, boost user efficiency, and provide deeper insights into customer interactions. In […]

Read More
Our Location worldwide
Indian Flag India
3rd Floor, A-10, Pegasus Tower, Sector 68, Noida, Uttar Pradesh 201301 +91-1203117884
United States of America Flag USA
333 West Brown Deer Road Unit G – 366 Milwaukee WI, USA 53217 +1(262) 310-7818
United Kingdom Flag UK
7 Bell Yard, London, WC2A 2JR +44 20 3239 9428
Canada Canada
HIC Global Solutions INC
43 Lafferty Lane, Richmond Hill, L4C 3N8, CA +1(262) 310-7818