Parent Poll Processor

The Poll Processor contains the logic that will be applied when polling a remote system for data.

The Poll Processor is a configuration record which contains the logic that will be applied when polling a remote system for data. There are three main scripts which are used to setup, execute and process the Poll Requests. The scripts given here are examples of how you might configure your Poll. The details of yours may differ depending on your requirements.

This parent Poll Processor will pull back a list of the relevant records (polling enough data to identify the records). Each record it identifies will be passed on to the child Poll Processor.

New Poll Processor

In Unifi Integration Designer, navigate to & open < Your Integration > (created following the Outbound Incident Guide).

Click the 'Poll Processors' icon & then New.

The fields to be configured for the New Poll Processor modal are as follows:

FieldDescriptionValue

Name

The name of the Processor.

<Your Name>

Your New Poll Processor modal should look like this:

Submit and view to further configure the Poll Processor.

Setup Script

The Setup Script is the first script to run (it runs at the point in time the Poll Request is created). It is used to build the environment for the poll and define what it will do. We will use it to setup the URL that will be called.

Navigate to Scripts > Setup Script.

The initial Poll Processor fields to be configured are as follows:

FieldDescriptionValue

Setup script

The script to setup the Poll Request record.

Update the code in the Setup script field so that it looks like the code below

The code in the Setup script field should look like this:

// Configure the new Poll Request record
(function(poll_request, poller, params) {

    var gdt = new GlideDateTime();
    gdt.addSeconds(-1800);

    var connection = poller.getIntegration().getActiveConnection();
    var user = connection.getBasicAuthUser();
    var time = connection.getData('last_update_time', (gdt + ''));
    var cvars = connection.getVariables();

    // We only need to retrieve a minimal set of fields
    var fields = cvars.list_fields;

    var query = [
        'correlation_idISNOTEMPTY',
        'sys_updated_by!=' + user,
        'sys_updated_on>' + time
    ];

    var uri_query = query.map(function(c) {
        return encodeURIComponent(c);
    }).join('%5E');

    poll_request.endpoint_url += '/table/incident?sysparm_query=' + uri_query +
        '&sysparm_fields=' + encodeURIComponent(fields) + '&sysparm_limit=10';

})(poll_request, poller, params);

Setup script: The parameters for which data to return are contained in the endpoint url.

fields: The table API will return all the data by default, so we choose to deliberately limit what is returned to the fields listed in this variable.

query: Instead of querying all the records on the remote instance, we deliberately limit the records to those where the correlation id is not empty (i.e. it's bonded), that haven't been updated by the Authentication user (our system) and that were updated since either since the last update time (if one exists), or in the last 30 minutes.

Endpoint URL: The value used in the poll_request.endpoint_url was initially generated using the ServiceNow REST API Explorer, substituting variables in for the uri query and fields. This value is appended to the existing endpoint url in the active connection before being added to the Poll Request.

Note: We have included the /table/incident element of the endpoint url because we used the truncated url in the Connection when following the Outbound Incident Guide. If you have used the full url there, then this element can be excluded here.

(We have also chosen to limit the number of records returned to 10.)

Your Setup Script form should look like this:

Navigate to Request Script.

Request Script

The Request Script is used to reach into the remote system and execute the request. We will use the ServiceNow RESTMessageV2() web service to make a REST call to the URL defined in the Setup Script.

The next Poll Processor field to be configured is as follows:

FieldDescriptionValue

Request script

The script that executes the request.

Update the code in the Request script field so that it looks like the code below

The code in the Request script field should look like this:

// Process the request e.g. by executing a web service and returning the response
(function(poll_request, poller, connection, params) {

    var rm = new sn_ws.RESTMessageV2();
    rm.setEndpoint(poll_request.endpoint_url);
    rm.setHttpMethod('GET');
    rm.setRequestHeader('Accept', 'application/json');
    rm.setRequestHeader('Content-type', 'application/json');
    rm.setBasicAuth(connection.getBasicAuthUser(), connection.getBasicAuthPassword());

    // rm.setRequestBody(JSON.stringify(body));
    var resp = rm.execute();

    answer = resp.getBody() + '';

})(poll_request, poller, connection, params);

Request script: This script uses the ServiceNow RESTMessageV2() web service to make a REST call to the endpoint url created in the Setup script. It returns the body of the request as the answer which it passes to the Response script_._

Your Request Script form should look like this:

Navigate to Response Script.

Response Script

The Response Script is used to process the information returned from the remote system. We will pass this data to Unifi, telling it which Message to use to process the data.

The last Poll Processor field to be configured is as follows:

FieldDescriptionValue

Response script

The script that processes the response to the request.

Update the code in the Response script field so that it looks like the code below

The code in the Response script field should look like this:

// Process the response returned by the request script
// The 'answer' variable from the request script is passed in here as the 'response' parameter 
(function (poll_request, poller, response, params) {

    var body = JSON.parse(response);

    // Nothing to do if no results were returned
    if ( body.result.length == 0 ) {
        poll_request.response_status = 'No Incidents returned\n\n' + JSON.stringify(body,null,2);
        return;
    }

    // Sample result
/*
{
  "result": [
    {
      "sys_id": "0ecc4865db734010c3ebde82ca961960",
      "number": "INC0010107",
      "correlation_id": "INC0010345",
    },
    {
      ... next Incident
    }
  ]
}   
*/

    // Establish the environment
    var integration = poller.getIntegration();
    var conn  = integration.getActiveConnection();
    var cvars = conn.getVariables();

    var info = [];

    // Process each result
    body.result.forEach(function(inc){

        // Set up a Poll to retrieve an Incident (returns PollRequest object)
        var pr = x_snd_eb.Poller.execute( cvars.incident_poller, {
            incident_id : inc.sys_id
        });

        info.push('Incident: ' + inc.number + ' - PollRequest: ' + pr.getValue('number'));

    });

    poll_request.response_status = info.join('\n') + '\n\n' + JSON.stringify(body,null,2);

})(poll_request, poller, response, params);

Response script: This script parses the response into a body object to contain the result, (returning if it doesn't contain anything).

It then establishes the environment, getting the Integration, Connection & Connection Variables.

After that it processes each of the results (returned tickets). For each ticket, it sets up a Poll Request to retrieve an Incident (returning a PollRequest object).

After processing each result, the results are logged to the Response status field of the Poll Request.

x_snd_eb.Poller.execute(): This method has two parameters. In the first we pass the sys_id of the child Poller (as created on the 'Child Poller' page). In the second we pass an object containing the sys_id of the returned Incident as the incident_id element (telling the Poller which record to poll).

params: The params object is passed through to the subsequent scripts (and on to further Pollers, if required). This is used to pass the incident_id to the child Poller.

Your Response Script form should look like this:

Save the Poll Processor.

Update Child Poll Processor

Navigate to and open < Your Child Poll Processor > (created on the 'Child Poll Processor' page) and update the value of the Parent field by selecting the parent Poll Processor created above.

Remember, We have not scripted any business logic to run from the value in the Parent field. It is given for documentary purposes only (for you to easily identify which parent Poll Processor kicked off the child).

Now let's move on and configure the Parent Poller.