How to Create a client script check if user is VIP or not, use script include to get answer true or false based on VIP flag and use script include answer in client script and validate.

Jayaprakash2
Kilo Explorer

How to Create a client script check if user is VIP or not, use script include to get answer true or false based on VIP flag and use script include answer in client script and validate.

3 REPLIES 3

Pavankumar_1
Mega Patron

Hi,

Use below client script and script include.

 

Client script: create onchange client script on your table select the user field on form.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

//if you comment below 3 lines it will works for onload
    if (isLoading || newValue === '') {
       return;
    }

    var gauser = new GlideAjax('global.getvipinfo');  //script include name
    gauser.addParam('sysparm_name', 'getInfo'); // script include function that you defined
    gauser.addParam('sysparm_user', newValue); // passing parameter to server
    gauser.getXML(setDetails);

    function setDetails(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer"); //response will gets on answer variable.
        if (answer == "true") {
            g_form.addInfoMessage("Caller is a VIP User");
        } else if (answer == "false") {
            g_form.addInfoMessage("Caller is not a VIP User");
        }


    }

}

 

Script Include:  

 

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

    getInfo: function() {

        var user = this.getParameter('sysparm_user'); // sysid of user record
        var usr = new GlideRecord('sys_user'); //query the user table
        usr.addQuery('sys_id', user); //filter the record with sysid
        usr.addEncodedQuery('vip=true'); //to check user VIP or not
        usr.query();
        if (usr.next()) {
            return "true";
        } else {
            return "false";
        }

    },

    type: 'getvipinfo'
});

 

Hope it helps!!

Please Mark ✅ Correct/helpful, if applicable, Thanks!! 

 

Regards

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Hello, i try your script but in my PDI is  always run and give caller is not a vip can you help

Pavankumar_1
Mega Patron

Hi,

Please close the thread by Mark it  ✅ Correct/helpful, if this solves your issue.

 

Regards

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar