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:37 AM
Hi @Abhishek M
I update the client script and used Glide Ajax still it's not working. It has resolved the error that pops up but user field is not being auto populated.
This is my updated code:
function onSubmit() {
var employeeNumber = g_form.getValue('employee_number');
var ga = new GlideAjax('EmployeeUtils');
ga.addParam('sysparm_name', 'getUserNameByEmployeeNumber');
ga.addParam('sysparm_employee_number', employeeNumber);
ga.getXMLAnswer(function(response) {
if (response) {
// Populate the "User name" field on the form with the retrieved user name
g_form.setValue('user_name', response);
} else {
console.error('User not found for the provided employee number');
}
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2024 11:02 PM
Hi @Hritik,
Can you share the script include code, as in your previous script include code, i saw though you said its client callable, i didn't saw ajaxprocessor being extended, also pls add alert in your client script to see what that script include is returning
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2024 01:52 AM
Please refer below post. You might be ending up with an issue being discussed in this post.
Thanks & Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2024 02:23 AM
Hi @Amit Verma
Thanks for the response.
Below is my Client Script:
function onSubmit() {
var employeeNumber = g_form.getValue('employee_number');
var ga = new GlideAjax('EmployeeUtils');
ga.addParam('sysparm_name', 'getUserNameByEmployeeNumber');
ga.addParam('sysparm_employee_number', employeeNumber);
ga.getXMLAnswer(function(response) {
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(response, "text/xml");
var userName = xmlDoc.getElementsByTagName("answer")[0].childNodes[0].nodeValue;
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');
}
});
}
Could you please let me know how can I twik this to get working ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2024 03:16 AM
Hi @Hritik
Try with the following Script Include and On Submit Catalog Client Script. Please note that this will only work from Service Portal not from the native view :
var EmployeeUtils = Class.create();
EmployeeUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
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.getValue('name'); // Assuming 'name' is the field storing the user's name
}
return null;
},
type: 'EmployeeUtils'
});
On Submit Catalog Client Script :
function onSubmit() {
//Type appropriate comment here, and begin script below
if (g_scratchpad.isFormValid)
return true;
var employeeNumber = g_form.getValue('employee_number');
var ga = new GlideAjax('EmployeeUtils');
ga.addParam('sysparm_name', 'getUserNameByEmployeeNumber');
ga.addParam('sysparm_employee_number', employeeNumber);
ga.getXMLAnswer(setAnswer);
return false;
function setAnswer(response) {
var userName = response;
if (userName) {
// Populate the "User name" field on the form with the retrieved user name
g_form.setValue('employee_to_offboard', userName);
} else {
g_form.addErrorMessage('User not found for the provided employee number');
return false;
}
}
var actionName = g_form.getActionName();
g_scratchpad.isFormValid = true;
g_form.submit(actionName);
}
Thanks & Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.