UI builder alert component, conditional visibility based on incident table

Kevin Gomez1
Tera Contributor

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!

4 REPLIES 4

thomaskennedy
Tera Guru

I added an Alert component to my page variant:

thomaskennedy_0-1691611938852.png

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:

thomaskennedy_1-1691612106270.png

 

Without my having the role, it is hidden:

 

thomaskennedy_2-1691612126408.png

 

 

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...

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.

 

thomaskennedy
Tera Guru

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.