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
Kilo Sage

i have this so far


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

   var userNo = g_form.getReference('avocat_lawyer', myFunc);
   
    function myFunc(userNo){
        var userMP = userNo.location && userNo.title.indexof('Managing Partner');
    g_form.setValue('managing_partner', userMP);
    }
   
}

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Peter Williams ,

 

I am not sure if I understood ur requirement clearly. U need to auto populate the user when the location is let's say London & his title is managing partner?

 

Thanks,

Danish

 

 

 

sorry let me explain a bit better

 

i have this field on my form:

PeterWilliams_0-1697640214264.png

 

Base on the Lawyer Location, Ex. if its London then i need to filter out the User table.

Filter condition is:

Location = London

Title Containing Managing Partner

 

If those condition met then the Managing Partner will get auto populated in this field

PeterWilliams_1-1697640319633.png

 

So i dont need to check if the Lawyers title is Managing Partner but it will use the lawyer location reference to get their Managing Partner filled in the reference field

 

 

 

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