Salesforce CMS: Image Management Automation Using REST API and SOQL

Learn how to automate retrieve, create, and update of images in Salesforce Enhanced CMS using SOQL, REST APIs, and the Connect Batch API — with a focus on managing product images efficiently.
Summary
Introduction
Salesforce Enhanced CMS is a powerful content management system that allows users to create and manage content for their commerce websites. However, creating images manually can be time consuming and error prone. In this blog post, we will explore how to automate the bulk creation of images in Salesforce Enhanced CMS using Salesforce Enhanced CMS APIs.
The postman collection used in this blog post can be found here.
We will cover the following use cases in this blog post:
- Creating a new image.
- Updating an existing image
- Publishing an image
- Retrieving an image
The high-level steps involved in automating the creation of images in Salesforce Enhanced CMS is described in the below diagram.
Retrieving an Image
To begin, we’ll retrieve an image from Salesforce Enhanced CMS. While there isn’t a direct API endpoint to fetch an image using its externalId
, we can still accomplish this using a SOQL query. Salesforce’s standard REST APIs allow us to execute SOQL queries, which we’ll leverage to retrieve the ManagedContent
record and its related ManagedContentVariant
records.
The ManagedContentVariant
contains the record ID necessary to update the image.
In Salesforce, every managed content item (such as an image) has at least one variant. For instance, consider a content item of type “news” (like a blog article) with English as the default language. When translated into other languages—such as Spanish, Japanese, or French—a new variant is created for each translation.
However, to keep things simple, we’ll assume that each product image has only a single variant.
Note: Please consider the official Salesforce constraints when using the SOQL over REST API
Sample Request
GET /services/data/v61.0/query/?q=select+Id,+Name,+(Select+id+from+ManagedContentVariants+)+from+ManagedContent+where+ExternalId+=+'external_id_1'+OR+ExternalId+=+'external_id_2'
In above request, we are retrieving the ManagedContent records using externalIds. We will talk about the externalId
in the next sections.
Sample Response
If the image exists in the system, the response will include details similar to the example below. Specifically, it will contain the ManagedContentVariant
record ID, which is key for performing updates to the image.
Bulk Create Images
If an image doesn’t exist in the system, we can create it using the CMS Contents API. We will use the SF connect batch API to bulk create the records.
Note: SF supports only up to 25 sub-requests in a single batch update request.
Sample Request
POST services/data/v61.0/connect/batch
In the above request, we’re creating a new image by providing an image URL, title, and alt text. The request also includes an externalId
, which serves as a unique identifier for the image within your system. This externalId
can later be used to query for the image. Additionally, we’re setting the apiName
to match the externalId
for consistency.
The endpoint returns a response for each sub-request. If the creation is successful (HTTP status 201 Created), the response will include the ID of the newly created managed content, accessible under the managedContentId
field. We’ll need this ID in the next step when we publish the image.
Bulk Update Existing Images
If an image exists in the system, we can update it using the ManagedContentVariant record ID. ManagedContentVariant can be updated using the CMS Contents Variant API. We will use the SF connect batch API to bulk update the records.
Note: SF supports only up to 25 sub-requests in a single batch update request.
Sample Request
POST services/data/v61.0/connect/batch
In this request, we’re updating an existing image record with a new image URL, title, and alt text.
As with creation, the endpoint responds to each sub-request individually. If the update is successful (200 OK), the response will include the ID of the updated managed content under the managedContentId
field. We’ll use this ID in the next step when publishing the image.
Publish Images
We need to publish the image to make it visible to the end-users. We can publish the image using the ManagedContent record ID. We can use the CMS Contents Publish API to publish the image.
Sample Request
POST /services/data/v61.0/connect/cms/contents/publish
We need to pass the ManagedContent record IDs in the request body to publish the images.
So, we have successfully created, updated, and published images using the Salesforce CMS API. The next step could be to assign the images to the product records. For this, I would recommend looking into how to automate creation of the Product Media records. As this is a standard SObject, you can use standard Salesforce APIs to create and update the records.