Passing JSON Values to REST API from list collector variable on form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2020 07:27 PM
Hi everyone,
I will do my best to try to explain my predicament. I just need a little guidance in the right direction or some insight on how others would tackle this.
We have developed a 3rd-party API that will handle some server orchestration for us. This API feeds into a database via a JSON structure that looks like this:
{
"ritm_no": "RITM00000004",
"requestor": "Smith,Bob",
"pam_group": "PAM-TEST",
"users": [
{
"name": "Tom Jones",
"email": "test@test.com",
"employee_id": "100000"
},
{
"name": "Bill Thomas",
"email": "test2@test.com",
"employee_id": "100001"
}
]
}
I have created a outbound REST POST activity. I have also created a catalog item that the customer uses to make selections on the form. This form then provides the data to send to the API. On the form, the customer has the ability to select multiple users (via a list collector) to add to the PAM group and from each user I need to provide the name, email, and employee number to the API. I know that I can easily provide the parameters for the API for the other fields via scratchpad variables that I've established in a run script activity in my workflow. However, I'm a bit lost on the best way to tackle any fields that allow multiple selections.
I know I can split the values of the list collector via .split(',') and GlideRecord to the sys_user table to get the corresponding field values.
var answer=[];
var user=current.variables.current.variables.cyberark_group_1_user_list.toString().split(',');
len=user.length;
for(var i=0;i<=len;i++){
var gr2=new GlideRecord('sys_user');
gr2.addQuery('sys_id','IN',user[i]);
gr2.query();
while(gr2.next()){
answer.push(gr2.getDisplayValue('name'));
answer.push(gr2.getDisplayValue('email'));
answer.push(gr2.getDisplayValue('employee_number'));
}
But I don't know how to approach getting the values to send to the API. I've also thought about passing all the parameters via a RESTMessageV2() script but not sure if that'll work either.
list=[];
while(foo.next()){
userjson={
"name": foo['name'],
"email": foo['email'],
"employee_id": foo['id']
};
list.append(userjson);
}
requestjson = {
"users": list
};
restMessage.setRequestBody(requestjson);
var response = restMessage.executeAsync();
Like I said, I'm just looking for any insight to get me started or if anyone has had any luck doing this stuff on other projects. As always, thank you all for your help!
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2020 10:32 PM
Hello,
I think the below example might help you with REST and JSON
Mark my ANSWER as CORRECT and HELPFUL if it helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2020 07:00 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2020 12:06 AM
Adding to this, I've been able to produce something similar to the JSON required but it is not quite correct.
Using the following script:
var jsonObj = {};
var user = current.variables.cyberark_group_1_user_list.toString().split(',');
len = user.length;
for(var i=0;i<=len;i++) {
var gr2 = new GlideRecord('sys_user');
gr2.addQuery('sys_id', user[i]);
gr2.query();
var arr = [];
var obj = {};
while (gr2.next()) {
var username = gr2.getDisplayValue('name');
var email = gr2.getDisplayValue('email');
var userid = gr2.getDisplayValue('employee_number');
obj = {
"username": username,
"email": email,
"userid": userid
};
}
arr.push(obj);
jsonObj.Users = arr;
gs.info(JSON.stringify(jsonObj));
}
I get the following in the logs:
{"Users":[{"username":"Frank Smith","email":"Frank.Smith@test","userid":"153504"}]}
However, if I pass two users on the same request, I get two separate results. These results need to be combined so that they look like the JSON above. I know I must have the order of my conditions wrong, or perhaps I'm going about this incorrectly, but any help would be greatly appreciated!