Get All Opportunities custom action
Summarize
Summary of Get All Opportunities custom action
The Get All Opportunities custom action enables ServiceNow customers to retrieve opportunity records directly from the Salesforce application. This action is primarily used when viewing the remote Opportunity table within ServiceNow, allowing integration with Salesforce data.
Show less
Action Inputs and Usage
This action requires a single integer input that specifies the maximum number of opportunity records to return. Since REST-based actions in this context do not support pagination, it is important to limit the number of records fetched to avoid performance issues. The recommended maximum is 1,000 records, with a default of 500.
Pre-processing Step
The pre-processing script dynamically constructs a Salesforce Object Query Language (SOQL) query to select opportunity records. It includes specified fields such as Name, Id, AccountId, CloseDate, Amount, StageName, and Probability. Customers can customize the query to control the order of results by adding an ORDER BY clause (e.g., ordering by CloseDate descending), enabling retrieval of the most relevant records first.
REST Step
The REST step uses ServiceNow’s standard Salesforce spoke REST actions and requires no modifications beyond ensuring it points to the correct Salesforce Connection Alias.
Post-processing Step
After receiving the Salesforce response, the post-processing script parses the data, handles errors, and formats the output. It sets status messages indicating success or failure and provides an informational message when the retrieved data hits the maximum record limit, signaling that not all opportunities may have been fetched.
Action Outputs
The action outputs include:
- Query execution status (success or error)
- Error messages if any issues occur during retrieval
- Informational messages about record limits
- The list of opportunity records returned by the query
This setup helps ServiceNow customers efficiently access Salesforce opportunity data within their workflows while managing data volume and ensuring robust error handling.
The Get All Opportunities action retrieves opportunity records from the Salesforce application. This action is invoked when you view the remote Opportunity table.
Action inputs
The Get All Opportunities action takes a single integer parameter that identifies the maximum number of records to be returned by the query. The REST-based actions can’t accommodate pagination and, for this reason, it’s important to limit the number of records returned from the third-party application. It isn’t recommended to place more than 1,000 records in the remote table. The default number of records is 500.
Pre-processing step
The pre-processing script step takes the action input as its own.
The pre-processing script creates a SELECT query for all opportunities limited by the maximum number of records allowed. This query is based on the Salesforce Object Query Language (SOQL).
(function execute(inputs, outputs) {
outputs.query = "query/?q=SELECT+Name,Id,AccountId,CloseDate,Amount,” +
“StageName,Probability,Type+FROM+Opportunity”;
outputs.query = outputs.query +
“+LIMIT+” + inputs.max_number_of_opportunity_records;
})(inputs, outputs);
You must specify the fields from the opportunity records that you’re interested in. This example uses the following fields: Name, Id, Account Id, Close Date, Amount, Stage Name, and Probability. To see the full list of available fields, use the Get Opportunity Fields action.
You can also control the ordering of the query search by adding the ORDERED BY keyword and value for ASC or DESC order direction. For example, you can add the following line to the script before the line specifying the LIMIT. It makes the query return the first 500 records with the most recent Close Date.
outputs.query = outputs.query + “+ORDERED+BY+CloseDate+DESC”;The pre-processing output is a query.
REST step
The REST step is a standard REST step from the Salesforce spoke REST-based actions. You don’t need to make any changes. Make sure that it points to the correct Connection Alias.
Post-processing step
The post-processing script step takes the action input and REST step output as its inputs.
The post-processing script checks the query response for errors, sets the error message if needed, extracts opportunity data from the response body, and creates an information message to indicate that not all data was retrieved from the Salesforce due to the number of records limitation.
(function execute(inputs, outputs) {
try{
var response = JSON.parse(inputs.res_body);
} catch(e) {
outputs = errorHandler(inputs, outputs);
}
function createOutputJson(inputs, outputs) {
try{
outputs.records = { data: response.records };
outputs.status = "Success";
if ( outputs.records.data.length ==
inputs.max_number_of_opportunity_records ) {
outputs.info_message = "Opportunity retrieve operation was “ +
“limited to" +
inputs.max_number_of_opportunity_records +
" records.";
}
} catch(e) {
outputs = errorHandler(inputs, outputs);
}
return outputs;
}
function errorHandler(inputs, outputs) {
outputs.status = "Error";
outputs.error_message = "Unknown Error. “ +
“Please check error log for more information";
if(inputs.res_body.contains("message"))
outputs.error_message = response[0].message;
return outputs;
}
if(inputs.status_code == "200")
outputs = createOutputJson(inputs, outputs);
else
outputs = errorHandler(inputs, outputs);
})(inputs, outputs);
The following are outputs of the post-processing step.
Action outputs
Action outputs consist of the query status, error and information messages, and opportunity records. See the preceding screenshot for the action outputs.