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.

Autopopulate the Manager field in the catalog item.

Servicenow de11
Tera Contributor

Hello,

 

I need to automatically populate the Approving Manager variable in a ServiceNow catalog item based on the following logic:

  • If the Is this request for you? variable is Yes, then the manager of the Requested by user should be auto-populated in the Approving Manager field.
  • If the Is this request for you? variable is No, then the manager of the user specified in the On Behalf of variable should be auto-populated in the Approving Manager field.

Could someone guide me on how to implement this?

 

Thanks in advance

 

4 REPLIES 4

Servicenow de11
Tera Contributor
 

Ankur Bawiskar
Tera Patron
Tera Patron

@Servicenow de11 

something like this in onchange catalog client script

Ensure you give correct variable names

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

    if (newValue == '') {
        g_form.clearValue('approving_manager'); // approving manager variable name here
    }
    var variableToPick = '';
	
    if (newValue == 'yes')
        variableToPick = 'requested_by'; // requested by variable name
    else if (newValue == 'no')
        variableToPick = 'on_behalf_of'; // on behalf of variable name

    var ref = g_form.getReference(variableToPick, callBackMethod);
}

function callBackMethod(ref) {
    if (ref.manager)
        g_form.setValue('approving_manager', ref.manager); // approving manager variable name here
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello Ankur,

 

I have tried with the above script but it is not auto populating.
All those variable are existing in the variable set. I wrote the above client script in the variable set itself

 

Thanks

Deepak Shaerma
Kilo Sage
Kilo Sage

hi @Servicenow de11 

Create the Script Include:

  • Name: UserManagerAjax

  • Client callable: Check this box (make it true)

    var UserManagerAjax = Class.create();
    UserManagerAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
        getManagerInfo: function() {
            var userId = this.getParameter('sysparm_user_id');
            var grUser = new GlideRecord('sys_user');
    
            // Check if the user ID is valid, the user exists, and has a manager
            if (userId && grUser.get(userId) && grUser.manager) {
                
                var managerId = grUser.getValue('manager');
                var managerName = grUser.getDisplayValue('manager');
    
                // Return the manager's ID and Name as a JSON object
                var result = {};
                result.id = managerId;
                result.name = managerName;
                return new JSON().encode(result);
            }
            
            // If no user or no manager, return null
            return null;
        },
    
        type: 'UserManagerAjax'
    });



 

Catalog Client Script: OnChange (for "Is this request for you?")

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }
    setManager();
}

function setManager() {
    var isForYou = g_form.getValue('is_for_you'); // <--- CHECK YOUR VARIABLE NAME
    var targetUserID;

    // 2. Decide which user field to check
    if (isForYou == 'Yes') {
        targetUserID = g_form.getValue('requested_for'); // <--- CHECK YOUR VARIABLE NAME
    } else {
        targetUserID = g_form.getValue('on_behalf_of'); // <--- CHECK YOUR VARIABLE NAME
    }

    // 3. If the target user field is empty, clear the manager and stop
    if (!targetUserID) {
        g_form.clearValue('approving_manager'); // <--- CHECK YOUR VARIABLE NAME
        return;
    }

    // 4. Call our Script Include to get the manager
    var ga = new GlideAjax('UserManagerAjax');
    ga.addParam('sysparm_name', 'getManagerInfo');
    ga.addParam('sysparm_user_id', targetUserID);
    ga.getXML(handleResponse);
}
}

function handleResponse(response) {
    var answer = response.responseXML.documentElement.getAttribute('answer');
    
    if (answer) {
        try {
            // Parse the JSON string from the server
            var manager = JSON.parse(answer);
            // Set the 'approving_manager' field
            g_form.setValue('approving_manager', manager.id, manager.name); // <--- CHECK YOUR VARIABLE NAME
        } catch (e) {
            // Error parsing, clear the field
            g_form.clearValue('approving_manager'); // <--- CHECK YOUR VARIABLE NAME
        }
    } else {
        // No manager was found, clear the field
        g_form.clearValue('approving_manager'); // <--- CHECK YOUR VARIABLE NAME
    }
}


Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma