- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2016 03:59 PM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2016 04:07 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2016 04:07 PM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2016 04:19 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2016 04:25 PM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2016 04:29 PM
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.