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

Hi,

You will have to configure the dictionary for that field and in the default value place those 2 lines of respective fields

Default value will work only once when new record is opened

There is no relationship of using default value and reporting

Regrds
Ankur

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

Hi @Ankur Bawiskar ,

I have placed these lines in Manager field default value, but not working. I have checked logged-in user record, it has manager and manager is active as well.

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

And I have tried with below lines, but getting the error.

javascript:gs.getUser().getRecord().getDisplayValue('manager');

Error: Illegal access to method getRecord() in class com.glide.sys.User

Hi,

are you in scoped application?

Regards
Ankur

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

No Ankur, In Global only.

Hi,

please share the dictionary configuration

Regards
Ankur

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