Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Autopopulate the fields(country, Region) in catalog item as user login

Sharada N S
Tera Contributor

Hi everyone,

In catalog item for logged- in user country and region needs to be auto populated. we have a variable set were we get values of  Requested for(user name from sys_user table) and location(deatils from cmn_loaction table) which is auto populated. In the same way need a onload client script to Auto Populate 'Country' and 'Region'(Single Line text fields) for logged in user (using the location field present in variable set).

BanashriBM1_0-1711360649865.png

Thanks

 

 

 

7 REPLIES 7

Ayushi12
Mega Sage

Hi @Sharada N S,

You can create a client script and script include to auto-populate the details. Please find the below link for reference.

https://www.servicenow.com/community/itsm-forum/auto-populate-requested-for-details-for-a-catalog-it...

Please mark this response as correct or helpful if it assisted you with your question.

swathisarang98
Giga Sage

Hi @Sharada N S ,

 

You can create a onload client script and call script include and return the value countrey and region and set it in the form 

 

Client script:

swathisarang98_0-1711381517760.png

 

function onLoad() {
   //Type appropriate comment here, and begin script below
   var sys_id = g_user.userID;
   var glAj = new GlideAjax('getUserCountry');
   glAj.addParam('sysparm_name','getUser');
   glAj.addParam('sysparm_sysId', sys_id);
   glAj.getXML(getResponse);

   
}
function getResponse(response){
	var ans = response.responseXML.documentElement.getAttribute('answer').split(',');
	alert('ans' + ans);
	g_form.setValue('country', ans[0]);
	g_form.setValue('region',ans[1]);
}

 

 

Script include:

swathisarang98_1-1711381556996.png

 

var getUserCountry = Class.create();
getUserCountry.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getUser: function(){
		var sys =this.getParameter('sysparm_sysId');
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id',sys);
		var arr = [];
		gr.query();
		if(gr.next()){
			arr.push(gr.location.country.toString());
			arr.push(gr.location.state.toString()); 
		}
		gs.info('country is ' + arr);
		return arr.toString();

	},

    type: 'getUserCountry'
});

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang