Logged in requested for details auto populate

zeerodger27
Tera Contributor

I have written the below catalog client script, when the requested for field has changed the city, state, location has to be populated, but how to populate the current logged in user details for the same fields?

 

OnChange Catalog client script

function onChange(control, oldValue, newValue, isLoading) {

   if (isLoading || newValue == '') {

      return;

   }

 

var ga = new GlideAjax('User');

    ga.addParam('sysparm_name', "checkRecordPresent");

    ga.addParam('sysparm_userID', g_form.getValue('requested_for'));

    ga.getXMLAnswer(function(answer){

        if(answer != 'not found'){

            var parser = JSON.parse(answer);

            g_form.setValue('req_manager', parser.u_reference_1);

            g_form.setValue('location', parser.location);

            g_form.setValue('city', parser.u_city);

            g_form.setValue('state', parser.state);

           

        }

    });

   //Type appropriate comment here, and begin script below

   

}

 

9 REPLIES 9

@Kishor O 

 

Script Include:

 

var User = Class.create();

User.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

    checkRecordPresent: function(){

        var obj = {};

        var id = this.getParameter('sysparm_userID');            

        var gr = new GlideRecord('sys_user');

        gr.addQuery('sys_id', id);

        gr.query();

        if(gr.next()){

            obj['u_reference_1'] = gr.getValue('u_reference_1');

            obj['location'] = gr.getValue('location');

            obj['u_city'] = gr.getValue('u_city');

            obj['state'] = gr.getValue('state');

           

        }

        return JSON.stringify(obj);

    },

 

    type: 'User'

});

soumyagupta
Tera Contributor

Hi @zeerodger27 ,

 

You can try the following code for your script include:

var User = Class.create();

User.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

    checkRecordPresent: function(){

        var obj = {
                 u_reference_1 : '',
                 location : '',
                 u_city : '',
                 state : ''
};

        var id = this.getParameter('sysparm_userID').toString();            

        var gr = new GlideRecord('sys_user');

        gr.addQuery('sys_id', id);

        gr.query();

        if(gr.next()){

            obj.u_reference_1 = gr.getValue('u_reference_1').toString();

            obj.location = gr.getValue('location').toString();

            obj.u_city = gr.getValue('u_city').toString();

            obj.state = gr.getValue('state').toString();
        }

        return JSON.stringify(obj);
    },

    type: 'User'

});

 

Please mark correct by selecting "Accept as Solution" and " Helpful" if this solves your issue.

 

Regards,

Soumya. 

If it did not work try to modify code like below.

if(gr.next()){

            obj.u_reference_1 = gr.u_reference_1;

            obj.location = gr.location

            obj.u_city = gr.u_city;

            obj.state = gr.state;
        }

soumyagupta
Tera Contributor

@zeerodger27 ,

 

Also, use "gs.getUserID();" in the default value of the 'Requested for' field to get the current logged-in user as requested for.

 

Please mark correct by selecting "Accept as Solution" and " Helpful" if this solves your issue.

 

Regards,

Soumya. 

 

chetanb
Tera Guru

Hello @zeerodger27 

 

Pls try below code.

 

function onChange(control) {
if (control.id === 'requested_for') {

populateLocationFields(control.getValue());

populateUserFields();
}
}

function populateLocationFields(requestedFor) {

}

  1. function populateUserFields() {
    // Get the current user
    var currentUser = gs.getUser();
    g_form.setValue('city', currentUser.getCity());
    g_form.setValue('state', currentUser.getState());
    g_form.setValue('location', currentUser.getLocation());
    }

Please mark my answer helpful and correct if it solves your issue 

 

Regards,

CB