How to auto populate the value of manager first name in a user defined field on incident table.

harshchaudh
Tera Contributor
g_form.setValue('u_mg',a.manager.first_name);

I write this line but it didn't fetch anything please suggest me correct way
3 REPLIES 3

Abbas_5
Tera Sage
Tera Sage

Hello @harshchaudh,

 

How are you planning to do the BR, client script?

 

Thanks,

Abbas

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@harshchaudh 

sorry your requirement is not clear.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Roshnee Dash
Tera Guru

HI @harshchaudh 
Can you please try like this

If you want to auto-populate a custom field (e.g., u_mg) with the first name of the manager of a user (like caller_id), you must use the field name, not the label.

Here’s how to do it using a Client Script (onChange of caller_id):You can write for onload also same to use it for on open of exiting record.

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

    var ga = new GlideAjax('GetManagerFirstName');
    ga.addParam('sysparm_name', 'getManagerFirstName');
    ga.addParam('sysparm_user_id', newValue); // caller_id sys_id
    ga.getXMLAnswer(function(response) {
        g_form.setValue('u_mg', response); // u_mg is your custom field
    });
}

And the Script Include (server-side): Script include need to be client callable.

 
var GetManagerFirstName = Class.create();
GetManagerFirstName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getManagerFirstName: function() {
        var userId = this.getParameter('sysparm_user_id');
        var userGR = new GlideRecord('sys_user');
        if (userGR.get(userId)) {
            var managerGR = new GlideRecord('sys_user');
            if (managerGR.get(userGR.getValue('manager'))) {
                return managerGR.getValue('first_name');
            }
        }
        return '';
    }
});
Your feedback makes the community stronger! If you found this helpful, marking it as the correct answer helps others.
Stay awesome,
Roshnee Dash