auto populate manager details based on user selected in requested for field

suuriya
Tera Contributor

Hi Community,

 

I have a requirement, in catalog form there is variable set in that there is 2 fields called 

common_vars_requested_for (reference) and common_vars_also_notify (list collector) both refers to user table.
 
When a user name is selected in common_vars_requested_for field then the manager of the person needs to be populate in common_vars_also_notify field and this field needs to be readonly.
So i have written ui policy and made the field readonly only for that particular catalog.
Im struck with autopop of manager 
written onchnage client script:
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

  var business = g_form.getValue('common_vars_requested_for');
    var ajax = new GlideAjax('Autopopulatemanager');
    ajax.addParam('sysparm_name', 'getDetails'); // function name
    ajax.addParam('sysparm_business', business); // business value
    ajax.getXML(detailParse); // call back function

    function detailParse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var unit = JSON.parse(answer); // parse response
        g_form.setValue('common_vars_also_notify', unit[0].common_vars_also_notify); //set owner
    }
   
}
Script include:
var Autopopulatemanager = Class.create();
Autopopulatemanager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDetails: function() {
        var details = [];
        var business= this.getParameter('sysparm_business');
        var userGR = new GlideRecord('sys_user');
        userGR.addQuery('common_vars_requested_for', business); // set query
        userGR.query();
        while (userGR.next()) {
            var users = {};
            users.business= userGR.common_vars_also_notify.getDisplayValue();
            details.push(users);
        }
        return JSON.stringify(details);
 },
    type: 'Autopopulatemanager'
});
But it is not working can you please let me know what mistake i have made.
 
Thanks in advance
9 REPLIES 9

dgarad
Giga Sage

Hi @suuriya 

List collector variable (common_vars_also_notif) must be referencing to some table (check in type specification).

Now when you have list collector referencing to table then from script it will be expecting a sys_id of records from its referenced table.

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

Pratiksha
Mega Sage
Mega Sage

var Autopopulatemanager = Class.create();
Autopopulatemanager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDetails: function() {
var details = [];
var business = this.getParameter('sysparm_business');

// Query the user record based on the sys_id
var userGR = new GlideRecord('sys_user');
userGR.addQuery('sys_id', business);
userGR.query();

if (userGR.next()) {
// Fetch the manager of the selected user
var manager = userGR.manager.getDisplayValue();
if (manager) {
// Add the manager's details to the response
var user = {};
user.manager = manager;
details.push(user);
}
}

return JSON.stringify(details);
},
type: 'Autopopulatemanager'
});

 

Try this

HI @Pratiksha  ,

 

Thanks for the reply

I tried the above script include but it didnt worked...in client script the way im setting is wrong?

suuriya_0-1715262281445.png

 

suuriya
Tera Contributor

HI @Pratiksha  ,

 

I figured it out in client script changed from g_form.setValue('common_vars_also_notify', unit[0].common_vars_also_notify); //set owner to g_form.setValue('common_vars_also_notify', unit[0].manager); 

 

Now it is working fine when requested for is changed but when the form loads then the logged in user will be displayed in requested for field so in this case it is not working.

as client script is written only for onchange so can you please let me know how to make it work for onload.

It would be helpful if you provide the script for that