Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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 ACCEPTED SOLUTION

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);

 

View solution in original post

2 REPLIES 2

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);

 

FibinP274053114
Tera Contributor

thank you @warren_chan