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

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

veena_kvkk88
Mega Guru

Thank you so much everyone! I wrote an after business rule on the second table and it worked perfectly. When I tried business rule approach at first, I used before rule. So I jumped to client script. Anyway, you've all been a great help!