- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2023 12:25 PM
Hi Guys,
I am using a Flow Designer Action to get the response body from a REST API call.
The response body looks like this:
{"result":[{"__type":"DynScripts.Scripts_KAlG0agHshDQHh81gO41sNH69TbZRui+GAPDelegation, Scripts_KAlG0agHshDQHh81gO41sNH69TbZRui","delegatedto":"employee1@test.rrd.com","type":"Mail","willExpire":true,"expiryDate":"2023-05-31T00:00:00.0000001Z"},{"delegatedto":"employee2@test.rrd.com","type":"Mail","willExpire":true,"expiryDate":"2023-06-14T21:53:00.0000000Z"}]}
I have this in my script step:
When I call this action, I get undefined for var response = obj.result;
(function executeRule(current, previous /*null when async*/ ) {
(function() {
try {
var inputs = {};
var userEmail = current.variables.delegation_emp_name.email;
inputs['email'] = userEmail; // String
gs.log('userEmail = ' + userEmail, 'Delegate');
// Start Asynchronously: Uncomment to run in background. Code snippet will not have access to outputs.
// sn_fd.FlowAPI.getRunner().action('global.oimgetdelegations').inBackground().withInputs(inputs).run();
// Execute Synchronously: Run in foreground. Code snippet has access to outputs.
var result = sn_fd.FlowAPI.getRunner().action('global.oimgetdelegations').inForeground().withInputs(inputs).run();
var outputs = result.getOutputs();
// Get Outputs:
// Note: outputs can only be retrieved when executing synchronously.
var obj = outputs['obj']; // String
var mrvs = current.variables.vs_delegate_info;
var response = obj.result;
gs.log('response = ' + response, 'Delegate');
gs.log('response.length = ' + response.length, 'Delegate');
if (response.length > 0) {
current.variables.delegation_already_exists = 'true';
}
for (var i = 0; i < response.length; i++) {
var res = response[i];
var newRow = mrvs.addRow();
var empID = res.delgate;
var user = new GlideRecord('sys_user');
user.addQuery('employee_number', empID);
user.query();
while (user.next()) {
var userName = user.name;
}
newRow.delegate_to = userName;
newRow.delegate_type = res.type;
var exp_date = res.expiryDate;
newRow.delegate_expiry = exp_date.slice(0, 10);
}
} catch (ex) {
var message = ex.getMessage();
gs.error(message);
}
})();
})(current, previous);
Could someone please help?
Thank you!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2023 04:04 PM
It seems like the issue is with how you are accessing the result property of the obj object. Since the obj object is a JSON string, you need to first parse it into a JavaScript object using the JSON.parse() method before you can access its properties.
Notice that I added the line var obj = JSON.parse(objString); after retrieving the obj property from the outputs object. This line parses the JSON string into a JavaScript object that you can access using dot notation.
Here's the updated code/script :
(function executeRule(current, previous /*null when async*/) {
(function() {
try {
var inputs = {};
var userEmail = current.variables.delegation_emp_name.email;
inputs['email'] = userEmail; // String
gs.log('userEmail = ' + userEmail, 'Delegate');
// Start Asynchronously: Uncomment to run in background. Code snippet will not have access to outputs.
// sn_fd.FlowAPI.getRunner().action('global.oimgetdelegations').inBackground().withInputs(inputs).run();
// Execute Synchronously: Run in foreground. Code snippet has access to outputs.
var result = sn_fd.FlowAPI.getRunner().action('global.oimgetdelegations').inForeground().withInputs(inputs).run();
var outputs = result.getOutputs();
// Get Outputs:
// Note: outputs can only be retrieved when executing synchronously.
var objString = outputs['obj']; // JSON string
var obj = JSON.parse(objString); // Parse the JSON string into a JavaScript object
var mrvs = current.variables.vs_delegate_info;
var response = obj.result;
gs.log('response = ' + response, 'Delegate');
gs.log('response.length = ' + response.length, 'Delegate');
if (response.length > 0) {
current.variables.delegation_already_exists = 'true';
}
for (var i = 0; i < response.length; i++) {
var res = response[i];
var newRow = mrvs.addRow();
var empID = res.delgate;
var user = new GlideRecord('sys_user');
user.addQuery('employee_number', empID);
user.query();
while (user.next()) {
var userName = user.name;
}
newRow.delegate_to = userName;
newRow.delegate_type = res.type;
var exp_date = res.expiryDate;
newRow.delegate_expiry = exp_date.slice(0, 10);
}
} catch (ex) {
var message = ex.getMessage();
gs.error(message);
}
})();
})(current, previous);
Please mark my answer correct/helpful in case it adds value and moves you a step closer to your desired ServiceNow solution goal.
Thanks,
Punit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2023 04:04 PM
It seems like the issue is with how you are accessing the result property of the obj object. Since the obj object is a JSON string, you need to first parse it into a JavaScript object using the JSON.parse() method before you can access its properties.
Notice that I added the line var obj = JSON.parse(objString); after retrieving the obj property from the outputs object. This line parses the JSON string into a JavaScript object that you can access using dot notation.
Here's the updated code/script :
(function executeRule(current, previous /*null when async*/) {
(function() {
try {
var inputs = {};
var userEmail = current.variables.delegation_emp_name.email;
inputs['email'] = userEmail; // String
gs.log('userEmail = ' + userEmail, 'Delegate');
// Start Asynchronously: Uncomment to run in background. Code snippet will not have access to outputs.
// sn_fd.FlowAPI.getRunner().action('global.oimgetdelegations').inBackground().withInputs(inputs).run();
// Execute Synchronously: Run in foreground. Code snippet has access to outputs.
var result = sn_fd.FlowAPI.getRunner().action('global.oimgetdelegations').inForeground().withInputs(inputs).run();
var outputs = result.getOutputs();
// Get Outputs:
// Note: outputs can only be retrieved when executing synchronously.
var objString = outputs['obj']; // JSON string
var obj = JSON.parse(objString); // Parse the JSON string into a JavaScript object
var mrvs = current.variables.vs_delegate_info;
var response = obj.result;
gs.log('response = ' + response, 'Delegate');
gs.log('response.length = ' + response.length, 'Delegate');
if (response.length > 0) {
current.variables.delegation_already_exists = 'true';
}
for (var i = 0; i < response.length; i++) {
var res = response[i];
var newRow = mrvs.addRow();
var empID = res.delgate;
var user = new GlideRecord('sys_user');
user.addQuery('employee_number', empID);
user.query();
while (user.next()) {
var userName = user.name;
}
newRow.delegate_to = userName;
newRow.delegate_type = res.type;
var exp_date = res.expiryDate;
newRow.delegate_expiry = exp_date.slice(0, 10);
}
} catch (ex) {
var message = ex.getMessage();
gs.error(message);
}
})();
})(current, previous);
Please mark my answer correct/helpful in case it adds value and moves you a step closer to your desired ServiceNow solution goal.
Thanks,
Punit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2023 07:27 PM
Thank you Punit3!!! It worked. I appreciate it.