Auto populate details using Glide ajax script

suresh1995
Tera Contributor

Hi All,my task is while selecting manager field then automatically manager's location and company details will populated in specific field but it not working, kindly find my script include & client script

 

script include:

var Autopopulate = Class.create();
Autopopulate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getCompanyAndlocation: function(){
    
        var caller = this.getParameter('sysparm_caller');
        var grUser = new GlideRecord('sys_user');
        grUser.get(caller);
      var grCompany = new GlideRecord('core_company');
grCompany.get(grUser.company.toString());
var usrobj = {};
usrobj.company = grUser.company.toString();
usrobj.location = grCompany.location.toString();
return JSON.stringify(usrobj);
  
},
type: 'Autopopulate'
});
client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var ga = new GlideAjax('Autopopulate');
   ga.addParam('sysparm_name''getCompanyAndlocation');
   ga.addParam('sysparm_caller', g_form.getValue('u_manager').toString()); 
   ga.getXML(populateSelect);

function populateSelect(response) {
   var answerJSON = response.responseXML.documentElement.getAttribute("answer");
   var answer = JSON.parse(answerJSON);
   g_form.setValue('u_location',answer.location.toString());
   g_form.setValue('u_company',answer.company.toString());
   
   //Type appropriate comment here, and begin script below

   //Type appropriate comment here, and begin script below
}
 
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@suresh1995 

location is from user table and not company table

so update script as this

var Autopopulate = Class.create();
Autopopulate.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getCompanyAndlocation: function(){
		var caller = this.getParameter('sysparm_caller');
		var grUser = new GlideRecord('sys_user');
		if(grUser.get(caller)){
			var usrobj = {};
			usrobj.company = grUser.company.toString();
			usrobj.location = grUser.location.getDisplayValue();
			return JSON.stringify(usrobj);
		}
	},

	type: 'Autopopulate'
});

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

3 REPLIES 3

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

What exactly is not working? Are you running into an error? Not getting any details? Etc? What information can you share?

 

Have you also tried debugging your script? Doing so, will shorten your search (and ours 😁) immensely.

 

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Ankur Bawiskar
Tera Patron
Tera Patron

@suresh1995 

location is from user table and not company table

so update script as this

var Autopopulate = Class.create();
Autopopulate.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getCompanyAndlocation: function(){
		var caller = this.getParameter('sysparm_caller');
		var grUser = new GlideRecord('sys_user');
		if(grUser.get(caller)){
			var usrobj = {};
			usrobj.company = grUser.company.toString();
			usrobj.location = grUser.location.getDisplayValue();
			return JSON.stringify(usrobj);
		}
	},

	type: 'Autopopulate'
});

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Mark Roethof
Tera Patron
Tera Patron

Just browsing your script VERY quickly, I did notice several not-best practices.

 

getXML => Use getXMLAnswer

setValue on references => User the second parameter

.get => Wrap this in an If statement

 

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn