Agent studio scripting

FibinP274053114
Tera Contributor

I am trying to create an AI agent in the Agent studio while create a tool as script i am constantly getting error while trying to get the output from that tool could someone please help me on this :
below is the code and error :

(function execute(inputs, outputs) {

    var incNumber = inputs.incident_number;

    if (!incNumber) {
        outputs.status = "Error: Incident number not provided";
        return;
    }

    var gr = new GlideRecordSecure("incident");
    gr.addQuery("number", incNumber);
    gr.query();

    if (gr.next()) {
        outputs.status = "Incident Found";
        outputs.short_description = gr.short_description.toString();
        outputs.state = gr.getDisplayValue("state");
        outputs.caller = gr.getDisplayValue("caller_id");
    } else {
        outputs.status = "Incident Not Found";
    }

})(inputs, outputs);

error:
{"fileName":"sn_aia_tool.51a76bfe3b433a1053918d41a3e45a33.script","sourceName":"sn_aia_tool.51a76bfe3b433a1053918d41a3e45a33.script","lineNumber":23,"name":"ReferenceError"}


1 REPLY 1

warren_chan
ServiceNow Employee

I believe that the "outputs" array isn't specifically declared, and rather, whatever is returned is understood as the output. Try this:

 

(function execute(inputs) {

    var incNumber = inputs.incident_number;
    var outputs = '';

    if (!incNumber) {
        outputs = "Error: Incident number not provided";
        return;
    }

    var gr = new GlideRecordSecure("incident");
    gr.addQuery("number", incNumber);
    gr.query();

    if (gr.next()) {
        outputs += "Incident Found";
        outputs += "Short description: " + gr.short_description.toString();
        outputs += "State: " + gr.getDisplayValue("state");
        outputs += "Caller: " + gr.getDisplayValue("caller_id");
    } else {
        outputs = "Incident Not Found";
    }

    return outputs;

})(inputs);