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 @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');
        }
    });
}

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

 

AbhishekM_0-1707289309938.png

 

@Hritik 

 

Please refer below post. You might be ending up with an issue being discussed in this post.

https://www.servicenow.com/community/developer-forum/calling-a-script-include-from-onsubmit-client-s...

https://www.servicenow.com/community/developer-articles/getxmlwait-alternative-for-service-portal/ta...

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

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 ?

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'
});

 

AmitVerma_0-1707304364324.png

 

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);
}

 

image.png

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.