Client side script and Script include not working

NamitD
Tera Contributor

Hello ,

 

I am trying to set VIP checkbox as true if a user is vip in incident form via client script and script include , here is my code but its not working . I created new field on incident form to set it but its not working and not returning anything via alert as well. Any help ?


Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   var c = g_form.getValue('caller_id');

   var g = new GlideAjax('userV');
   g.addParam('sysparm_name','isVip');
   g.addParam('sysparm_value',c);
   g.getXMLAnswer(call);

   function call(response){
    var answer = response.responseXML.documentElement.getAttribute('answer');
    alert(answer);
    g_form.setValue('u_vip',answer);

   //Type appropriate comment here, and begin script below
   
}
 
 
Script Include:

var userV = Class.create();
userV.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    isVip : function(){
        var u = this.getParameter('sysparm_value');
        var sr= new GlideRecord(sys_user);
        sr.addQuery('sys_id',u);
        sr.query();
        if(sr.next()){
            var s = sr.vip;
            return s;
        }
       


},
    type: 'userV'
});




1 ACCEPTED SOLUTION

Hi @NamitD ,

 

I have tried the below in PDI and it is working. Please check the below for reference:

 

Script Include(client callable - true)

SN_Learn_0-1721038545181.png

 

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

    isVip: function() {
        var userID = this.getParameter('sysparm_value');

        var userTab = new GlideRecord('sys_user');
        if (userTab.get(userID)) {
            var checkVIP = userTab.vip;
        }
        return checkVIP;
    },

    type: 'userV'
});

Also, check if there are any ACL created when you are creating script include, like below. You need to evaluate true according to it otherwise it wont work, OR you can disable the ACL after elevating to 'security_admin'.

SN_Learn_1-1721038638942.png

 

 

 

onChange Client Script:

SN_Learn_2-1721038739319.png

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var c = g_form.getValue('caller_id');

    var g = new GlideAjax('userV');
    g.addParam('sysparm_name', 'isVip');
    g.addParam('sysparm_value', c);
    g.getXML(userVIPCheck);

    function userVIPCheck(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('u_vip', answer);

    }
}

 

 

Output:

 

Not a VIP User:

 

SN_Learn_3-1721038784551.png

 

VIP User:

SN_Learn_4-1721038832562.png

 

 

Mark this as Helpful / Accept the Solution if this helps

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

View solution in original post

7 REPLIES 7

SN_Learn
Kilo Patron
Kilo Patron

Hi @NamitD ,

 

Please try the below, tested and working in PDI

 

Script Include:

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


    isVip: function() {
        var userID = this.getParameter('sysparm_value');

        var userTab = new GlideRecord('sys_user');
        if (userTab.get(userID)) {
            var checkVIP = userTab.vip;
        }
        return checkVIP;
    },


    type: 'userV'
});

 

Onchange Client script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var c = g_form.getValue('caller_id');

    var g = new GlideAjax('userV');
    g.addParam('sysparm_name', 'isVip');
    g.addParam('sysparm_value', c);
    g.getXML(userVIPCheck);

    function userVIPCheck(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('u_vip', answer);
    }
}

 

 

Mark this as Helpful / Accept the Solution if this helps

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

Community Alums
Not applicable

Hi @NamitD ,

I check your code there is one small error you write GlideRecord like 

var sr= new GlideRecord(sys_user);
You need to pass the table name in string like below
var sr= new GlideRecord("sys_user");
 
Please mark my answer correct and helpful if this works for you
Thanks and Regards 
Sarthak

Rupanjani
Giga Guru

Hi @NamitD ,

 

In Script include, in the line where GlideRecord is written, the table should be given in quotes.

The code is breaking there as the table name is not passed as a string.

Make this change and try. It will work.

 

 


Mark the response correct and helpful if the answer assisted your question.

Its_Azar
Tera Guru

Hi there @NamitD 

Try replacing both these.

function call(response) {
    var answer = response.responseXML.documentElement.getAttribute('answer');
    alert(answer); // For debugging
    g_form.setValue('u_vip', answer == 'true');
}

 

SI

isVip: function() {
    var userId = this.getParameter('sysparm_value');
    var userRecord = new GlideRecord('sys_user');
    userRecord.addQuery('sys_id', userId);
    userRecord.query();
    if (userRecord.next()) {
        return userRecord.vip == true ? 'true' : 'false';
    }
    return 'false';
},

 

If this helps kindly accept the response thanks much.

☑️ If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.




Kind Regards,

Mohamed Azarudeen Z

Developer @ KPMG

 Microsoft MVP (AI Services), India