Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Update Catalog UI Policy

dvelloriy
Kilo Sage

Hello,

I have a catalog UI policy applied to a catalog item. It is making a variable on the catalog form read-only on load.

Requirement is to make this variable editable for users whose falls under certain departments. (user.department.id == 400123,400234,400345 and so on)

How can I include this condition in the UI policy?

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@dvelloriy 

not possible using UI policy

Please use onLoad catalog client script + GlideAjax and check if logged in user department ID IS ONE OF those values

Script Include: It should be client callable

var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkRecordPresent: function() {
        var id = this.getParameter('sysparm_userID');
        var allowedDepartments = ['400123', '400234', '400345']; // Add your department IDs here

        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', id); // Use valid field name here
        gr.query();
        if (gr.next()) {
            if (allowedDepartments.indexOf(gr.department.id.toString()) != -1) {
                return 'found';
            } else {
                return 'not found';
            }
        }
        return 'not found';
    },

    type: 'checkRecords'
});

AnkurBawiskar_0-1741183818882.png

 

onLoad client script:

function onLoad() {

    var ga = new GlideAjax('checkRecords');
    ga.addParam('sysparm_name', "checkRecordPresent");
    ga.addParam('sysparm_userID', g_user.userID);
    ga.getXMLAnswer(function(answer) {
        if (answer == 'found')
            g_form.setReadonly('variableName', false);
        else
            g_form.setReadonly('variableName', true);
    });
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @dvelloriy 

 

In UI policy add a condition

user.department.id is one of them and mentions the required ID.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Tried that already. Its applied on catalog item. Conditions wont show me dot walked fields.

Correct, then you need to use the Client scripts where you need mention these deatils.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Ankur Bawiskar
Tera Patron
Tera Patron

@dvelloriy 

not possible using UI policy

Please use onLoad catalog client script + GlideAjax and check if logged in user department ID IS ONE OF those values

Script Include: It should be client callable

var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkRecordPresent: function() {
        var id = this.getParameter('sysparm_userID');
        var allowedDepartments = ['400123', '400234', '400345']; // Add your department IDs here

        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', id); // Use valid field name here
        gr.query();
        if (gr.next()) {
            if (allowedDepartments.indexOf(gr.department.id.toString()) != -1) {
                return 'found';
            } else {
                return 'not found';
            }
        }
        return 'not found';
    },

    type: 'checkRecords'
});

AnkurBawiskar_0-1741183818882.png

 

onLoad client script:

function onLoad() {

    var ga = new GlideAjax('checkRecords');
    ga.addParam('sysparm_name', "checkRecordPresent");
    ga.addParam('sysparm_userID', g_user.userID);
    ga.getXMLAnswer(function(answer) {
        if (answer == 'found')
            g_form.setReadonly('variableName', false);
        else
            g_form.setReadonly('variableName', true);
    });
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader