Client Script Error

Tomas Linde
Tera Expert

Good morning everyone,
I have created a Catalog Client Script and an Include Script so that a user that contains the keyword "Manager" in its firstname and/or lastname cannot be selected in a variable of type reference and displays a message in case an attempt is made to select .
The fact is that it is not executed correctly and I cannot find the error, I would appreciate your help, greetings

 

* Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading) {
    return;
  }

  var requestedFor = g_form.getValue('for_whom_are_you_requesting_this_item');

  var ga = new GlideAjax('ALOCatalogItemOtherSoftwareAJAX');
  ga.addParam('sysparm_name', 'isManager');
  ga.addParam('sysparm_user', requestedFor);

  ga.getXML(checkManager);
}

function checkManager(response) {
  var isManager = response.responseXML.documentElement.getAttribute("answer");

  if (isManager == 'true') {
    g_form.showFieldMsg('for_whom_are_you_requesting_this_item', 'The user is not allowed.', 'error');
  } else {
    g_form.clearFieldMsg('for_whom_are_you_requesting_this_item');
  }
}


* Script Include: 

var ALOCatalogItemOtherSoftwareAJAX = Class.create();
ALOCatalogItemOtherSoftwareAJAX.prototype = {

    isManager: function(userSysId) {
        var gr = new GlideRecord('sys_user');
        if (gr.get(userSysId)) {
            return (gr.getValue('firstname').toLowerCase().indexOf('manager') !== -1 ||
                    gr.getValue('lastname').toLowerCase().indexOf('manager') !== -1);
        }
        return false;
    },

    type: 'ALOCatalogItemOtherSoftwareAJAX'
};
3 ACCEPTED SOLUTIONS

Gustav Aldenbra
Kilo Sage

Hi @Tomas Linde 

It looks like your script include is not set as client callable. Make sure you check it client callable and also make sure the script extends the AbstractAjaxProcessor. The first part of you script include should look like this

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

 

View solution in original post

swathisarang98
Giga Sage
Giga Sage

Hi @Tomas Linde ,

 

As i can see inside function isManger you are not passing the sysid of user, try adding below code 

var userSysId = this.parameter ('sysparm_user');

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

 

 

View solution in original post

Hi @Tomas Linde, I have identified the issue with the field names of first_name and last_name in the script include.

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

	isManager: function() {
		var userSysId = this.getParameter('sysparm_user');
        var gr = new GlideRecord('sys_user');
        if (gr.get(userSysId)) {
            return (gr.getValue('first_name').toLowerCase().indexOf('manager') !== -1 ||
                    gr.getValue('last_name').toLowerCase().indexOf('manager') !== -1);
        }
        return false;
    },

    type: 'ALOCatalogItemOtherSoftwareAJAX'
});

 

Also, you try the client script as below:

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading) {
    return;
  }
  var ga = new GlideAjax('ALOCatalogItemOtherSoftwareAJAX');
  ga.addParam('sysparm_name', 'isManager');
  ga.addParam('sysparm_user', newValue);
  ga.getXML(checkManager);
}

function checkManager(response) {
  var isManager = response.responseXML.documentElement.getAttribute("answer");
  if (isManager) {
    g_form.showFieldMsg('for_whom_are_you_requesting_this_item', 'The user is not allowed.', 'error');
  } else {
    g_form.clearFieldMsg('for_whom_are_you_requesting_this_item');
  }
}

 

SunilKumar_P_0-1708605220228.png

 

 

Regards,

Sunil

 

View solution in original post

8 REPLIES 8

Gustav Aldenbra
Kilo Sage

Hi @Tomas Linde 

It looks like your script include is not set as client callable. Make sure you check it client callable and also make sure the script extends the AbstractAjaxProcessor. The first part of you script include should look like this

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

 

swathisarang98
Giga Sage
Giga Sage

Hi @Tomas Linde ,

 

As i can see inside function isManger you are not passing the sysid of user, try adding below code 

var userSysId = this.parameter ('sysparm_user');

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

 

 

SunilKumar_P
Giga Sage

Hi @Tomas Linde, As you are calling the script include from the client script, you need to check the Client Callable checkbox. The first two line in your script should be

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

Also, you need to retrieve the 'sysparm_user' parameter in the script include using the this.getParameter as show in the below script.

 

Script Include:

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

	isManager: function() {
		var userSysId = this.getParameter('sysparm_user')
        var gr = new GlideRecord('sys_user');
        if (gr.get(userSysId)) {
            return (gr.getValue('firstname').toLowerCase().indexOf('manager') !== -1 ||
                    gr.getValue('lastname').toLowerCase().indexOf('manager') !== -1);
        }
        return false;
    },

    type: 'ALOCatalogItemOtherSoftwareAJAX'
});

SunilKumar_P_0-1708603165994.png

 

Regards,

Sunil

Thank you very much for the help, it still doesn't work.