Calling Now Assist Custom Skill from scripts with non-record related parameters

Joanna15
Tera Expert

I have a use case where I need to call Now Assist custom skill from a request flow. I will need to pass not only the RITM details to the Now Assist custom skill, but also other data points collected from external system. 

 

Reading the ServiceNow doc Call a custom skill from a script • Zurich Enable AI • Docs | ServiceNow, I wonder if queryString of inputsPayload can be used to pass these extra attributes? I wonder if there is any example of how queryString is being in the calling script and Now Skill? Is it possible to pass JSON object in the  queryString? 

 

I wonder if someone investigated this and has some learning to share?

 

1 REPLY 1

Spencer1
Tera Contributor
There are two ways to implement this.
 
Option 1 - Call the Now Assist skill via code using a JSON object as an input:
 
Create a custom Now Assist skill where a JSON object is the input:
 
image (1).png

 

Deploy the skill as a UI Action:
 
image (2).png

 

The generated UI action will have the code you can use to call the Now Assist skill. Simply, generate your own JSON object and assign it to the input payload:
 
image (3).png

 

Option 2 - Create a custom Now Assist Skill that uses the RITM variables and data queried from an API as inputs:
 
Create an input that is a Requested Item:
 
image (4).png

 

 
 
Under tools, create two scripts:
 
image (5).png

 

 
Query the RITM variables via code and put them in a JSON object:
 
(function runScript(context) {
    /* @Param {Object} context - Execution context, which may include:*/
    /*                         - Previous tool outputs/inputs (e.g., context['ToolName.attributeName'] or context.getValue('ToolName.attributeName')).*/
    /*                         - Additional context information (e.g., context.getAllValues()).*/
    /* @returns {Object} - The computed value for the attribute.*/

    var ritmSysId = context['requested_item'];

    // Get all variables for this RITM
    var variables = {};
    var gr = new GlideRecord('sc_item_option');
        gr.addQuery('request_item', ritmSysId);
        gr.query();

    while (gr.next()) {
        var questionText = gr.item_option_new.question_text.toString();
        var variableName = gr.item_option_new.name.toString();
        var value = gr.value.toString();
        var displayValue = gr.getDisplayValue('value');
       
        variables[variableName] = {
            question: questionText,
            value: value,
            displayValue: displayValue
        };
    }  

    return (JSON.stringify(variables, null, 2));
})(context);
 
Query the API data via code and put them into a JSON object:
 
(function runScript(context) {
    /* @Param {Object} context - Execution context, which may include:*/
    /*                         - Previous tool outputs/inputs (e.g., context['ToolName.attributeName'] or context.getValue('ToolName.attributeName')).*/
    /*                         - Additional context information (e.g., context.getAllValues()).*/
    /* @returns {Object} - The computed value for the attribute.*/

     try {
        var request = new sn_ws.RESTMessageV2();
       
        // Public endpoint: Fetching current temperature for Atlanta, GA
        // No Auth required.
        // Used as a sample endpoint.
        request.setEndpoint('https://api.open-meteo.com/v1/forecast?latitude=33.749&longitude=-84.388&current=temperature_2m');
        request.setHttpMethod('GET');
       
        var response = request.execute();
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();

        if (httpStatus === 200) {
            // Parse the JSON string into a variable
            var weatherData = JSON.parse(responseBody);
        } else {
            gs.error("API Error: " + httpStatus);
        }
    } catch (ex) {
        gs.error("Exception: " + ex.message);
    }

    return weatherData;
})(context);
 
You can then use these tools as inputs:
 
image (6).png
 
Good Luck!