---
sourceDocument: Brazil Customer Service Management
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/customer-service-management

 Release :

    - brazil

ft:locale :

    - en-US

ft:publication_title :

    - Brazil Customer Service Management

ft:clusterId :

    - csm

bundleId :

    - csm

workflow :

    - Customer and Industry


---

# Get All Opportunities custom action

# Get All Opportunities custom action {#ariaid-title1}

Release version: Brazil  
Updated September 10, 2026  
![](https://www.servicenow.com/docs/portal-asset/ico-clock) 2 minutes to read
Summarize  
![AI sparkle icon](https://servicenow.com/docs/portal-asset/ai-sparkle-icon) 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

The Get All Opportunities action enables ServiceNow customers to retrieve opportunity records directly from the Salesforce application.
This action is triggered when viewing the remote Opportunity table within ServiceNow, facilitating seamless integration and data synchronization with Salesforce opportunities.
Show full answer Show less  

## Action Inputs and Query Construction

This action requires a single integer input parameter that specifies the maximum number of opportunity records to return. Because REST-based actions do not support pagination, it is important to limit the number of records to avoid performance issues. The recommended maximum is 1,000 records, with a default of 500.

A pre-processing script dynamically constructs a Salesforce Object Query Language (SOQL) query to fetch the specified opportunity fields: **Name, Id, AccountId, CloseDate, Amount, StageName, Probability, and Type** . Customers can customize the query to include additional fields by using the **Get Opportunity Fields** action and can control record ordering by adding an ORDER BY clause (e.g., ordering by CloseDate descending).

## REST Step

The action uses a standard REST step from the Salesforce spoke to execute the query. No modifications are necessary here, but customers must ensure the correct Salesforce Connection Alias is configured to enable connectivity.

## Post-processing and Outputs

The post-processing script handles the Salesforce response by:

* Parsing the JSON response and checking for errors.
* Extracting opportunity records into the output structure.
* Generating an informational message if the number of retrieved records equals the specified maximum, indicating that not all available data might have been retrieved due to record limits.
* Setting appropriate error messages if the response contains errors or is invalid.

The final action outputs include the query status, any error or informational messages, and the retrieved opportunity records.

## Practical Benefits for ServiceNow Customers

* Efficiently synchronize Salesforce opportunity data into ServiceNow without manual export/import processes.
* Control over the volume of data retrieved to optimize performance and avoid overload.
* Customizable queries to retrieve relevant opportunity fields and order data as needed.
* Error handling ensures clear visibility into issues during data retrieval.  
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 {#csm-get-all-opportunities-action__section_cz2_m2z_53b}

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 {#csm-get-all-opportunities-action__section_hrw_m2z_53b}

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 {#csm-get-all-opportunities-action__section_j3b_n2z_53b}

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 {#csm-get-all-opportunities-action__section_jxp_mfz_53b}

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 {#csm-get-all-opportunities-action__section_xnl_2gz_53b}

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

