Catalog client script to get department ID in service portal

lando321
Tera Contributor

Hello all,

Im trying to get the department ID on a catalog item, but i am having trouble getting this information to display in the service portal. 

Here is my form: 2 fields shown are in a variable set

lando321_0-1678477521198.png

I have tried a few catalog client scripts but none of them are working for me, upon searching i found that you have to use a callback function in the portal, but this callback function is still not working for me.

 

Catalog client script:

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


    var user = g_form.getReference('name', callBack);

    function callBack(user) {


        g_form.setValue('department_code1', user.department.id);

    }
}

 

lando321_1-1678477979770.png

Could someone help give me some direction on this one?

1 ACCEPTED SOLUTION

Paul Deonarine3
Tera Expert

Can you try with below script, 

 

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

  // Get the user reference value from the "name" field
  var userRef = g_form.getValue('name');

  // Use the GlideRecord API to retrieve the user record and its associated department
  var userGr = new GlideRecord('sys_user');
  userGr.addQuery('sys_id', userRef);
  userGr.query(function(result) {
    if (result.next()) {
      var departmentRef = result.getValue('department');
      var departmentGr = new GlideRecord('cmn_department');
      departmentGr.addQuery('sys_id', departmentRef);
      departmentGr.query(function(result) {
        if (result.next()) {
          // Set the value of the "department_code1" field to the department ID
          g_form.setValue('department_code1', result.getValue('department_code'));
        }
      });
    }
  });
}

 

View solution in original post

10 REPLIES 10

Thanks a lot Brad for your assistance, i really appreciate your time!

Paul Deonarine3
Tera Expert

There's a typo in your script include. JSON.strigify should be JSON.stringify. This is likely causing the issue where returneddata is undefined in your client script.

Try changing this line:

 

return JSON.strigify(results);

 

to:

 

return JSON.stringify(results);

 

Also, in your client script, it looks like you're trying to pass three arguments to g_form.setValue() instead of two. The function only takes two arguments, so you should remove the returneddata.name argument. Change this line:

 

g_form.setValue('department_code1', returneddata.id, returneddata.name);

 

to:

 

g_form.setValue('department_code1', returneddata.id);

 

Once you've made those changes, try running your script again and see if it's working as expected.

Hi @Paul Deonarine3 thanks for the reply,

Made those updates, no longer returning undefined, but still not setting the value of that particular field.

Paul Deonarine3
Tera Expert

Can you try with below script, 

 

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

  // Get the user reference value from the "name" field
  var userRef = g_form.getValue('name');

  // Use the GlideRecord API to retrieve the user record and its associated department
  var userGr = new GlideRecord('sys_user');
  userGr.addQuery('sys_id', userRef);
  userGr.query(function(result) {
    if (result.next()) {
      var departmentRef = result.getValue('department');
      var departmentGr = new GlideRecord('cmn_department');
      departmentGr.addQuery('sys_id', departmentRef);
      departmentGr.query(function(result) {
        if (result.next()) {
          // Set the value of the "department_code1" field to the department ID
          g_form.setValue('department_code1', result.getValue('department_code'));
        }
      });
    }
  });
}

 

This turned out to be the solution that worked, with a slight modification of changing 'department_code' to 'id". Really appreciate the help, are there any resources you could share on becoming more familiar with script includes and when to use them?