Client script onSubmit is not working as it should be
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-26-2024 02:43 AM - edited ‎02-26-2024 02:45 AM
Hey all, I have a client script that runs onsubmit that get value from field (custom user table reference) and createing new user in sys_user table. after that it populate a filed. I will attach photo to further explain.
as you can see, when the caller field is empty we have a true false check box.
if we press the check box all the fields disappear and we have new field that points on custom user table.
after I choose the inActive employee and press save I want to popluate the caller field and set the check box back to false.
like this for example. it well populate the fields and remove the checkbox (I have ui policy that check if caller field is empty)
my problem is that when I press save first time nothing change and when I press once again it will work.
how can I do it from first try ?
I will attach the scrupts
client script
function onSubmit() {
//Type appropriate comment here, and begin script below
var func = new GlideAjax('customTable');
var user = g_form.getValue('u_inactive_employee');
var usercheck = g_form.getValue('u_didn_t_find_user');
if (user != '' && usercheck == 'true') {
func.addParam('sysparm_name', 'createUserFromOldEmployee');
func.addParam('sysparm_Userid', user);
func.addParam('sysparm_mamash_request', g_form.getUniqueValue());
func.getXML(getResponse);
}
function getResponse(response) {
var values = response.responseXML.documentElement.getAttribute('answer');
if(values != ''){
alert(values);
}
}
}
and the script include function
createUserFromOldEmployee: function() {
var user_sys_id = '';
var Request = this.getParameter("sysparm_request");
var inActiveUser = this.getParameter("sysparm_Userid");
var gr = new GlideRecord('u_past_employees');
gr.get('sys_id', inActiveUser);
gr.query();
if (gr.next()) {
var user = new GlideRecord('sys_user');
user.addQuery('user_name', gr.u_user_id);
user.query();
if (user.next()) {
user.active = true;
user.update();
user_sys_id= user.sys_id;
} else {
var newUser = new GlideRecord('sys_user');
newUser.initialize();
newUser.user_name = gr.u_user_id;
newUser.first_name = gr.u_first_name;
newUser.last_name = gr.u_last_name;
newUser.email = gr.u_email;
newUser.name = gr.u_name;
newUser.state = gr.u_state_province;
newUser.preferred_language = gr.u_language;
newUser.title = gr.u_title;
newUser.active = gr.u_active;
newUser.company = gr.u_company;
newUser.location = gr.u_location;
newUser.department = gr.u_department;
newUser.mobile_phone = gr.u_mobile_phone;
newUser.u_employee_id = gr.u_employee_id;
newUser.u_team = gr.u_team;
newUser.manager = gr.manager;
newUser.update();
user_sys_id= newUser.sys_id;
}
}
var grRequest = new GlideRecord('u_requests');
grRequest.get('sys_id', Request);
grRequest.query();
if (grRequest.next()) {
grRequest.setValue('u_inactive_employee','');
grRequest.setValue('u_didn_t_find_user','false');
grRequest.setValue('u_caller', user_sys_id);
grRequest.update();
return grRequest.u_didn_t_find_user;
}
},

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-26-2024 10:07 AM
Hi there,
One of the issues with your script is the bad practice of performing AJAX calls onSubmit. onSubmit Client Scripts need to run synchronously, because if they don't return false, the form will submit, reload, and stop any scripts that are currently running (or waiting to run; such as your callback function).
For resolution several options can be thought of, like using a GlideSession variable, g_scatchpad or adding a hidden field that is controlled by an onChange Client Script.
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field