Get All Opportunities custom action

  • Release version: Yokohama
  • Updated January 30, 2025
  • 2 minutes to read
  • Summarize
    Summarized using AI
    This content was generated using new OpenAI-powered functionality. Results are provided on an as is basis and are not guaranteed to be accurate or complete.

    Summary of Get All Opportunities custom action

    TheGet All Opportunitiescustom action in ServiceNow retrieves opportunity records from the Salesforce application. It is primarily used when viewing the remote Opportunity table, enabling you to access Salesforce opportunity data directly within ServiceNow.

    Show full answer Show less

    Action Inputs and Limits

    This action requires a single integer input specifying the maximum number of opportunity records to return. Due to limitations with REST-based actions and the lack of pagination support, it is important to limit the number of records fetched. The recommended maximum is 1,000 records, with a default value set to 500 to maintain optimal performance.

    Pre-processing Step

    The pre-processing script generates a SOQL query to select opportunity fields from Salesforce, constrained by the specified record limit. The default fields retrieved include Name, Id, AccountId, CloseDate, Amount, StageName, Probability, and Type. You can customize the fields by modifying the query or use the Get Opportunity Fields action for a full list of available fields.

    Additionally, you can control the ordering of the records by adding an ORDER BY clause—for example, to retrieve the most recent opportunities first, add ordering by CloseDate DESC.

    REST Step

    The REST step uses the Salesforce spoke’s standard REST-based action to execute the query. Ensure this step is configured to use the correct Connection Alias for your Salesforce instance. No modifications to this step are typically required.

    Post-processing Step

    This script processes the Salesforce response by:

    • Parsing the JSON response and checking for errors.
    • Extracting the opportunity records into the output payload.
    • Setting informational messages when the retrieved record count reaches the specified limit, indicating that some records may not have been retrieved due to the limit.
    • Handling errors gracefully by setting appropriate error messages.

    Action Outputs

    The outputs include:

    • Status indicating success or error.
    • Error messages if the query fails.
    • Information messages about record limits.
    • The list of opportunity records retrieved from Salesforce.

    Practical Benefits for ServiceNow Customers

    This action enables you to seamlessly integrate Salesforce opportunity data into ServiceNow, empowering your teams with real-time insights without switching platforms. By limiting record counts and managing queries efficiently, it ensures performance and usability when working with Salesforce data in your ServiceNow environment.

    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.

    Inputs screen for the Get All Opportunities action showing the action input and its default value.

    Pre-processing step

    The pre-processing script step takes the action input as its own.

    Pre-processing step screen showing the action input with a pill picker to generate a query.

    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.

    Output variable section showing the query parameter as an output.

    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.

    REST step screen showing connection details and request details for verification.

    Post-processing step

    The post-processing script step takes the action input and REST step output as its inputs.

    Post Processing step screen showing the inputs, including the action input and input variables.

    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.

    Output variable screen within the Post Processing step showing records, error_messages, info_messages, and status variables.

    Action outputs

    Action outputs consist of the query status, error and information messages, and opportunity records. See the preceding screenshot for the action outputs.