- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2023 01:20 PM - edited 04-05-2023 03:07 PM
I need to get all the Entitlements for a user and return all the applications and the access they have into a string field
example:
Application: App1 Access: View
Application: App2 Access: Admin
I am getting back the values but I do not know how to combine them in the client script and then pass all the results into 1 string field?
resSTR Application Compose Access Level Admin Rights
resSTR Application Test Application Access Level View Only
resSTR Application Compass Access Level View Only
getAllEntitlements: function() {
var resultObj = {};
var user = this.getParameter('sysparm_user');
var app = this.getParameter('sysparm_app');
var allApps = [];
var grApp = new GlideRecord('u_business_application_entitlements');
grApp.addQuery('u_user', user);
grApp.query();
while (grApp.next()) {
resultObj = {
"application": grApp.u_application.getDisplayValue().toString(),
"access": grApp.u_access_level.getDisplayValue().toString()
};
var json = new global.JSON();
gs.info("TRICIA DATA " + json.encode(resultObj));
return json.encode(resultObj);
// return json.encode(resultObj);
}
},
TRICIA DATA {"access":"Admin Rights","application":"Compose"}
Catalog CLient Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.hideFieldMsg('current_applications');
var gInv = g_form.getValue('access_type');
alert(gInv);
if (gInv == 'revoke_access') {
var getUserIn = new GlideAjax('GetEntitlementsUtil');
getUserIn.addParam('sysparm_name', 'getAllEntitlements');
getUserIn.addParam('sysparm_user', g_form.getValue('requested_by'));
getUserIn.getXML(populateDetails);
}
function populateDetails(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var returneddata = JSON.parse(answer);
//TRICIA DATA {"access":"Admin Rights","application":"Compose"}
alert(answer);
g_form.setValue('current_applications', answer);
var msg = getMessage('This User has Entitlements to: ' + "'" + answer + "." + "The entitlements for all applications in ServiceNow will be set to inactive. .", function(msg) {
g_form.showFieldMsg('current_applications', msg, 'warning');
});
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2023 06:15 PM
Hi @triciav,
in your script include,
//Initialize your resultObj variable to array type
var resultObj = [];
//In the while loop, push values like this
while(grApp.next()){
resultObj.push('Application:' + grApp.u_application.getDisplayValue().toString() + ' - Access Level: ' + grApp.u_access_level.getDisplayValue().toString());
}
//return the result
return JSON.stringify(resultObj);
In Your Client Script populateDetails finction, after getting the answer, parse it back to array
var data = JSON.parse(answer);
//append all the values in to one string
var data_str = '';
data.forEach(function(elem){
data_str = data_str + elem + '\n';
});
Now you can use the data_str variable to populate the field.
g_form.setValue('current_applications', data_str);
Thanks,
Anvesh
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2023 06:15 PM
Hi @triciav,
in your script include,
//Initialize your resultObj variable to array type
var resultObj = [];
//In the while loop, push values like this
while(grApp.next()){
resultObj.push('Application:' + grApp.u_application.getDisplayValue().toString() + ' - Access Level: ' + grApp.u_access_level.getDisplayValue().toString());
}
//return the result
return JSON.stringify(resultObj);
In Your Client Script populateDetails finction, after getting the answer, parse it back to array
var data = JSON.parse(answer);
//append all the values in to one string
var data_str = '';
data.forEach(function(elem){
data_str = data_str + elem + '\n';
});
Now you can use the data_str variable to populate the field.
g_form.setValue('current_applications', data_str);
Thanks,
Anvesh
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 05:11 AM
Awesome! Thank you so much Anvesh!
This was exactly what I needed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 05:58 AM