OnLoad Client Script - How to auto populate / fill up user details for the fields ??

Jason Lau Chang
Giga Contributor

Hi Team,

Would like to create the OnLoad Catalog Client Script that when the user is loading the form, it will auto populate / fill up some user details in the form, how do I start ??

Thank you!!

 

1 ACCEPTED SOLUTION

Hi Jason

Basically you want to populate the requested for as soon as the form loads

If yes there are 2 ways 

1.Default Value :You can use it by modifying the variable default value

find_real_file.png

 

2. In onload client script use

g_form.setValue('requested_for',g_user.userID);

Regards

Pranav

 

View solution in original post

10 REPLIES 10

Pranav Bhagat
Kilo Sage

Try this on the onload script

 

var user = g_form.getReference('referenc field name', populateInfo);
	function populateInfo(user) {
		
g_form.setValue('field location', user.location);
g_form.setValue('field2', user.manager);
	
		
	}

 

You can also do a Glide Ajax 

Example

Add this onChange Script in the Requested by:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	//Type appropriate comment here, and begin script below
	
	var gajax = new GlideAjax('populateUtils');
	gajax.addParam('sysparm_name', 'getuserdetails');
	gajax.addParam('sysparm_user', newValue);
	gajax.getXML(setCIDetails);
	
	function setCIDetails(serverResponse) {
		var cc = serverResponse.responseXML.documentElement.getAttribute("answer");
		g_form.setValue('field name', cc.location);
		
		
	}
	
}

And this script include:

var populateUtils= Class.create();
populateUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getuserdetails: function () {
		var usr = this.getParameter('sysparm_user');
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id',usr);
		gr.query();
		
		if (gr.next()) {
			return gr;
			
		}
		else {
			return '';
		}
		
		
	},
	
	
	type: 'populateUtils'
});

 

Reference Documents

https://community.servicenow.com/community?id=community_question&sys_id=e681f1d0db74f3845ed4a851ca961983

 https://community.servicenow.com/community?id=community_question&sys_id=9d42a370dbcabb8023f4a345ca9619dc

https://www.covestic.com/servicenow-code-snippets-catalog-items-and-the-portal-widget-data-object/ 

 

Regards

Pranav

Hi Pranav,

Thanks for trying to help, for the OnLoad Script that you provided below, how do I know the "reference field name" ?? Or where can I find it ??

Thanks again man !!

var user = g_form.getReference('referenc field name', populateInfo);
	function populateInfo(user) {
		
g_form.setValue('field location', user.location);
g_form.setValue('field2', user.manager);
	
		
	}

Hi Jason

 

Is there any variable which is referencing to the user table ?

If yes then that will be the reference field.

 

OR

 

If you just want the logged in user details to filled in . try this

 

var user = new GlideRecord('sys_user');
user.addQuery('sys_id', g_user.userID);
user.query(userCallBack);

function userCallBack(user) {
if(user.next()) {
g_form.setValue('field 1', user.email);
g_form.setValue('field 2', user.phone);
}
}

 

Some good example for glide ajax for your reference

https://community.servicenow.com/community?id=community_question&sys_id=3902b2b4dbab2f0011762183ca9619bf

Regards

Pranav

Hi Pranav,

>> Is there any variable which is referencing to the user table ? If yes then that will be the reference field.

Basically I have a form, once the user click to open the form, it will trigger the OnLoad Script, then inside the form, there is a question "Requested For:" (normally user will need to search for their name) (Variable name for this field is "requested_for") 

So would like to make this "Requested For:" field auto fill up the user name, can I say the reference field for the code above is "requested_for" ?? (the variable name of the question field??)

 

Thanks again!!! Cheers~~