OnSubmit Client Script Error #not working

Hritik
Tera Expert

Hi Community,

I am getting the below error for OnSubmit Client Script which is referring the Script Include. 

Hritik_0-1707235832117.png

 

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.

18 REPLIES 18

AshishKM
Kilo Patron
Kilo Patron

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

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 .

 

Hritik_0-1707237866871.png

 

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

Hi @AshishKM 

 

Updated the return statement, still not working. 

Hritik_0-1707240464943.pngHritik_1-1707240473144.png

 

What am i missing here? 

Should I try this via After Insert BR ?