Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

how to add 'impact' and 'servcie_offering' when VA is creating incident with scripts

SonnyY
Tera Contributor

The following script is to create incident with 'short_description', 'Urgency' etc. The Virtual Agent is using this code to create incident.
I want to also add 'Impact' and 'Service_offering'. However, it did not work when I did the following:

-duplicate this script and create a new one;
-add 'impact' and 'service_offering' in line 6
-add new lines to set value of 'impact' and 'service_offering' between line 15 and line 16.

Please shed some light on how to achieve this.

System Definition>Script Includes>VACreateINCUtil

1 var VACreateINCUtil = Class.create();
2
3 VACreateINCUtil.prototype = {
4 initialize: function() {},
5
6 createIncident: function(caller, short_description, urgency, description, cmdb_ci) {
7 var incDetails = {};
8
9 // Create Universal Request if create ur with incident property is set to true
10 var universalgr = this.createUR();
11
12 var incGr = new GlideRecord("incident");
13 incGr.initialize();
14 incGr.setValue("caller_id", caller);
15 incGr.setValue("urgency", urgency);
16 incGr.setValue("short_description", short_description);
17 incGr.setValue("description", description);
18 incGr.setValue("contact_type", "virtual_agent");
19 incGr.setValue("cmdb_ci", cmdb_ci);
20
21 if (!gs.nil(universalgr))
22 incGr.setValue("universal_request", universalgr.sys_id);
23
24 incDetails.sysid = incGr.insert();
25 incDetails.incNum = incGr.getValue('number');
26
27 // Check if creation of incident is successful otherwise delete UR
28 if (!gs.nil(universalgr)) {
29 if (!gs.nil(incDetails.sysid))
30 this.updateUR(universalgr, incGr);
31 else // Delete UR if incident creation failed
32 this.deleteUR(universalgr);
33 }
34
35 return incDetails;
36 },
37
38 createUR: function() {
39 var create_ur = gs.getProperty("sn_itsm_va.com.snc.create_universal_request_with_incident");
40 if (create_ur == "true") {
41 if (new GlidePluginManager().isActive("com.snc.universal_request")) {
42 var universalgr = new GlideRecord("universal_request");
43 universalgr.initialize();
44 universalgr.insert();
45 return universalgr;
46 }
47 }
48 return;
49 },
50
51 updateUR: function(universalGr, incGr) {
52 // Creating new reference for universalGr to get latest values
53 var gr = new GlideRecord("universal_request");
54 gr.get(universalGr.sys_id);
55 gr.setValue("short_description", incGr.short_description);
56 gr.setValue("contact_type", "virtual_agent");
57
58 if (gs.nil(gr.getValue("primary_task")))
59 gr.setValue("primary_task", incGr.getUniqueValue());
60
61 gr.update();
62 },
63
64 deleteUR: function(universalGr) {
65 universalGr.deleteRecord();
66 },
67
68 type: 'VACreateINCUtil'
69 };

1 ACCEPTED SOLUTION

rpriyadarshy
Kilo Sage
The issue is usually not in the Script Include itself. The Script Include can set any Incident field, but the Virtual Agent topic/action calling it must also pass the new parameters (Impact & Service Offerings) or you Can set in background using added script.

Since Impact and Service Offering both failed together, the most likely root cause is:

You modified the Script Include But did not modify the VA Script Action / Flow Action / Script Step that invokes VACreateINCUtil.createIncident(). 

As a result, the new parameters are arriving as undefined, so nothing is populated on the Incident.

If you share the Virtual Agent script/action that calls VACreateINCUtil, I can tell you the exact code changes needed.

 

Some More Inputs - 

 

Update Function Inputs Parameter List.

createIncident: function(caller, short_description, urgency, impact,service_offering, description, cmdb_ci) {}
 
Set the Incident Value
incGr.setValue("urgency", urgency);
incGr.setValue("impact", impact);
incGr.setValue("service_offering", service_offering);
incGr.setValue("short_description", short_description);
incGr.setValue("description", description);
 
if Impact and Service Offering will come from VA then 

create corresponding VA variables for:Impact and Service Offerings.

Regards

RP

 

View solution in original post

4 REPLIES 4

rpriyadarshy
Kilo Sage
The issue is usually not in the Script Include itself. The Script Include can set any Incident field, but the Virtual Agent topic/action calling it must also pass the new parameters (Impact & Service Offerings) or you Can set in background using added script.

Since Impact and Service Offering both failed together, the most likely root cause is:

You modified the Script Include But did not modify the VA Script Action / Flow Action / Script Step that invokes VACreateINCUtil.createIncident(). 

As a result, the new parameters are arriving as undefined, so nothing is populated on the Incident.

If you share the Virtual Agent script/action that calls VACreateINCUtil, I can tell you the exact code changes needed.

 

Some More Inputs - 

 

Update Function Inputs Parameter List.

createIncident: function(caller, short_description, urgency, impact,service_offering, description, cmdb_ci) {}
 
Set the Incident Value
incGr.setValue("urgency", urgency);
incGr.setValue("impact", impact);
incGr.setValue("service_offering", service_offering);
incGr.setValue("short_description", short_description);
incGr.setValue("description", description);
 
if Impact and Service Offering will come from VA then 

create corresponding VA variables for:Impact and Service Offerings.

Regards

RP

 

SonnyY
Tera Contributor

based on the script: System Definition>Script Includes>VACreateINCUtil

I've created a new script:   VACreateINCUtilforITHelpDesk

 

var VACreateINCUtilforITHelpDesk = Class.create();

VACreateINCUtilforITHelpDesk.prototype = {
    initialize: function() {},

    createIncident: function(caller, short_description, impact, urgency, description, cmdb_ci) {
        var incDetails = {};

        //Create Universal Request if create ur with incident property is set to true
        var universalgr = this.createUR();

        var incGr = new GlideRecord("incident");
        incGr.initialize();
        incGr.setValue("caller_id", caller);
        incGr.setValue("urgency", urgency);
        incGr.setValue("impact", impact);
        incGr.setValue("short_description", short_description);
        incGr.setValue("description", description);
        incGr.setValue("contact_type", "virtual_agent");
        incGr.setValue("cmdb_ci", cmdb_ci);

        if (!gs.nil(universalgr))
            incGr.setValue("universal_request", universalgr.sys_id);

        incDetails.sysid = incGr.insert();
        incDetails.incNum = incGr.getValue('number');

        //Check if creation of incident is successful otherwise delete UR
        if (!gs.nil(universalgr)) {
            if (!gs.nil(incDetails.sysid))
                this.updateUR(universalgr, incGr);
            else // Delete UR if incident creation failed
                this.deleteUR(universalgr);
        }

        return incDetails;
    },

    createUR: function() {
        var create_ur = gs.getProperty("sn_itsm_va.com.snc.create_universal_request_with_incident");
        if (create_ur == "true") {
            if (new GlidePluginManager().isActive("com.snc.universal_request")) {
                var universalgr = new GlideRecord("universal_request");
                universalgr.initialize();
                universalgr.insert();
                return universalgr;
            }
        }
        return;
    },

    updateUR: function(universalGr, incGr) {
        // Creating new reference for universalGr to get latest values
        var gr = new GlideRecord("universal_request");
        gr.get(universalGr.sys_id);
        gr.setValue("short_description", incGr.short_description);
        gr.setValue("contact_type", "virtual_agent");
        if(gs.nil(gr.getValue("primary_task")))
            gr.setValue("primary_task", incGr.getUniqueValue());
        gr.update();
    },

    deleteUR: function(universalGr) {
        universalGr.deleteRecord();
    },

    type: 'VACreateINCUtilforITHelpDesk'
};

 

I called it in the VA as below, but it is not working for 'impact'.

(function execute() {
    var caller = vaInputs.caller;
    var short_desc = vaInputs.short_description;
    var urgency = vaInputs.urgency;
    var impact = vaInputs.impact;
    var desc = vaInputs.description;
    var cmdb_ci = vaInputs.cmdb_ci;

     if (!gs.nil(caller) && !gs.nil(short_desc)) {
        var incDetails = new sn_itsm_va.VACreateINCUtilforITHelpDesk().createIncident(caller, short_desc, urgency, impact, desc, cmdb_ci);
        vaVars.inc_sysid = incDetails.sysid;
     }
     
})()

sk4408bc
Tera Contributor

Hey @SonnyY 
Did this work?

SonnyY
Tera Contributor

You modified the Script Include But did not modify the VA Script Action / Flow Action / Script Step that invokes VACreateINCUtil.createIncident(). 

You're right about it. However, the parameters were defined ok. The issue occurred where the api was not defined correctly in the VA script.  Please see below. Now that I have updated the API prefix, it worked perfectly. Many thanks, 

if (!gs.nil(caller) && !gs.nil(short_desc)) {
        var incDetails = new x_rehpl_it_servi_0.VACreateINCUtilforITHelpDesk().createIncident(caller, short_desc, urgency, impact, service_offering, desc, cmdb_ci);
        vaVars.inc_sysid = incDetails.sysid;
     }
     
})()