Field value getting cleared OnLoad

Ajay37
Tera Contributor

Hi All,

I am populating Manager of user in opened_by field using script include and Glide Ajax. The opened_by field has logged in user by default. So I want it to work on both onLoad and onChange. The thing here is, for suppose user in opened_by field has no manager, and If I enter it manually and then save the form. The Value is getting cleared once the form is loaded. How to prevent it?

Script Include:

var userDetails = Class.create();
userDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    managerandtitle: function() {
var manager = '';
var title = '';
        var user1 = this.getParameter('sysparm_user');
        var gr1 = new GlideRecord('sys_user');
        gr1.addQuery('sys_id', user1);
        gr1.query();
        if (gr1.next()) {
            manager = gr1.manager;
            title = gr1.title;
			}
        }
        return manager + ',' + title;
    },
    type: 'userDetails'
});

 

Client Script:

function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {

retrun;

}

var ga1 = new GlideAjax('userDetails');
ga1.addParam('sysparm_name', 'managerandtitle');
ga1.addParam('sysparm_user', newValue);
ga1.getXML(user_info);

function user_info(response) {

var ans = response.responseXML.documentElement.getAttribute('answer');
var ans1 = ans.split(",");
g_form.setValue('u_manager', ans1[0]);
g_form.setValue('u_title', ans1[1]);
}


}

 

1 ACCEPTED SOLUTION

Hi Ajay,

To keep it simple and easy

I would suggest this

1) Keep default value in both title and manager so that if logged in user has Manager and Title then it auto-populates

2) Then ensure your onChange client script doesn't run on load and the Manager + Title is cleared when User is cleared

Default value for manager:

javascript: gs.getUser().getManagerID();

Default value for title field

javascript: gs.getUser().getRecord().getValue('title');

Updated Client Script:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading) {
		return;
	}

	// if new value is cleared then clear manager and title both
	if(newValue == ''){
		g_form.clearValue('u_manager');
		g_form.clearValue('u_title');
	}
	else{
		var ga1 = new GlideAjax('userDetails');
		ga1.addParam('sysparm_name', 'managerandtitle');
		ga1.addParam('sysparm_user', newValue);
		ga1.getXML(user_info);
		function user_info(response) {
			var ans = response.responseXML.documentElement.getAttribute('answer');
			var ans1 = ans.split(",");
			g_form.setValue('u_manager', ans1[0]);
			g_form.setValue('u_title', ans1[1]);
		}
	}
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

33 REPLIES 33

you should use different script for different use, create onload script to populate data onload of form and create onchange script for onchange of field. It will solve your most of the problems.  

Hi @Pranesh ,

Sorry for this I have tested now and the script you updated is not working. 

If I write onLoad and Onchange script differently, 

For suppose, opened_by user has no manager, so when onLoad script runs, it will result null value in manager field. If I enter manager name manually, and save the form, the onLoad script will run again right? and It clears the value right as opened_by user not having manager.

Thanks

Hi Ajay,

please check my above comment to keep it easy and simple

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

you can run the onload on new record only, it will work fine.

 

g_form.isNewRecord()