Update a field on a different table through onSubmit() Client Script

veena_kvkk88
Mega Guru

Hi everyone,

I have two custom tables, the latter is displayed as a related list on the former. So when I add a new record in the related list, I need a field to be updated in the current form, i.e. the former table. That field actually has to store the concatenated result of all the columns in the related list table.

I tried to do it using onSubmit() client script on the 2nd table, which calls a script include that updates the field on 1st tables. Sorry if I'm making it confusing. Here is the code.

function onSubmit() {

  var ps = g_form.getValue('u_placeholder_specification');

  var fo = g_form.getDisplayBox('u_first_operand').value;

  var op = g_form.getValue('u_operator');

  var so = g_form.getValue('u_second_operand');

  var co = g_form.getValue('u_condition');

  var request = new GlideAjax('AIM_SetQueryonPS');

  request.addParam('sysparm_name', 'setQuery');

  request.addParam('sysparm_ps', ps);

  request.addParam('sysparm_fo', fo);

  request.addParam('sysparm_op', op);

  request.addParam('sysparm_so', so);

  request.addParam('sysparm_co', co);

  request.getXML(parseResponse);

  function parseResponse(response){

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

  alert(query);

  }

}

The Script Include:

var AIM_SetQueryonPS = Class.create();

AIM_SetQueryonPS.prototype = Object.extendsObject(AbstractAjaxProcessor, {

  setQuery: function(){

  var query = '';

  var ps = this.getParameter('sysparm_ps');

  var fo = this.getParameter('sysparm_fo');

  var op = this.getParameter('sysparm_op');

  var so = this.getParameter('sysparm_so');

  var co = this.getParameter('sysparm_co');

  var gr = new GlideRecord('u_specs');

  gr.addQuery('sys_id', ps);

  gr.query();

  while(gr.next()){

  if(!JSUtil.nil(co)){

  query += fo + " " + op + " " + so + " " + co + "\n";

  }

  else {

  query += fo + " " + op + " " + so + "\n";

  }

  }

  gr.u_query = query;

  gr.update();

  return query;

  },

      type: 'AIM_SetQueryonPS'

});

Please tell me where I'm going wrong. Is this even possible?

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

It is probably not working because the response is probably coming back after the client script finishes running and you've moved on from the form, so the callback function is never executed.   The onSubmit Client Script is one of a very few scenarios where you do not want to run an asynchronous Ajax call.



You would be better off creating an onAfter Business Rule instead to synch up the field.


View solution in original post

5 REPLIES 5

Jim Coyne
Kilo Patron

It is probably not working because the response is probably coming back after the client script finishes running and you've moved on from the form, so the callback function is never executed.   The onSubmit Client Script is one of a very few scenarios where you do not want to run an asynchronous Ajax call.



You would be better off creating an onAfter Business Rule instead to synch up the field.


Chuck Tomasi
Tera Patron

Hi Veena,



Why not do this with an AFTER business rule instead? So much easier.



Business Rules - ServiceNow Wiki


Business Rules Best Practices - ServiceNow Wiki  


veena_kvkk88
Mega Guru

I initially used business rule to achieve this, but I guess something happened and made me switch to client script. I am now trying with a business rule again. On the "identifications" table. I will let you know if it doesn't work again.


Good idea. Let us know if you need help. Nothing on that script screamed "onSubmit" to me. They're best when you need to say "WAIT! Before you really submit this, check this other stuff first!" It wasn't checking so much as setting stuff.