- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2023 03:50 AM
Hi Team,
In MRVS I have list collector variable. Here in each row i can able to add more then 1 ci in the list collector. And through workflow i am passing that value to api.
Here, I am facing challenge for adding multiple rows. For instance, for row 1 if i add 3 ci in list collector i can able to fetch it..
then if add another row, then 1st row 2 record and 2nd row 1record means, then the values i am not able concardinate here. Can someone assist here how to do outloop to get values.
below are my script
var source_name;
var mvrs = JSON.parse(current.variables.u_request_details);
var mvrsLength = mvrs.length;
gs.info("Anand API - FireFlow mvrsLength 1: " + mvrsLength);
for (var i = 0; i < mvrs.length; i++) {
var sourceCMBD = mvrs[i].source_cmdb;
gs.info("Anand API - FireFlow SourceCMBD 2: " + sourceCMBD);
var sourceName = new GlideRecord('cmdb_ci');
var query = 'sys_id=' + sourceCMBD;
sourceName.addEncodedQuery(query);
sourceName.query();
while (sourceName.next()) {
source_name = sourceName.name;
}
And like below i am sending the payload to api. >>>>>>>>>>>>>>
var r1 = new sn_ws.RESTMessageV2('ANAND API - FireFlow', 'Fireflow Traffic Change Request');
r1.setStringParameterNoEscape('source_name', source_name);
r1.setMIDServer('************');
var response1 = r1.execute();
var responseBody1 = response1.getBody();
var httpStatus1 = response1.getStatusCode();
var parsedResponse2 = JSON.parse(responseBody1);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2023 04:10 AM
Hi @AnandKumar1
Can you try by updating below lines (highlighted in bold red)
//make it array or JSON object choice is yours - here taken as array
var source_name =[];
var mvrs = JSON.parse(current.variables.u_request_details);
var mvrsLength = mvrs.length;
gs.info("Anand API - FireFlow mvrsLength 1: " + mvrsLength);
for (var i = 0; i < mvrs.length; i++) {
var sourceCMBD = mvrs[i].source_cmdb;
gs.info("Anand API - FireFlow SourceCMBD 2: " + sourceCMBD);
var sourceName = new GlideRecord('cmdb_ci');
var query = 'sys_idIN' + sourceCMBD;
sourceName.addEncodedQuery(query);
sourceName.query();
while (sourceName.next()) {
source_name.push(sourceName.name); //push value in array
}
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2023 04:17 AM
I'm not following exactly. In your log are you getting the full and expected values for source_cmdb? If not, there's an alternate approach to parsing an MRVS that I can give you. Assuming you're getting this far for now, it looks like you would want to change your query to allow for multiple:
var query = 'sys_idIN' + sourceCMBD;
and then inside the while loop you need to push each value to an array rather than overwriting the same script variable:
var source_nameArr = [];
var mvrs = JSON.parse(current.variables.u_request_details);
var mvrsLength = mvrs.length;
gs.info("Anand API - FireFlow mvrsLength 1: " + mvrsLength);
for (var i = 0; i < mvrs.length; i++) {
var sourceCMBD = mvrs[i].source_cmdb;
gs.info("Anand API - FireFlow SourceCMBD 2: " + sourceCMBD);
var sourceName = new GlideRecord('cmdb_ci');
var query = 'sys_idIN' + sourceCMBD;
sourceName.addEncodedQuery(query);
sourceName.query();
while (sourceName.next()) {
source_nameArr.push(sourceName.name.toString());
}
}
use source_nameArr.join(',') in the payload
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2023 04:10 AM
Hi @AnandKumar1
Can you try by updating below lines (highlighted in bold red)
//make it array or JSON object choice is yours - here taken as array
var source_name =[];
var mvrs = JSON.parse(current.variables.u_request_details);
var mvrsLength = mvrs.length;
gs.info("Anand API - FireFlow mvrsLength 1: " + mvrsLength);
for (var i = 0; i < mvrs.length; i++) {
var sourceCMBD = mvrs[i].source_cmdb;
gs.info("Anand API - FireFlow SourceCMBD 2: " + sourceCMBD);
var sourceName = new GlideRecord('cmdb_ci');
var query = 'sys_idIN' + sourceCMBD;
sourceName.addEncodedQuery(query);
sourceName.query();
while (sourceName.next()) {
source_name.push(sourceName.name); //push value in array
}
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2023 04:50 AM
Thanks for your quick help vishal. I getting same response as previous one in logs.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2023 05:03 AM
Hi @Anand
Its strange....!!
as per for loop its should loop twice if there are two rows.
Can you try to add more rows in MRVS and check ??
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2023 04:17 AM
I'm not following exactly. In your log are you getting the full and expected values for source_cmdb? If not, there's an alternate approach to parsing an MRVS that I can give you. Assuming you're getting this far for now, it looks like you would want to change your query to allow for multiple:
var query = 'sys_idIN' + sourceCMBD;
and then inside the while loop you need to push each value to an array rather than overwriting the same script variable:
var source_nameArr = [];
var mvrs = JSON.parse(current.variables.u_request_details);
var mvrsLength = mvrs.length;
gs.info("Anand API - FireFlow mvrsLength 1: " + mvrsLength);
for (var i = 0; i < mvrs.length; i++) {
var sourceCMBD = mvrs[i].source_cmdb;
gs.info("Anand API - FireFlow SourceCMBD 2: " + sourceCMBD);
var sourceName = new GlideRecord('cmdb_ci');
var query = 'sys_idIN' + sourceCMBD;
sourceName.addEncodedQuery(query);
sourceName.query();
while (sourceName.next()) {
source_nameArr.push(sourceName.name.toString());
}
}
use source_nameArr.join(',') in the payload