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 08:24 AM
Hi @Hritik ,
Are you using this on any catalog item, then you don't need the client script and script include for name only.
This "Employee Number" is reference field or Text, if its reference then you have the user object already.
Replace the script include with below code.
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;
},
type: 'EmployeeUtils'
};
-Thanks,
AshishKM
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2024 08:44 AM
Hi @AshishKM
The above code did resolved my error but it's not auto populating the user field.
This is applied on catlog form.
employee_number is a single line text type field .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2024 09:03 AM
As the employee to offboard is reference type, the script include should return sys_id of record not the value.
Update the return statement.
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.sys_id; // Assuming 'name' is the field storing the user's name
}
return null;
},
type: 'EmployeeUtils'
};
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2024 09:28 AM - edited 02-06-2024 09:28 AM
Hi @AshishKM
Updated the return statement, still not working.
What am i missing here?
Should I try this via After Insert BR ?