Scripting: How to populate set values
Summarize
Summary of Scripting: How to populate set values
This guide explains how to use an “On Configure/Reconfigure” blueprint enrichment script in ServiceNow CPQ to load and populate set values dynamically from an external source. The example demonstrates retrieving data from Salesforce via a SOQL query using an external connection, then preloading the results into a set field within a configuration blueprint. This approach enables you to automate and customize configuration data based on external system data, enhancing the configuration experience for users.
Show less
Prerequisites
- Enrichments must be enabled in your ServiceNow CPQ instance.
- External connections must be enabled and configured in your ServiceNow CPQ instance.
- A set must be created and associated with the blueprint.
- A text field must be associated with the set to receive data.
Key Features
- External Connection Setup: Configure an external Salesforce connection in the ServiceNow CPQ Admin under Utilities, specifying the integration type and SOQL query (e.g., retrieving contact last names filtered by AccountId).
- Enrichment Script Logic: The script initializes an empty array for set data, checks for existing data, then performs a SOQL call to Salesforce to retrieve records.
- Populating Set Values: For each retrieved record, the script maps the Salesforce contact’s LastName to a field in the set and adds it to the set data array.
- Dynamic Configuration: After processing, the updated set data is assigned to the set in the configuration request, which is then returned to update the blueprint’s configuration.
Practical Application and Customization
- You can change the hardcoded AccountId to a dynamic value from a quote field or twin field for flexibility.
- Update the Salesforce connection name and set/field variable names in the script to match your environment and data model.
- To overwrite user-entered data on each configuration, remove the conditional check that prevents overwriting existing set data.
- For selective field overwrites while preserving other user edits, explicitly reference set indexes and fields within the script, ensuring proper checks to avoid null reference errors.
Best Practices
- Always check if the set data array contains elements before attempting to update specific indexes to prevent errors.
- Remember that JavaScript array indexing starts at 0, so adjust your index references accordingly.
- Use the example conditional logic to safely update individual fields within a set without clearing all user input.
Outcome
By implementing this enrichment script pattern, ServiceNow CPQ customers can automatically populate and refresh configuration set values from external systems like Salesforce. This improves configuration accuracy, streamlines data integration, and enhances the end-user configuration experience by delivering relevant, up-to-date data within blueprints.
View a detailed example of how to use an On Configure/Reconfigure blueprint enrichment script to load field values into a set.
This article demonstrates how to use an “On Configure/Reconfigure” blueprint enrichment script to load field values into a set.
This example shows making a SOQL call to Salesforce using an external connection to get the last names of all of the account contacts and preloading them into a set for further configuration.
Prerequisites
To complete this example you need:
- Enrichments enabled in your ServiceNow CPQ instance
- External connections enabled in your ServiceNow CPQ instance
- A set created and associated with the blueprint
- A text field associated with the set
External connection: SOQL call
In the ServiceNow CPQ Admin screen, find the external connections under Utilities.
- Set the integration type to Salesforce.
- Define the SOQL query to make. In this example, we are retrieving all the contacts that are associated with a particular account and storing the last name.
SOQL query:
SELECT LastName FROM Contact WHERE AccountId = '{{accId}}'
On Configure/Reconfigure script
In the blueprint, navigate to the enrichments area, and add the script to the “On Configure/Reconfigure” enrichment.
let setData = []; // initialize the set data as an empty array
//if initializing, default in setData
if (cfgRequest.demoset_swc.data == []) {
const inp = {"accId":"0015f000006lRKcAAM" }; // Hardcoded accountId, could be mapped in from a field
const response = Salesforce.getSetInit_swc(inp); // SELECT LastName FROM Contact WHERE AccountId = '{{accId}}'
const contactList = response.body.records;
if (contactList.length > 0) {
for (var contact of contactList) {
let elem = {"textTest_swc":{"value": contact.LastName } }; // set the field value
setData.push(elem); // add to the setData array
}
// after looping through all the contacts/previous set data, add them to the set
cfgRequest.demoset_swc.set("data",setData);
}
}
return cfgRequest;
Script walkthrough
- The setData array is initialized as an empty array (line 1).
- Check whether the set already has data in it, either from the API payload or from a previous configuration (line 3).
- Define the inputs for the query to Salesforce (line 5). In this example, the account ID is hard coded but could be passed from a field or mapped from a twin field on the quote. Make the SOQL query to Salesforce by using the external connection that was defined, then save the response (line 6) and get the records array (line 7).
- If the call returned records (line 8), loop over the records (line 9) and set the field value of “textTest_swc” to the contact LastName (line 10). Finally, add the new set element to the setData array (line 11).
- After looping through the contacts, add all of the setData to the set, “demoSet_swc”, in the configuration request (line 14).
- Finally, return the updated configuration request (line 18).
To reuse this script, make changes to the following lines:
- Line 5: Update the account ID (0015f000006lRKcAAM) to a new value, or twin a field from the quote.
- Line 6: Update the name of the Salesforce connection.
- Line 10: Update the field name (textTest_swc) to a field in the set that you are using.
- Line 16: Update the set name (demoset_swc) to the name of the set that you are using.
Setting data from an external connection each configuration
If instead you want defaulted data to overwrite whatever the user had entered in the previous configuration, delete the if clause in line 4.
If you want only certain fields in the set to overwrite user-edited fields, but you want to keep other fields between configurations, you must reference the set indexes explicitly:
cfgRequest.[setName].data[i].set("fieldName",{"value":"fieldValue"});
Replace the following to use in your enrichment:
setName: The variable name of your setfieldName: The variable name of your fieldi: An existing index at which you would like to determine the fieldʼs valuefieldValue: The value you would like to set for the field
You also must guarantee that the index you are referencing in the data exists. Including a check for the length of the set data array is a good way to guarantee that it exists. See the following example.
if (cfgRequest.setTest.data.length >= 1) {
cfgRequest.setTest.data[0].set("setTestNumberField",{"value":cardNumber});
cfgRequest.setTest.data[0].set("setTestSingleSelect picklist",{"value":"set option 1"});
}
Notice that while the length function returns “1”, the index is “0”, because indexes are referenced starting at 0 as the first index.