The CreatorCon Call for Content is officially open! Get started here.

Clear variable value on an onLoad Client Script

rachelconstanti
Mega Sage

How do I clear a variable value using an onLoad Client Script?

 

The catalog item auto fills a variable (employee type) based on the requested for.   Requested for auto fills when the submitter opens the catalog item.

 

Using an onChange script, the employee type does not populate unless the requested for is changed.

Using an onLoad script, the employee type populates but if I change the 'requested for' the employee type does not change.

 

What can I do to get this to populate correctly?  

 

Thank you for any help you can offer...

1 ACCEPTED SOLUTION

Use the onchange client script and remove the isloading check on it.

-Anurag

View solution in original post

16 REPLIES 16

When I use an onChange script, the employee type does not populate because requested for is populated.

 

When I use onLoad, the employee type populates based on the requested for, however if I change the requested for, the employee type does not change.

 

How can I set this up to have the employee type auto populate onLoad and change if the requested for us changed?

 

 

Hi @rachelconstanti ,

 

Please try the below client script & script include & let me know if it works or not

Client script: onChange

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

var ga = new GlideAjax('EmployeeUtils');
        ga.addParam('sysparm_name', 'FetchEmployeeType');
        ga.addParam('sysparm_requestor', g_form.getValue('requested_for'));
        ga.getXML(rollback);

        function rollback(response) {
            var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_employee_type',answer);//Use proper backend names of the variable

}
}

 

Script Include: Client Callable should be true

 

var EmployeeUtils = Class.create();
EmployeeUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

	FetchEmployeeType: function(){

		var requestor = this.getParameter('sysparm_requestor');
		var employeeType = '';

		var gr = new GlideRecord('sys_user');
		if(gr.get(requestor)){
			employeeType = gr.u_employee_type;//Use proper backend names of variables
		}
		return employeeType;

	},

    type: 'EmployeeUtils'
});

 

Thanks,

Danish