Help with a script include to update fields on the requested item table

Jyotsna22
Kilo Contributor

I have the a script include that makes a API call based on the output response, below is the piece of code I am executing in background script. We have 2 servicenow instances (Local & Client) and when this is called a new REQ and RITM will be created on Client instance with reference numbers starting with 'REQ' and 'RITM' and o the local instance the reference numbers will start with 'WREQ' and 'WRITM'

 

var reqFor = "T27589";
var reqBy = "T27589";
var ritmNum = "WRITM9820";
var catsysid = "febed567db035850caaa320a689619b6";
var app = "yes";
var sdesc = "Decommissioning of VM-AZURE";

 

var gr = new GlideRecord("sc_req_item");
gr.addQuery("request", '70fb6b0e1bfd951098e66280604bcbf9');
gr.query();
while (gr.next()) {
var ownvar = new GlideRecord('sc_item_option_mtom');
ownvar.addQuery('request_item.number', gr.number);
ownvar.addQuery('sc_item_option.value', '!=', '');
ownvar.orderBy('sc_item_option.order');
ownvar.query();
var variableData = {};
var items = "Summary of " + gr.number + " raised from Catalog : " + gr.cat_item.getDisplayValue() + "\n\n";
while (ownvar.next()) {
var field = ownvar.sc_item_option.item_option_new;
var fieldValue = ownvar.sc_item_option.item_option_new.name;
// Print variable name
items += field.getDisplayValue() + " : " + gr.variables[fieldValue].getDisplayValue() + "\n";
}
}


var Dobj = {
requested_for : reqFor,
opened_by : reqBy,
external_ticket_id : ritmNum,
file_name : "",
file_data : "",
approvals_needed : app,
short_description : sdesc,
RITMs: [
{
item: catsysid,
variables :[
{
"name": "request_details",
"value": items
},
{
"name": "wbs_code",
"value": "12345"
},
{
"name": "variable1",
"value": "test1"
},
{
"name": "variable2",
"value": "test2"
},
{
"name": "variable3",
"value": "test3"
},
{
"name": "variable4",
"value": "test4"
},
{
"name": "variable5",
"value": "test5"
},
{
"name": "automation_possible",
"value": "yes"
},
{
"name": "application_service",
"value": "test app"
},
{
"name": "complexity",
"value": "Medium"
},
{
"name": "primary_ci",
"value": "testserver"
}
]
}
]
};

var str = JSON.stringify(Dobj);
//gs.print(str);


var script =new CMP_EON_Oauth_Utils();
var token = script.get_Token();

var r = new sn_ws.RESTMessageV2();
r.setEndpoint('https://api-nonprod.eon.com/edt/itsm/dev/service-mgmt-api/v1/ritm/create');
r.setHttpMethod('POST');
r.setRequestHeader('Authorization','Bearer'+' '+token);
r.setRequestHeader('Content-Type', 'application/json');
r.setRequestBody(str);
var response = r.execute();
gs.log("Response" + response.getBody(), 'EONReqOutbound');
var result=response.getBody();
var errorcode=response.getStatusCode();

gs.print(result);
gs.print(errorcode);

 

Below is the response I receive when I execute the above script in background script

Jyotsna22_0-1665663099792.png

 

so now logic to be updated in Script include to update fields on local instance requested item form

 

if  errorcode == 200

{

Update current ritm record

ACK/acknowledgement = true

Client RITM = RITM number

Client Request = Request number

Set RITM state = Pending approval

}

 

Else{

Update current ritm record

ACK/acknowledgement = true

Error_Message = Error message from rest response

}

 

__________________________________________________________________________

 

 

Failure Response:

 

Jyotsna22_1-1665663274847.png

 

 

Success Response :

 

Jyotsna22_2-1665663274863.png

 

 

0 REPLIES 0