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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

try to add some alert and check

also issue in your client script here

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

return;

}

Regards
Ankur

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

Hi @Ankur Bawiskar ,

What needs to be modified, could you please update?

Thanks

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

Hi Ankur,

Thanks for the update. Could you please tell me, What is the default script, if I need to populate title of Manager but not user in opened_by field. And also I have one doubt in using default value, as I read in using dot-walking method, it doesn't show values while generating reports of this table. So similar to this, do any issues occur  if we use default value method?

Thanks Ankur.