The CreatorCon Call for Content is officially open! Get started here.

How to call form variables from a Catalog Client Script (Script Includes)

Jeff16
Tera Contributor

How do I call form variables from an onChange catalog client script using script includes?  The returned answer I'm getting is empty:

Catalog Client Script:

if(newValue != ""){
  g_form.hideFieldMsg('employee', true);
}

var ga = new GlideAjax("getEmployeeRequestedItem");
ga.addParam('sysparam_newValue', newValue);
ga.getXML(getRequestedItem);

function getRequestedItem(response){
  var answer = response.responseXML.documentElement.getAttribute("answer");
  if(answer == newValue){
    g_form.showFieldMsg('employee', "There is already an Active Something for this Employee", 'error');
    g_form.setValue('employee', "");
  }
}

Script Includes:

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

  getRequestedItem: function(){
    var ritmSysID = "";
    var userSysID = this.getParameter('sysparam_newValue');

    var gr = new GlideRecord('sc_req_item');
    gr.addQuery('active', true);
    gr.addQuery('short_description', 'Something');
    gr.query();

    while(gr.next()){
      if(gr.variables.employee == userSysID){
        ritmSysID = gr.variables.employee;
        break;
      }
    }
  return ritmSysID;
  },
type: 'getEmployeeRequestedItem'
});

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Fixed 2 items in client script

1. Client script needs to specify the name of function in Script Include.

2. Changed order of .setValue() and .showFieldMsg(). Setting showFieldMsg() and then .setValue() will hide field message.

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax("getEmployeeRequestedItem");
    ga.addParam('sysparm_name', 'getRequestedItem');
    ga.addParam('sysparam_newValue', newValue);
    ga.getXML(getRequestedItem);

    function getRequestedItem(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == newValue) {
            g_form.setValue('employee', "");  // need to set value to empty before setting error message
			g_form.showFieldMsg('employee', "There is already an Active Something for this Employee", 'error');
        }
    }
}

Script Include:

var getEmployeeRequestedItem = Class.create();
getEmployeeRequestedItem.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getRequestedItem: function() {
        var ritmSysID = "";
        var userSysID = this.getParameter('sysparam_newValue');
        var gr = new GlideRecord('sc_req_item');
        gr.addQuery('active', true);
        gr.addQuery('short_description', 'Something');
        gr.query();

        while (gr.next()) {
            if (gr.variables.employee == userSysID) {
                return gr.variables.employee;
            }
        }
        return ritmSysID;
    },
    type: 'getEmployeeRequestedItem '
});

Execution result:

find_real_file.png

View solution in original post

11 REPLIES 11

vkachineni
Kilo Sage

In a background script window run this script to see what are the keys for a RITM with the matching criteria. See if "employee" a variable. 

var getRitm = new GlideRecord('sc_req_item');
getRitm.addQuery('number', 'RITM0010040'); //replace with your RITM
getRitm.query();

if (getRitm.next()) {
    var keys = Object.keys(getRitm.variables);
    gs.print(keys);
}

Also you are looping using a while loop. May be the last RITM does not have an employee?

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

gs.print(keys.toString());

Yes, employee is a variable.  Also, I've run tests on the query, so I know it does produce results, however, when I gs.addInfoMessage(answer); it returns an empty value. Thanks.

Did you make the changes to the Client script and Script include as @Hitoshi Ozawa suggested.

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Gaurav Shirsat
Mega Sage

Hello

could you please return your ritmSysID as string by using  return ritmSysID.toString();

Mark my Response as Correct or Helpful, if you find it Appropriate.
Gaurav Shirsat
https://www.linkedin.com/in/gauravshirsat/