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 

change your 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.getUniqueValue();
            details.push(users);
        }
        return JSON.stringify(details);
 },
    type: 'Autopopulatemanager'
});
If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

suuriya
Tera Contributor

HI @dgarad ,

 

Thanks for the reply

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

suuriya_0-1715262281445.png

 

try the below code.

 

var Autopopulatemanager = Class.create();
Autopopulatemanager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDetails: function() {
        var details = [];
var manager= '';
        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()) {
            
            manager= userGR.getUniqueValue();
           
        }
        return manager;
 },
    type: 'Autopopulatemanager'
});

 

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

Thanks
dgarad

suuriya
Tera Contributor

HI @dgarad ,

 

Can you help me with how we can achieve this when the form loads.....when user opens the item in portal then user name will be displayed in common_vars_requested_for field now this user manager needs to be populate in common_vars_also_notify.

 

As it is needs to be displayed only for one particular cat item can you let me know how we can achieve this using onload script

swathisarang98
Giga Sage
Giga Sage

Hi @suuriya ,

 

You can do this through Auto populate feature available in variables in catalog item no need to do scripting, 

select Dependent Question as Requested For and Dot walk Path as Manager,

swathisarang98_0-1715339814927.png

 

Result:

User Table,

swathisarang98_1-1715339814848.png

 

Portal View:

swathisarang98_2-1715339814832.png

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang