Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

RITM Rest API Integration

reks
Kilo Contributor

Hi Guys,

I am doing RITM integration servicenow to servicenow using REST API.

I have created REST Outbound POST method.

content = {"number":"${number}","comments":"${comments}","request_item":"${request_item}"}

am setting the values for the above variables.

for the variable 'request_item' -   i want to create a custom payload, where i want to sent the list of all RITM's created for the request with variables for each RITM.

Sample payload.

"request_item": [

              { "number":"RITM1312312", "variables":[ { "name":"BMW", "value":"testing" },{ "name":"BMW1", "value":"testing" } ] },

              { "number":"RITM123123", "variables":[ { "name":"BMW", "value":"testing" },{ "name":"BMW1", "value":"testing" } ] }              

      ]

So that, at the other instance i can print the length of 'request_item' ,which gives me 2 and use for loop and set all variable names with corresponding values.

Please help me like how to set value to the variable (request_item) in the above format and how to fetch the values for the same variable in other system.

var r = new sn_ws.RESTMessageV2('RITMIntegration', 'POST');

r.setStringParameterNoEscape ('number', current.number);

r.setStringParameterNoEscape ('comments', current.comments);

Appreciate your inputs.

Thanks,

Rekha

5 REPLIES 5

Alikutty A
Tera Sage

You may need to work out with JSON array



var vars = [];


var json_1 = { "name":"BMW", "value":"testing" };


var json_2 ={ "name":"BMW1", "value":"testing" };


vars.push(json_1);


vars.push(json_2);


r.setStringParameterNoEscape ('variables', vars);



Thank You


Please Hit Like, Helpful or Correct depending on the impact of response


reks
Kilo Contributor

If i want to pass the values dynamically ?


like below.


var json_1 = { "number:"+curent.number+", comments:"+current.comments };



if i give above code, than its showing me error (missing :)



Thanks,


Rekha


Here is some dynamic code, you need to determine the total number of items



var vars = [];


for(var i=0 ;   i< arr.length; i++){


  var json = { "name": arr[i], "value": arr[i] };


  vars.push(json);


}


r.setStringParameterNoEscape ('variables', vars);




Thank You


Please Hit Like, Helpful or Correct depending on the impact of response


reks
Kilo Contributor

If i want to the fetch the values for the variable at other system.



var requestBody = request.body;


var requestData = requestBody.data;


gs.log(requestData.variables.length);



i need to get 2 since am sending 2 RITMS, but here am getting like total length of the string like around 35.



gs.log(requestData.variables[0].name)


getting undefined.