Create a RITM using cart api in scoped application.

Sai25
Giga Guru

Hi All,

I want to create a RITM using Cart API and REST message POST method.

When incident is submitted in source Instance RITM should be created in Target Instance.

 

Thank You,

Sai.

1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

indeed as ankur already suggested for Service Catalog API , that will fulfill your requirement. 

 

here is the code:

 

    var cTable = new GlideRecord('incident');
    cTable.get(current.sys_id);
    var json = cTable.getValue('variable');

    var ab = JSON.stringify(json);

    var data = ab.replace(/\\r\\n/g, '').replace(/"/g, '').replace(/'/g, '"');
    var parser = new global.JSONParser();
    var json_alert = parser.parse(data);

    var r = new sn_ws.RESTMessageV2();
    r.setEndpoint('https://xxxx.service-now.com/api/sn_sc/servicecatalog/items/' + json_alert.sysparm_id + '/order_now');
    r.setHttpMethod('POST');

    var user = '<admin>';
    var password = '<password>';

    var obj = {};
    obj.sysparm_quantity = json_alert.sysparm_quantity;
    var vobj = {};
    vobj.comments = json_alert.variables.carrier;
    jsonObj.variables = vobj;
    r.setBasicAuth(user, password);
    r.setrHeader("Accept", "application/json");
    r.setrHeader('Content-Type', 'application/json');
    r.setrBody(JSON.stringify(obj));
    var response = r.execute();

    gs.info(response.getBody());

 

this thread linked to your below thread , so in future any other user will see this thread , they can completely understand your exact use case. 

 

https://community.servicenow.com/community?id=community_question&sys_id=0f8e1837db749454414eeeb5ca96...

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sai,

you can consume the OOB REST API of the target instance using RestMessageV2 from source instance

I have replied to similar question and that has worked; ensure you enhance it as per your need

Also are you saying details from incident will be populated in catalog item variable?

https://community.servicenow.com/community?id=community_question&sys_id=6c5316bbdb7c5810f7fca851ca96196d

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sai,

details steps and script below; no need of cart api etc; just OOB Service Catalog API you need to consume from instance A to instance B

Note: Ensure you give proper details below

1) catalog item sys_id of target instance

2) proper username and password of API user in target instance

3) proper instanceName

4) proper variables of catalog item

5) proper values from incident

var catalogItemSysId = '';

var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://instanceName.service-now.com/api/sn_sc/servicecatalog/items/' + catalogItemSysId + '/order_now');
request.setHttpMethod('POST');

//Eg. UserName="admin", Password="admin" for this code sample.
var user = 'admin';
var password = 'admin';

var jsonObj = {};
jsonObj.sysparm_id = catalogItemSysId.toString();
jsonObj.sysparm_quantity = 1;

var variablesObj = {};

// test is the name of variable and we are sending inc description to this
variablesObj.test = current.description; 

// caller is variable name of type ref to sys_user and we are sending inc caller to this
variablesObj.caller = current.caller_id;

jsonObj.variables = variablesObj;

request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');
request.setRequestBody(JSON.stringify(jsonObj));
var response = request.execute();

gs.info(response.getBody());

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Harsh Vardhan
Giga Patron

indeed as ankur already suggested for Service Catalog API , that will fulfill your requirement. 

 

here is the code:

 

    var cTable = new GlideRecord('incident');
    cTable.get(current.sys_id);
    var json = cTable.getValue('variable');

    var ab = JSON.stringify(json);

    var data = ab.replace(/\\r\\n/g, '').replace(/"/g, '').replace(/'/g, '"');
    var parser = new global.JSONParser();
    var json_alert = parser.parse(data);

    var r = new sn_ws.RESTMessageV2();
    r.setEndpoint('https://xxxx.service-now.com/api/sn_sc/servicecatalog/items/' + json_alert.sysparm_id + '/order_now');
    r.setHttpMethod('POST');

    var user = '<admin>';
    var password = '<password>';

    var obj = {};
    obj.sysparm_quantity = json_alert.sysparm_quantity;
    var vobj = {};
    vobj.comments = json_alert.variables.carrier;
    jsonObj.variables = vobj;
    r.setBasicAuth(user, password);
    r.setrHeader("Accept", "application/json");
    r.setrHeader('Content-Type', 'application/json');
    r.setrBody(JSON.stringify(obj));
    var response = r.execute();

    gs.info(response.getBody());

 

this thread linked to your below thread , so in future any other user will see this thread , they can completely understand your exact use case. 

 

https://community.servicenow.com/community?id=community_question&sys_id=0f8e1837db749454414eeeb5ca96...

Ankur Bawiskar
Tera Patron
Tera Patron

@sai.reddy 

I believe this question was related to creating RITM in target instance using RestMessage and POST method.

Hence I shared the relevant script and mentioned to pick the value and set the relevant variables as per your requirement.

Regards
Ankur

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader