How to populate the assigned_to field with the opened_by user's manager that is defined in sys_user

ChuanYanF
Tera Guru

Dear experts,

 

I am facing a problem where my onLoad script is not working as expected. I want to make the assigned_to field in the risk event form to autopopulate the user's manager for whichever users that creates a new risk event record. I have defined each user's manager in the sys_user record and I have created a script include called GetManager and a onLoad client script. Please advise what approach or amendments I need to be done to my scripts or my actions.

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

    getManager: function() {
        var userID = this.getParameter('sysparm_user_id');
        var userGR = new GlideRecord('sys_user');
        if (userGR.get(userID)) {
            if (userGR.manager) {
                return userGR.manager.toString();
            }
        }
        return '';
    }

});
function onLoad() {
    var openedBy = g_form.getValue('opened_by');

    if (openedBy) {
        // Use GlideAjax to call a Script Include that returns the manager
        var ga = new GlideAjax('GetManager');
        ga.addParam('sysparm_name', 'getManager');
        ga.addParam('sysparm_user_id', openedBy);
        ga.getXMLAnswer(function(response) {
            if (response) {
                g_form.setValue('assigned_to', response);
            }
        });
    }
}
2 ACCEPTED SOLUTIONS

And use below 👇 script 

 

function onLoad() {

 

 if (g_form.isNewRecord()) {

 

    var openedBy = g_form.getValue('opened_by');

 

    if (openedBy) {

 

      g_form.getReference('opened_by', function(user) {

 

        if (user.manager) {

 

          g_form.setValue('assigned_to', user.manager);

 

        }

 

      });

 

    }

 

  }

 

}

 

This will work if fields are correct and also try by removing "is New Record" 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQC

View solution in original post

Shivalika
Mega Sage

Hello @ChuanYanF 

 With my recent solution even without bringing "opened by on the form" its working, I have done it for incident and see the reference screenshots and code - 

 

Shivalika_0-1744173559174.png

 

 

Shivalika_1-1744173585544.png

 

 

Shivalika_2-1744173596229.png

 

 

//CLIENT SCRIPT
function onLoad() {
   //Type appropriate comment here, and begin script below
   
    var ga = new GlideAjax('GetOpenedByManager');
    ga.addParam('sysparm_name', 'getManager');
    ga.addParam('sysparm_record_number', g_form.getValue('number')); // Replace with actual record number
	alert(g_form.getValue('number'))
    ga.getXMLAnswer(function(response) {
        alert("working");
        alert("Opened By's Manager: " + response);
		g_form.setValue('assigned_to',response);
    });

}

 

//SCRIPT INCLUDE

// Name: GetOpenedByManager
// Accessible from: Client Callable - true
var GetOpenedByManager = Class.create();
GetOpenedByManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getManager: function () {
        
        var recordNumber = this.getParameter('sysparm_record_number');
        
        var gr = new GlideRecord('incident');
        gr.addQuery('number', recordNumber);
        gr.query();

        if (gr.next()) {
            var openedBy = gr.getValue('caller_id');
            if(openedBy) {
                var userGR = new GlideRecord('sys_user');
                if (userGR.get(openedBy)) {
                    var manager = userGR.getValue('manager');
                    return manager;
                }
            }
            
        }
        
    }
});

 

This answers your second question as well @ChuanYanF 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

View solution in original post

23 REPLIES 23

I see what is the problem here right now, I need to put the opened_by field out to my form only it will populate the assigned_to field, is there a way where I dont have to put the opened_by field into the form but the client script will still runs?

No @ChuanYanF  You don't need to put openedBy into the form. 

 

See how its populating in alert - 

 

Shivalika_0-1744169452788.png

 

 

Shivalika_1-1744169461463.png

 

 

Shivalika_2-1744169469018.png

 

Do one thing @ChuanYanF   Just use below script and nothing else, you will get your answer that assignmetn group is the issue - 

 

function onLoad() {
   //Type appropriate comment here, and begin script below

   var caller = g_form.getValue('opened_by');

   alert(g_form.getReference('opened_by').manager);
   
}

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

Shivalika
Mega Sage

Hello @ChuanYanF 

 

Just use below in client script -

 

 var ga = new GlideAjax('GetOpenedByManager');
    ga.addParam('sysparm_name', 'getManager');
    ga.addParam('sysparm_record_number', g_form.getValue('number')); // Replace with actual record number
	 ga.getXMLAnswer(function(response) {
        
       g_form.setValue('assigned_to',response);
    });

 

Use below in CLIENT CALLABEL Script include -

 

getManager: function () {
        
        var recordNumber = this.getParameter('sysparm_record_number');
        
        var gr = new GlideRecord('incident');
        gr.addQuery('number', recordNumber);
        gr.query();

        if (gr.next()) {
            var openedBy = gr.getValue('opened_by');
            if (openedBy) {
                var userGR = new GlideRecord('sys_user');
                if (userGR.get(openedBy)) {
                    var manager = userGR.getValue('manager');
                    return manager;
                }
            }
            
        }
        
    }

 

 

Must change the table name to what your record table name and all the paranthese and everything in the script inclyde that also should be correct. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

Shivalika
Mega Sage

Hello @ChuanYanF 

 With my recent solution even without bringing "opened by on the form" its working, I have done it for incident and see the reference screenshots and code - 

 

Shivalika_0-1744173559174.png

 

 

Shivalika_1-1744173585544.png

 

 

Shivalika_2-1744173596229.png

 

 

//CLIENT SCRIPT
function onLoad() {
   //Type appropriate comment here, and begin script below
   
    var ga = new GlideAjax('GetOpenedByManager');
    ga.addParam('sysparm_name', 'getManager');
    ga.addParam('sysparm_record_number', g_form.getValue('number')); // Replace with actual record number
	alert(g_form.getValue('number'))
    ga.getXMLAnswer(function(response) {
        alert("working");
        alert("Opened By's Manager: " + response);
		g_form.setValue('assigned_to',response);
    });

}

 

//SCRIPT INCLUDE

// Name: GetOpenedByManager
// Accessible from: Client Callable - true
var GetOpenedByManager = Class.create();
GetOpenedByManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getManager: function () {
        
        var recordNumber = this.getParameter('sysparm_record_number');
        
        var gr = new GlideRecord('incident');
        gr.addQuery('number', recordNumber);
        gr.query();

        if (gr.next()) {
            var openedBy = gr.getValue('caller_id');
            if(openedBy) {
                var userGR = new GlideRecord('sys_user');
                if (userGR.get(openedBy)) {
                    var manager = userGR.getValue('manager');
                    return manager;
                }
            }
            
        }
        
    }
});

 

This answers your second question as well @ChuanYanF 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY