Fetch Manager name in single line text variable with reference to user[sys_user]

Mohankumari
Tera Contributor

Hi all, 

I want to fetch Manager Name using user record, if its not found, then fetch Assignment contact name of the user.

 

I have created a variable reference type -requestedFor which would fetch user name in catalog item in service portal.

I have also created 1 more variable by name Manager- single line text, where I need to fetch manager name, if the user does not have manager name or empty , if should fetch assignment contact name.

 

I tried auto populate method on variable , which would only fetch Manager name through dot-walking method. 

I tried checking dynamic default and added a script under default value , but here I would get the assignment contact name if manager name is not present. blocker is , when the username is changed in reference field, it would still show the previous user record details.

 

Since I cant user script Include, Business rule, I would need help here to fetch the uuser record details.

 

script used in default value:

javascript:
var answer = '';
var userId = gs.getUserID();
var userGR = new GlideRecord('sys_user');
if (userGR.get(userId)) {
var managerName = userGR.getDisplayValue('manager');
if (managerName && managerName !== '') {
answer = managerName;
} else {
answer = userGR.getDisplayValue('u_assignment_contact');
}
}
answer;

 

 

2 REPLIES 2

Ankur Bawiskar
Tera Patron

 

for this auto populate won't work

You need to have onChange client script + GlideAjax

something like this

Script Include: It should be client callable

var GetUserManagerOrAssignmentContact = Class.create();
GetUserManagerOrAssignmentContact.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getManagerOrAssignmentContact: function() {
        var userId = this.getParameter('sysparm_user_id');
        if (!userId)
            return '';

        var userGR = new GlideRecord('sys_user');
        if (!userGR.get(userId))
            return '';

        var managerName = userGR.getDisplayValue('manager');
        if (managerName)
            return managerName;

        return userGR.getDisplayValue('u_assignment_contact') || '';
    },

    type: 'GetUserManagerOrAssignmentContact'
});

Client script:

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

    if (newValue == '') {
        g_form.setValue('manager', '');
        return;
    }

    var ga = new GlideAjax('GetUserManagerOrAssignmentContact');
    ga.addParam('sysparm_name', 'getManagerOrAssignmentContact');
    ga.addParam('sysparm_user_id', newValue);
    ga.getXMLAnswer(function(answer) {
        g_form.setValue('manager', answer || '');
    });
}

💡 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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Tanushree Maiti
Mega Patron

Hi @Mohankumari 

 

Step 1: Create a script Include

  • Name: RefQualItils

Sample code/Not tested

var RefQualUtils = Class.create();
RefQualUtils.prototype = {
    initialize: function() {},

    festchManager: function(userSysID) {
        var userGR = new GlideRecord('sys_user');
        if (userGR.get(userSysID)) {
if (userGR.manager && userGR.manager != '') {
return 'sys_id=' + userGR.getValue('manager'); // Return manager
}
else { return 'sys_id=' + userGR.getValue('u_assignment_contact'); } } return ''; }, type: 'RefQualUtils'};

 

Step2: Apply the script to your field's Reference Qualifier

           

  1. Right-click the field label on the form.
  2. Go to the Type Specifications tab.
  3. Set Use reference qualifier to Advanced.
  4. In the Reference qualifier field, enter: 

javascript:new RefQualUtils().fetchManager(current.variables.user_variable_name);

 

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: