UI builder alert component, conditional visibility based on incident table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2023 11:28 AM
Hey all, I'm having trouble getting this set up in UI Builder. I'm trying to make the Alert Component conditionally visible based on the user's role, and if the incident table has priority 1 incidents.
if there are no priority 1 incidents, and if the user does not contain 'x-role' do not show the alert
show the alert if the users has 'x-role' and if there is at least 1 incident in the incident table
so far i've only added the role condition, not sure how to call server-side stuff in UI builder.
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2023 01:16 PM
I added an Alert component to my page variant:
I used script to control visibility:
/**
* @Param {params} params
* @Param {api} params.api
* @Param {TransformApiHelpers} params.helpers
*/
function evaluateProperty({
api,
helpers
}) {
var result = false;
// Hide component if:
// A. the user does not have the foo role, OR
// B. there are no open P1 incidents
// A
if (api.context.session.user.roles.indexOf("foo") == -1) {
result = true;
}
// B
if (!result) {
var gr = new api.GlideRecord("incident");
gr.addEncodedQuery("stateIN1,2^priority=1"); // New or Active P1
gr.query();
if (!gr.next()) {
result = true;
}
}
return result;
}
When I have the foo role, the alert appears:
Without my having the role, it is hidden:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2023 07:24 PM - edited ‎08-09-2023 07:31 PM
Thanks, Thomas, I really appreciate it. I thought server-side stuff with glideRecord could only be done through data binding and using data resources such as a REST call to an endpoint.
where can i find the documentation for this line in particular?
var gr = new api.GlideRecord("incident");
I don't see this in the documentation ?
Really new with UI builder, thanks a lot . i appreciate the insight tho.
https://docs.servicenow.com/bundle/utah-api-reference/page/app-store/dev_portal/API_reference/api/co...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-10-2023 09:13 AM
Kevin,
Check here. I believe I have the syntax right, but I would not recommend doing it exacty this way. My example was in global scope, and this is not supported in scoped apps. Checking a GlideRecord should be done some other way such as REST.
I would set up both of these as boolean page variables (user has role x, and open P1 incidents exist) and populate them on page load. Then you can avoid scripting on the visibility panel and bind to the variables instead. It's cleaner.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-11-2023 06:58 AM
Actually, using REST to figure out if there are any P1s is a bad idea. Use a display business rule and stick the result on the scratchpad, and pick it up from there in your visibility condition. Or set a page variable in the onLoad and have the visibility condition check the variable.