Updating Salesforce Records Using External IDs with jsforce

Aug 10, 2023

In the world of Salesforce development, there are various tools and libraries that can empower you to achieve efficient data management. One such powerful tool is jsforce, a popular JavaScript library that provides a robust interface for interacting with Salesforce. In this blog, we'll delve into how you can leverage jsforce to update Salesforce records, specifically using external IDs. We'll walk through the steps and provide code examples to make this process clear and actionable.

Why External IDs Matter

External IDs are incredibly useful in scenarios where you need to identify and update records from external systems or when you have a unique identifier already established in Salesforce. This makes updating records seamless, and you don't have to worry about obtaining record IDs through queries. It's especially beneficial when integrating Salesforce with other applications, maintaining data integrity, or executing data migrations.

Setting Up Your Environment

Before we dive into the code, make sure you have Node.js installed on your system. If not, download it from the official website (https://nodejs.org/) and follow the installation instructions from  JSforce Documentation

Updating Records with External IDs using jsforce

Let's say you want to update a Salesforce Case record using an external ID CaseNumber . Here's a step-by-step guide:

Authentication: Start by authenticating your connection to Salesforce using your credentials.

var jsforce = require('jsforce');
var conn = new jsforce.Connection({
  // you can change loginUrl to connect to sandbox or prerelease env.
  // loginUrl : 'https://test.salesforce.com'
conn.login(username, password, function(err, userInfo) {
  if (err) { return console.error(err); }
  // Now you can get the access token and instance URL information.
  // Save them to establish connection next time.
  // logged in user property
  console.log("User ID: " + userInfo.id);
  console.log("Org ID: " + userInfo.organizationId);
  // ...

Update Record: Use the sobject.update() method and specify the object type, the external ID field, the external ID value, and the fields you want to update.

  Status: 'Closed', // field you want to update
  Subject: 'Test from JSFORCE using CaseNumber as ExtId' // another field you want to update
}, 'CaseNumber/123456', function(err, ret) {
  if (err || !ret.success) { return console.error(err, ret); }
  console.log('Updated Successfully : ' + ret.id);

Note: syntax to pass ExternalId is ExtId/<<ext-id-value>> in our case its CaseNumber/123456

Replace CaseNumber with the actual name of your external ID field you looking for and set the value accordingly. You can update other fields as needed.

Considerations and Best Practices

  • Ensure that the external ID is unique within the object.
  • Implement proper error handling in your code to gracefully manage any issues that may arise during updates.
  • Test your code in a controlled environment before performing updates in a production environment.

Phanindra Mangipudi

Salesforce, Lightning Web Componets, Node.Js, Angular 2+, Bootstrap