Call two script include in one Client Script?

zrocky
Giga Contributor

Hello SN Community,

Is it possible to call two script include, compare their values, and if they match auto-populate a field?

1st Script Include

function getCostCenter(record) {

  var ccListProperty = gs.getProperty('aitqa_cost_centers');

  var ccArr = ccListProperty.split(',');

  var result = '';

  var gr = new GlideRecord('sys_user');

  gr.addQuery('sys_id', current.u_resource_manager);

  gr.query();

  while (gr.next()){

  if (gr.u_sector == 'Ann-Quality Assurance') {

  var qacc;

  for (i=0; i<ccArr.length;i++) {

  qacc = new GlideRecord('cmn_cost_center');

  qacc.addQuery('name', ccArr[i]);

  qacc.query();

  while (qacc.next()) {

  result += qacc.sys_id + ',';

  }

  }

  }

  result += gr.cost_center + ',';

  }

  return 'sys_idIN' + result;

}

2nd Script Include

function getCostCenter(record) {

  var ccListProperty = gs.getProperty('aitqa_cost_centers');

  var ccArr = ccListProperty.split(',');

  var result = '';

  var gr = new GlideRecord('sys_user');

  gr.addQuery('sys_id', current.u_resource_manager);

  gr.query();

  while (gr.next()){

  if (gr.u_sector == 'Ann-Quality Assurance') {

  var qacc;

  for (i=0; i<ccArr.length;i++) {

  qacc = new GlideRecord('cmn_cost_center');

  qacc.addQuery('name', ccArr[i]);

  qacc.query();

  while (qacc.next()) {

  result += qacc.sys_id + ',';

  }

  }

  }

  result += gr.cost_center + ',';

  }

  return 'sys_idIN' + result;

}

I would like to write a Client Script where I can call these two Script Includes, execute, match their fields (Cost Center Code needs to match with CCOwner, i.e. 123345 == John Doe) and if they match. Auto-populate Cost Center Owner field on a form.

8 REPLIES 8

zrocky
Giga Contributor

Hello SN,



Again, I appreciate the help and suggestions. I messed up the second Script Include it shoould've been this one.



function getCCOwner(record) {


  var gr = new GlideRecord('cmn_cost_center');


  gr.addQuery('sys_id',record.u_cost_center_code);


  gr.query();


  var result = '';


  while (gr.next()){


  result += gr.manager + ',';


  }


  return 'sys_idIN' + result;


}



I will try BR and hopefully I can make it work today. What I am trying to achieve to is auto populate a field 'Cost Center Owner' based on 'Cost Center Code', but Cost center Code choice list is triggered based on another field which is 'Resource Manager'. So...



User selects a 'Resource Manager', 'Cost Center Code' populates the choice list based who is the Resource Mgr. once CCCode is selected the goal is to auto populate Cost Center Ownership based on CCCode.


zrocky
Giga Contributor

Just to close this issue I will post the code for client script and Script Include



Client Script


var ga = new GlideAjax('popOwner');


ga.addParam('sysparm_name','helloWorld');


ga.addParam('sysparm_user_name',g_form.getValue('u_cost_center_code'));


ga.getXML(HelloWorldParse);


function HelloWorldParse(response) {


  var answer = response.responseXML.documentElement.getAttribute("answer");


  g_form.setValue('u_approving_vp', answer);


}



Script Include



var popOwner = Class.create();
popOwner.prototype = Object.extendsObject(AbstractAjaxProcessor, {
helloWorld: function() {
  var rec = new GlideRecord('cmn_cost_center');
  rec.addQuery('sys_id',this.getParameter('sysparm_user_name'));
  rec.query();
  while(rec.next()) {
    return rec.manager;
  }
},


      type: 'popOwner'
});



Thanks for the support SN community!


Just curious 😃 Did you keep the HelloWorld in the code? 😃


Yes, honestly I didn't write the code. Another developer wrote the code. I was surprised about that as well. I'm a newbie in coding.