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

Hi @Amit Verma 

 

I was using OnSubmit script because my goal was to populate Employee field when request is created. 

 

The actual use of this configuration is when third party is going to send rest call to create request.

So when they send Employee Number my requirement is to populate Employee name based on that.

 

This form is not being used in native UI or service portal only via inbound integration

 

I was under the impression that OnSubmit logic will work here too

Thanks for sharing the original requirement, this is how things go here and there, community trying to fix issues/problem based on given statement only. 😎

 

For REST API, which type of API you are planning, scripted or table.

 

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Hi @AshishKM 

Noted !

 

I will mostly provide Service Catalog API to third party. 

Since this is a Catalog form and fields are on the form I will be using Service Catalog API.

Hugo Barros
Tera Contributor

Hello @Hritik ,
It seems you're trying to call an script include from client script, but using server side syntax. To accomplish this requirement, consider using a GlideAjax call. Please follow below steps:

1 - replace your onSubmit client script by an onChange client script, pointing to the employee_number field;

2 - try below code:

 

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {
        return;
    }

    var employeeNumber = g_form.getValue('employee_number');
    var ga = new GlideAjax('EmployeeUtils');
    ga.addParam('sysparm_name', 'getUserNameByEmployeeNumber');
    ga.addParam('sysparm_number', employeeNumber);
    ga.getXML(getEmployee);

}

function getEmployee(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");

    if (answer) {
		
		if (answer == "notFound"){
			g_form.setFieldMsg("type a message to alert the user that the employee number is invalid");
		}else{
			g_form.setValue('employee_to_offboard', answer);
		}

 

Afterwads, you should also make your Script Include Client callable (update client callable checkbox field to true within your script include record);

Finally, also update your script include function:

 

var EmployeeUtils = Class.create();
EmployeeUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {    //check if the system populates this clause, otherwise client calls wont work. If the system does not update it, create anothe script include with the client callable checkbox as true before saving the record

     getUserNameByEmployeeNumber: function() {
       var employeeNumber = this.getParameter('sysparm_number'); //get the number parameter from GlideAjax call
        var userGr = new GlideAggregate('sys_user'); //since you are not performing any update, consider replacing glide record by glide aggregate to improve performance
        userGr.addQuery('employee_number', employeeNumber);
        userGr.query();
        if (userGr.next()) {
            return userGr.getUniqueValue(); // get sys_id to populate in the employee_to_offboard field 
        }
        return "notFound";
    }
}

    

 


Please make sure that the catalog client script and the script include are from the same aplication. Otherwhise, use the API name (field within script include record) instead of the class name when creating the glide ajax call variable.


Kind regards, and i hope this may help.