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

In fact i am still getting the same error:

Hritik_2-1707240649032.png

Can we twik the code with GlideAjax ?

Hello @Hritik 

 

Check your addQuery

 

Please mark useful If I helped you

@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 

 

Please mark this as helpful and accept as a solution if this resolves your Ask.
Regards,

Shyamkumar

Hi @shyamkumar VK ,

 

I used the same script include but the User name is not auto populating. 

Hritik_0-1707297774242.pngHritik_1-1707297787294.png

Hritik_2-1707297808206.png

 

 

 Employee Number field is Single Line text field. Not sure where it's getting wrong

Abhishek M
Kilo Guru

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