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.

Need help with Catalog item Script Include

MWright1
Giga Guru

Hi everyone,

I have a form with a Department field and a Department French lookup field.  When the user selects a department, the department french should auto populate.

The form contains these two fields:

find_real_file.png

The first one is a reference field pointing to a table I created, and the 2nd one is a Single Line text to hold the french value (the one I need to populate).

The lookup is to a table I created called Department with only 2 fields:

1. Department Name English - u_department_name - this is also the Display field.

2. Department Name French - u_department_name_fr

Because this is a catalog item, I created a Script Include as follows:

find_real_file.png

var department_details = Class.create();
department_details.prototype = Object.extendsObject(AbstractAjaxProcessor, {
department_info: function() {
      var result = this.newItem("result");
      var v_dept_id = this.getParameter('dept_sys_id');

      var dept_detail = new GlideRecord('u_department');
      dept_detail.addQuery('sys_id',v_dept_id);
      dept_detail.query();
	
      var frval;
		
      while(dept_detail.next()){
         result.setAttribute("dept_fr",dept_detail.u_department_name_fr);
      }
   },
   type: 'department_details'
});

On the form itself, I created an onChange Catalog Client Scripts as follows:

find_real_file.png

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	
   if(newValue == ''){
      g_form.setValue('department_fr','');
   }else{
      var ga = new GlideAjax('department_details');
      ga.addParam('dept_sys_id',newValue);
      ga.getXML(ajaxResponse);
   }
	
   function ajaxResponse(serverResponse) {
      var result = serverResponse.responseXML.getElementsByTagName("result");
      var v_dept_fr = result[0].getAttribute("dept_fr");

      g_form.setValue('department_fr',v_dept_fr);
      
   }
}

When I run it, it gives me this Javascript error.

find_real_file.png

I am missing something that I cannot see after staring at this code for 2 hours.

I have no idea what is causing it and it is not working.  I do not know where to start debugging this.  Please help!

 

 

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi MWright,

Change the code as follows:

Script Include:

var DepartmentDetails = Class.create();
DepartmentDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    department_info: function() {
        var v_dept_id = this.getParameter('sysparm_dept_sys_id');
        var dept_detail = new GlideRecord('u_department');
        if (dept_detail.get(v_dept_id)) {
            return dept_detail.u_department_name_fr;
        }
    },
    type: 'DepartmentDetails'
});

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }
    if (newValue == '') {
        g_form.clearValue('u_department_name_fr');
        return;
    }
    var ajax = new GlideAjax('DepartmentDetails');
    ajax.addParam('sysparm_name', 'department_info');
    ajax.addParam('sysparm_dept_sys_id', newValue);
    ajax.getXMLAnswer(function(answer) {
        if (answer.length > 0) {
            g_form.setValue('u_department_name_fr', answer);
        }
    });
}

Execution result:

Step1: Select department

find_real_file.png

Step 2: Clear department

find_real_file.png

View solution in original post

4 REPLIES 4

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi MWright,

I haven't tested it but something like as follows.

Script Include:

var DepartmentDetails = Class.create();
DepartmentDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    department_info: function() {
        var v_dept_id = this.getParameter('dept_sys_id');
        var dept_detail = new GlideRecord('u_department');
        if (dept_detail.get(v_dept_id)) {
            return dept_detail.u_department_name_fr;
        }
    },
    type: 'DepartmentDetails'
});

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }
    if (newValue == '') {
        g_form.clearValue('department_fr');
        return;
    }
    var ajax = new GlideAjax('DepartmentDetails');
	ajax.addParam('sysparm_name', 'department_info');
    ajax.addParam('sysparm_dept_sys_id', newValue);
    ajax.getXMLAnswer(function(answer) {
        if (answer.length > 0) {
            g_form.setValue('department_fr', answer);
        }
    });
}

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi MWright,

Change the code as follows:

Script Include:

var DepartmentDetails = Class.create();
DepartmentDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    department_info: function() {
        var v_dept_id = this.getParameter('sysparm_dept_sys_id');
        var dept_detail = new GlideRecord('u_department');
        if (dept_detail.get(v_dept_id)) {
            return dept_detail.u_department_name_fr;
        }
    },
    type: 'DepartmentDetails'
});

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }
    if (newValue == '') {
        g_form.clearValue('u_department_name_fr');
        return;
    }
    var ajax = new GlideAjax('DepartmentDetails');
    ajax.addParam('sysparm_name', 'department_info');
    ajax.addParam('sysparm_dept_sys_id', newValue);
    ajax.getXMLAnswer(function(answer) {
        if (answer.length > 0) {
            g_form.setValue('u_department_name_fr', answer);
        }
    });
}

Execution result:

Step1: Select department

find_real_file.png

Step 2: Clear department

find_real_file.png

Thank you Hitoshi.

Unfortunately, it didn't work for me.  I still get this:

find_real_file.png

 

 

MWright1
Giga Guru

Thank you!  I finally got it to work.