Passing data from Salesforce to ServiceNow CPQ fields
Summarize
Summary of Passing data from Salesforce to ServiceNow CPQ fields
This guide explains how to pass external data, specifically from Salesforce or other APIs, into ServiceNow CPQ configurations using the On Configuration/Reconfigure enrichment (Init enrichment). This enables you to enrich your CPQ configurations with information that is not directly available within ServiceNow CPQ at initialization, such as credit card types or custom Salesforce data.
Show less
Setup and Configuration
- Begin by setting up external connections in ServiceNow CPQ to integrate with APIs or Salesforce. This involves defining connection parameters like name, host, path, authentication, and timeout.
- Create the target fields in ServiceNow CPQ that will receive the external data and associate them with the relevant blueprint using the enrichment.
- All four ServiceNow CPQ field types can accept data assigned from the enrichment, provided the data format matches the field type.
Scripting the Init Enrichment
- The enrichment script is written in JavaScript and uses ServiceNow CPQ’s advanced function editor.
- External connections are referenced in the script via
External.{connectionName}(inputs)orSalesforce.{connectionName}(inputs)for Salesforce connections. - Within the Init enrichment, the configuration request object (
cfgRequest) represents the current configuration’s fields, allowing assignment of external data using: cfgRequest.{fieldName}.set("value", value);cfgRequest.{fieldName}.value = value;- Twinned Salesforce quote fields are pre-populated in
cfgRequestbefore the script runs, enabling their use as inputs for enrichment logic. - To retrieve field values, use either
cfgRequest.{fieldName}.get("value")orcfgRequest.{fieldName}.value.
Example: Populating Credit Card Type from an External API
- An external connection is configured to call FakerAPI.it, which returns fake credit card data.
- The enrichment script calls the API, parses the JSON response, extracts the card type, and assigns it to the ServiceNow CPQ field
cardTypeadv. - This process results in the
cardTypeadvfield being populated with values like “American Express” dynamically during configuration initialization.
Querying Salesforce Data Using SOQL
- For data outside of Salesforce quotes, create a Salesforce external connection in ServiceNow CPQ Admin with integration type set to Salesforce.
- Enter the SOQL query to retrieve the required Salesforce data directly.
- Use the Salesforce external connection in the Init enrichment script to pull and assign this data to configuration fields.
Practical Benefits for ServiceNow Customers
- Enables dynamic enrichment of CPQ configurations with external data, improving configuration accuracy and customization.
- Supports integration with Salesforce and other APIs for seamless data flow into ServiceNow CPQ.
- Allows leveraging twinned Salesforce quote fields as inputs to external calls, enhancing script logic flexibility.
Learn how to pass information from Salesforce into ServiceNow CPQ using the On Configuration/Reconfigure enrichment.
Sometimes a ServiceNow CPQ configuration needs information from outside the configuration passed into the configuration on initialization. There are several ways to do this.
If the information can be found on the Salesforce quote, use the twinning method of data transfer. External data would be assigned to a field in ServiceNow CPQ.
In this example, ServiceNow CPQ uses the On Configuration/Reconfigure enrichment (called “Init enrichment” here) to send an API call to the website “FakerAPI.it”. This website sends an API response with fake credit card information.
Our goal is to populate a field in ServiceNow CPQ with the credit card type (Visa, MasterCard, or other).
Setup
The first step of using an enrichment and external connection is to set it up. For instructions, see Set up External connections for configuration rules. (Note that the examples in this link focus mostly on the On BOM Response enrichment. This article complements the other because it focuses more on the Init enrichment.)
The following information is used for our external connection to FakerAPI.it.
| Parameter | Value |
|---|---|
| Name | CardType |
| variableName | cardType |
| host | https://fakerapi.it |
| path | /api/v1/credit_cards?_quantity=1 |
| Authentication Type | None |
| timeout | 500 |
Next, the field in ServiceNow CPQ must be created and associated with the blueprint that uses the enrichment. All four ServiceNow CPQ field types can be assigned data from the enrichment. The data assigned to the field from the API response just needs to match the field.
Scripting the Init enrichment
The final step is to write the script for the Init enrichment. The enrichment's language is based on JavaScript, and users are encouraged to look at the Help section of the advanced function editor for most of the sample scripts needed. The following lines of code are not in the Help section, so are documented here.
To reference an external connection in an enrichment, use the following:
Var {varName} = External.{externalConnectionVariableName}(inputs)
If calling to Salesforce, change External to Salesforce.
Var {varName} = Salesforce.{externalConnectionVariableName}(inputs)
If there are no inputs, use empty parentheses.
In the Init enrichment, the Fields item changes to Configuration Request, which is referenced in the script as cfgRequest. This item is used to define fields in the ServiceNow CPQ configuration that is being initialized. Therefore, if external data is passed to ServiceNow CPQ, it is assigned from the cfgRequest JSON object and values have to be programmed into it. That can be scripted two ways (Items in curly braces ( { } ) and in italics are determined by the
user):
cfgRequest.{LogikVariableName}.set(“value”, “{value}”) ;
cfgRequest.{LogikVariableName}.value = ”{value}” ;
For example, suppose the company Solar Fare Well sells and installs solar panels. They want to assign two external values to their configurator: which way the roof faces and the square footage of the roof. Their Init enrichment might have the following lines of code:
cfgRequest.roofDirection.value= “North”;
cfgRequest.sqft.set(“value”, 500);
Notice that the number does not need quotes around it.
The Configuration Request item contains all the requests on the configuration. This includes fields that are twinned into ServiceNow CPQ. Moreover, cfgRequest is populated with the twinned fields before the Init enrichment script runs. This means that twinned fields can be used as variables (inputs) for the enrichment script
and external connection, which can affect the output from the script and external connection. The following snippet shows the formatting options when getting values from cfgRequest.
var x = cfgRequest.{varName}.get(“value”);
x= cfgRequest.{varName}.value;
Sample script solution
When implementing the above code, the first step is to look at the JSON response from the external connection call.
Using the external connection defined above, the following enrichment script returns an API response similar to the one that follows just below.
var sample=External.cardType();
return sample;
"body": {
"total": "1",
"code": "200",
"data": [
{
"owner": "Mercedes Emmerich",
"number": "2410279364686428",
"expiration": "04/25",
"type": "American Express"
}
],
"status": "OK"
}
Note that this is not the entire API response, and that card data changes with each API call. As expected, this matches the API response from the website.
Next, we need to parse down to the card type. The formatting examples in the Advanced Function Help section can help you identify how to parse the JSON object response.
The following enrichment script returns the result that follows.
var sample=External.cardType();
var x=sample.body.data[0].type;
cfgRequest.cardType_adv.set("value", x);
return cfgRequest;
{
"cardType_adv": {
"userEdited": false,
"value": "American Express"
}
}
So in ServiceNow CPQ, the cardType_adv field is defined as “American Express”.
Query data from Salesforce (SOQL)
As mentioned earlier, data that is in a Salesforce quote can be twinned into ServiceNow CPQ. However, if the data is found elsewhere in Salesforce, the Init enrichment has Salesforce as the external connection, and you perform a query using Salesforce Object Query Language (SOQL). If you are unfamiliar with the SOQL language, you can find out more at Salesforceʼs Trailhead site.
To use SOQL in the enrichment, the only difference from the examples above is a change to the external connection. In ServiceNow CPQ Admin, click Utilities, click External Connections, and then click New. This is how you normally create an external connection.
For the field integration type, select Salesforce. This provides a box for you to enter your SOQL query.