- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2022 01:05 AM - edited 12-19-2022 01:06 AM
I have following requirement.
I want Json array such that :
"UserandRoles":[
{
"name":"Abel",
"role":"admin"
},
{
"name":"Abel",
"role":"security_admin"
},
{
"name":"John",
"role":"itil"
}
]
Script is as below:
var info = [];
var Information = {};
var gr = new GlideRecord('sys_users');
gr.addQuery('sys_id', usersys);
gr.query();
if( gr.next()){
info.name = gr.name.getDisplayValue();
info.Roles = gr.u_roles.getDisplayValue(); // u_role field contain roles in folllowing format role1, role2, role3....
info .push(Information );
}
Thanks,
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2022 01:24 AM
var infoList = []
var Information = {};
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', usersys);
gr.query();
if( gr.next()){
var info = {}
var user_name = gr.name.getDisplayValue();
var roles_arr = gr.roles.split(','); // u_role field contain roles in folllowing format role1, role2, role3....
for (var i = 0; i < roles_arr.length; ++i) {
info.name = user_name
info.role = roles_arr[i]
infoList.push(info);
}
}
Information.UserandRoles = infoList
gs.info(JSON.stringify(Information))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2022 01:14 AM
Try the below code snippet
var gr = new GlideRecord('sys_users');
gr.addQuery('sys_id', usersys);
gr.query();
if (gr.next()) {
var name = gr.name.getDisplayValue();
var rolesArr = gr.u_roles.getDisplayValue().split(","); // u_role field contain roles in folllowing format role1, role2, role3....
var userRole = {};
var resultArr = [];
if (rolesArr.length > 0) {
for (var role = 0; role < rolesArr.length; role++) {
userRole.role = rolesArr[role];
userRole.name = name;
resultArr.push(userRole);
}
}
var finalResult = {
"UserandRoles": resultArr
};
gs.info("Result " + finalResult);
}
Thanks & Regards,
Vasanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2022 01:24 AM
var infoList = []
var Information = {};
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', usersys);
gr.query();
if( gr.next()){
var info = {}
var user_name = gr.name.getDisplayValue();
var roles_arr = gr.roles.split(','); // u_role field contain roles in folllowing format role1, role2, role3....
for (var i = 0; i < roles_arr.length; ++i) {
info.name = user_name
info.role = roles_arr[i]
infoList.push(info);
}
}
Information.UserandRoles = infoList
gs.info(JSON.stringify(Information))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2022 02:29 AM
@newhand
Its working but If user having 3 roles then only 1st role gets printed 3 times.
The 2nd and 3 rd role does not get printed
e.g. abel tuter (admin, security_admin, pa_reports)
Abel tuter admin
Abel tuter admin
Abel tuter admin
Jon linkon itil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2022 04:07 AM
try this.
info.role = roles_arr[i] + ""