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.

Need to auto populate reference field base on sign in user location

Peter Williams
Kilo Sage

So i have this requirement that i am not able to figure out

 

i have this catalog item request for. 

What i need is, when a user logs into the form, it will detect their location

and base on their location i need to search for the title Managing Partner and have that user auto fill the reference field

 

i was thinking of a on change client script but not quiet sure on how to script it out for that

 

 


  function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   var userNo = g_form.getReference('avocat_lawyer', myFunc);
   
    function myFunc(userNo){

    g_form.setValue('managing_partner', userNo.u_preferred_name);
    }
   
}
1 ACCEPTED SOLUTION

Hello @Peter Williams ,

You can use GlideAjax to get the managing Partner.

Use the below code in on change client script:-

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('getManagingPartner');
    ga.addParam('sysparm_name', 'getMP');
    ga.addParam('sysparm_lawyer', newValue); //newValue will look up field for
    ga.getXML(callback);

    function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('managing_partner', answer);

    }

}

 

Create a script include named 'getManagingPartner' and make it client callable.

Use the below script in the script include:-

 

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

    getMP: function() {
	var loc;
        var lawyer = this.getParameter('sysparm_lawyer');
        var gr = new GlideRecord("sys_user"); 
        gr.addQuery("sys_id", lawyer);
        gr.query();
        if (gr.next()) {
             loc = gr.location.toString();
        }
	var mp = new GlideRecord('sys_user');
	mp.addQuery('location',loc);
	mp.addEncodedQuery('titleLIKEManaging Partner');
	mp.query();
	if(mp.next()){
	return mp.sys_id.toString();
	}
    },

    type: 'getManagingPartner'
});

 

 

If this response clears up your doubt, kindly flag it as both helpful and correct.

Thanks,

Alka

View solution in original post

12 REPLIES 12

@Peter Williams yes

Please mark it helpful also.😊

Hi @Peter Williams ,

 

U need to go with Client script & Script include for this.

 

Client Script: OnChange & UI Type all

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   //Type appropriate comment here, and begin script below
   var user = g_form.getValue('lawyer');// Use proper backend name from your form

   var gr = new GlideAjax('fetchPartnerUtils');
   gr.addParam('sysparm_name','fetchPartner');
   gr.addParam('sysparm_user',user);
   gr.getXMLAnswer(updatePartner);

   function updatePartner(answer){
	if (answer){
		g_form.setValue('managing_partner',answer); // Use proper backend name from your form Also assuming Managing partner is reference field
	}else{
		alert('No user found');
	}
   }

}

 

Script Include:

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

	fetchPartner: function(){

		var user = this.getParameter('sysparm_user');

		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id',user);
		gr.query();
		if(gr.next()){
			var location = gr.location;
		}

		var manager = new GlideRecord('sys_user');
		manager.addQuery('location',location);
		manager.addEncodedQuery('titleLIKEManaging Partner');
		manager.query();
		if(manager.next()){
			var id = manager.sys_id;
		}

		return id;

	},

    type: 'fetchPartnerUtils'
});

DanishBhairag2_0-1697642182064.png

 

Kindly try the above code & let me know.

 

Thanks,

Danish

 

 

getting browser script error and not populating