OnSubmit Client Script Error #not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2024 08:15 AM
Hi Community,
I am getting the below error for OnSubmit Client Script which is referring the Script Include.
Please find my codes as below:
Script Include: (Client Callable is checked)
var EmployeeUtils = Class.create();
EmployeeUtils.prototype = {
initialize: function() {},
getUserNameByEmployeeNumber: function(employeeNumber) {
var userGr = new GlideRecord('sys_user');
userGr.addQuery('employee_number', employeeNumber);
userGr.query();
if (userGr.next()) {
return userGr.getValue('name'); // Assuming 'name' is the field storing the user's name
}
return null;
}
};
Client Script: (OnSubmit)
function onSubmit() {
var employeeNumber = g_form.getValue('employee_number');
var userUtils = new EmployeeUtils();
var userName = userUtils.getUserNameByEmployeeNumber(employeeNumber);
if (userName) {
// Populate the "User name" field on the form with the retrieved user name
g_form.setValue('employee_to_offboard', userName);
} else {
console.error('User not found for the provided employee number');
}
}
My requirement is to populate the user field "employee_to_offboard" On form submission when Employee Number is given.
Please let me know what is it that I am doing wrong.
Thanks,
Hritik.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2024 09:31 AM
In fact i am still getting the same error:
Can we twik the code with GlideAjax ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2024 09:56 AM - edited 02-06-2024 11:19 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2024 10:33 AM
@Hritik ,
Please update Script Include as below
var EmployeeUtils = Class.create();
EmployeeUtils.prototype = {
initialize: function() {},
getUserNameByEmployeeNumber: function() {
var employeeNumber=this.getParameter('sysparm_employee_number')
var userGr = new GlideRecord('sys_user');
userGr.addQuery('employee_number', employeeNumber);
userGr.query();
if (userGr.next()) {
return userGr.getDisplayValue('name'); // Assuming 'name' is the field storing the user's name
}
return null;
}
};
Regards.
Shyamkumar
Regards,
Shyamkumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2024 01:24 AM
Hi @shyamkumar VK ,
I used the same script include but the User name is not auto populating.
Employee Number field is Single Line text field. Not sure where it's getting wrong
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2024 08:31 AM
Hi @Hritik,
You need to use glideajax while calling script include in your client script, refer below article to learn about glideajax
directly calling script include inside client script would not work, it should be called via glideajax.
How to call script include from client script | #servicenow #GlideAjax