OnSubmit Catalog client script to update custom field value

joshualazzarus
Mega Contributor

Hello,

I have a requirement to get the variable value & check in my custom table for matching record & then to update the state value to 'completed' in my custom table.

I am using a OnSubmit catalog client script  but it is not updating my custom table field state.

Type:OnSubmit

Catalog Item: Source Form Details

Script:

function onSubmit() {
//Type appropriate comment here, and begin script below
var sId = g_form.getValue('source_id'); // variable value
var gr = new GlideRecord('u_customtable'); // custom table
gr.addQuery('u_source','=',sId); // checking source id is available in my custom table
gr.query();
if(gr.next()){
var sts = gr.u_state;
g_form.setValue(sts,'Completed'); // setting the value of my custom table to 'Completed' but not working.
gr.update();
}
else{
// do nothing
}
}

Is this a correct approch or iam missing something. Plz help.

Thx,

JR

5 REPLIES 5

Anil Lande
Kilo Patron

Hi,

You cannot use GlideRecord Query for updating record in Client script.

Instead use a GlideAjax call make a call to function and perform update operation in script include.

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Ankur Swami
Tera Guru

Hi,

 

This is not the right approach to do that. You can not do the server query from the client without calling a script include. 

You have to call a script include by using a GlideAjax, The returned value from the Script include should set to the field.

Anil Lande
Kilo Patron

Please find below link to learn how to use glide ajax:

 

https://community.servicenow.com/community?id=community_article&sys_id=9f7ce2e1dbd0dbc01dcaf3231f961...

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Ankur Bawiskar
Tera Patron
Tera Patron

@joshualazzarus 

Better approach is to use workflow run script since you must be having already workflow for your catalog item

Use this in workflow run script

var sId = current.variables.source_id;

var gr = new GlideRecord('u_customtable'); // custom table
gr.addQuery('u_source', sId); // checking source id is available in my custom table
gr.query();
if(gr.next()){
	gr.setValue('u_state','Completed'); // setting the value of my custom table to 'Completed' but not working.
	gr.update();
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader