When the assignment group on the incident form is changed, display the assignment group's manager

SNow Learner41
Tera Contributor

Hi All,

My Requirement is When the assignment group on the incident form is changed, display the assignment group's manager name on form. (Hint - Use Client script and script include) so i written everything but unfortunately it is showing an error in testing time i am unable to sort out that problem can any one please help me to resolve the issue.

Please follow the below screenshots and code 

Script Include:

var AssignmentGroupManagerUtils = Class.create();
AssignmentGroupManagerUtils.prototype = {
    initialize: function() {},
    // Function to get the manager name of an assignment group
    getManagerName: function(assignmentGroupId) {
        var managerName = '';
        var group = new GlideRecord('sys_user_group');
        if (group.get(assignmentGroupId)) {
            managerName = group.manager.name;
        }
        return managerName;
    },
    type: 'AssignmentGroupManagerUtilss'
};
Client Script Onchange:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var assignmentGroupId = g_form.getValue('assignment_group');
    if (assignmentGroupId) {
        var assignmentGroupManagerUtils = new AssignmentGroupManagerUtils();
        var managerName = assignmentGroupManagerUtils.getManagerName(assignmentGroupId);

        if (managerName) {
            g_form.setValue('u_manager_name', managerName); // Set the manager name field on the form
        } else {
            g_form.setValue('u_manager_name', ''); // Clear the manager name field if manager not found
        }
    }
}
The error is :
error.png
Please help me to resolve this issue
Thank You,
Kiran Kumar.
2 ACCEPTED SOLUTIONS

Hi @SNow Learner41,

 

I'm struggling to understand why you want to do this via an onChange Client Script and implementing a new field that already exists. I'd strongly recommend you use the OOB functionality already present and related field in my earlier post.

 

However, to provide you with the code and help with your understanding of calling Server-side code from the client, see the below changes and note the use of GlideAjax. 

Please note - I've assumed your customer field 'u_manager_name' is a reference field. If not, comment and uncomment accordingly on the Script include provided.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.

 

Thanks, Robbie

 

Client Script:

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    //alert('running Custom manager'); //USed for testing to confirm the Script is being called.
    var assignmentGroupId = g_form.getValue('assignment_group');
    if (assignmentGroupId) {

        var assignmentGroupManagerUtils = new GlideAjax('AssignmentGroupManagerUtils');
        assignmentGroupManagerUtils.addParam('sysparm_name', 'getManagerName')
        assignmentGroupManagerUtils.addParam('sysparm_assignmentGroup', assignmentGroupId);
        assignmentGroupManagerUtils.getXMLAnswer(updateCustomManager);
    }
}

    function updateCustomManager(response) {
        //alert(response); //Used for testing. Uncommon to check response value if required

        if (response) {
            //var returneddata = answer.evalJSON(true);
            g_form.setValue("u_manager_name", response); // Set the manager name field on the form using sys_id assuming the u_manager_name field is a reference field
        } else {
            g_form.setValue("u_manager_name", ''); // Clear the manager name field if manager not found
        }
    }

 

 

 

Script Include: (Make sure the Script Include 'Client Callable' field is checked / set to true)

 

 

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

    // Function to get the manager name of an assignment group
    getManagerName: function() {
        var assignmentGroupId = this.getParameter('sysparm_assignmentGroup');
        var managerName = '';
        var group = new GlideRecord('sys_user_group');
        if (group.get(assignmentGroupId)) {
            managerName = group.manager; //retruning the sys_id assuming the custom u_manager_name field is a reference field. 
			Change accordingly
			//managerName = group.manager.name.toString(); retrurning the string value if the custom u_manager_name field is a string
        }
        return managerName;
    },
    //type: 'AssignmentGroupManagerUtilss'
    //};

    type: 'AssignmentGroupManagerUtils'
});

 

 

 

View solution in original post

Robbie
Kilo Patron
Kilo Patron

Hi @SNow Learner41,

 

I'm struggling to understand why you want to do this via an onChange Client Script and implementing a new field that already exists. I'd strongly recommend you use the OOB functionality already present and related field as shown and discussed in my earlier post.

 

However, to provide you with the code and help with your understanding of calling Server-side code from the client, see the below changes and note the use of GlideAjax. 

Please note - I've assumed your customer field 'u_manager_name' is a reference field. If not, comment and uncomment accordingly on the Script include provided.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.

 

Thanks, Robbie

 

Client Script:

 

 

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    //alert('running Custom manager'); //USed for testing to confirm the Script is being called.
    var assignmentGroupId = g_form.getValue('assignment_group');
    if (assignmentGroupId) {

        var assignmentGroupManagerUtils = new GlideAjax('AssignmentGroupManagerUtils');
        assignmentGroupManagerUtils.addParam('sysparm_name', 'getManagerName')
        assignmentGroupManagerUtils.addParam('sysparm_assignmentGroup', assignmentGroupId);
        assignmentGroupManagerUtils.getXMLAnswer(updateCustomManager);
    }
}

    function updateCustomManager(response) {
        //alert(response); //Used for testing. Uncommon to check response value if required

        if (response) {
            //var returneddata = answer.evalJSON(true);
            g_form.setValue("u_manager_name", response); // Set the manager name field on the form using sys_id assuming the u_manager_name field is a reference field
        } else {
            g_form.setValue("u_manager_name", ''); // Clear the manager name field if manager not found
        }
    }

 

 

 

 

 

Script Include: (Make sure the Script Include 'Client Callable' field is checked / set to true)

 

 

 

 

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

    // Function to get the manager name of an assignment group
    getManagerName: function() {
        var assignmentGroupId = this.getParameter('sysparm_assignmentGroup');
        var managerName = '';
        var group = new GlideRecord('sys_user_group');
        if (group.get(assignmentGroupId)) {
            managerName = group.manager; //retruning the sys_id assuming the custom u_manager_name field is a reference field. 
			Change accordingly
			//managerName = group.manager.name.toString(); retrurning the string value if the custom u_manager_name field is a string
        }
        return managerName;
    },
    //type: 'AssignmentGroupManagerUtilss'
    //};

    type: 'AssignmentGroupManagerUtils'
});

 

 

 

View solution in original post

7 REPLIES 7

Hi dgarad,

Thank you for response and information, i tried in that way also but same error i am facing.

Robbie
Kilo Patron
Kilo Patron

Hi @SNow Learner41,

 

I'm struggling to understand why you want to do this via an onChange Client Script and implementing a new field that already exists. I'd strongly recommend you use the OOB functionality already present and related field as shown and discussed in my earlier post.

 

However, to provide you with the code and help with your understanding of calling Server-side code from the client, see the below changes and note the use of GlideAjax. 

Please note - I've assumed your customer field 'u_manager_name' is a reference field. If not, comment and uncomment accordingly on the Script include provided.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.

 

Thanks, Robbie

 

Client Script:

 

 

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    //alert('running Custom manager'); //USed for testing to confirm the Script is being called.
    var assignmentGroupId = g_form.getValue('assignment_group');
    if (assignmentGroupId) {

        var assignmentGroupManagerUtils = new GlideAjax('AssignmentGroupManagerUtils');
        assignmentGroupManagerUtils.addParam('sysparm_name', 'getManagerName')
        assignmentGroupManagerUtils.addParam('sysparm_assignmentGroup', assignmentGroupId);
        assignmentGroupManagerUtils.getXMLAnswer(updateCustomManager);
    }
}

    function updateCustomManager(response) {
        //alert(response); //Used for testing. Uncommon to check response value if required

        if (response) {
            //var returneddata = answer.evalJSON(true);
            g_form.setValue("u_manager_name", response); // Set the manager name field on the form using sys_id assuming the u_manager_name field is a reference field
        } else {
            g_form.setValue("u_manager_name", ''); // Clear the manager name field if manager not found
        }
    }

 

 

 

 

 

Script Include: (Make sure the Script Include 'Client Callable' field is checked / set to true)

 

 

 

 

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

    // Function to get the manager name of an assignment group
    getManagerName: function() {
        var assignmentGroupId = this.getParameter('sysparm_assignmentGroup');
        var managerName = '';
        var group = new GlideRecord('sys_user_group');
        if (group.get(assignmentGroupId)) {
            managerName = group.manager; //retruning the sys_id assuming the custom u_manager_name field is a reference field. 
			Change accordingly
			//managerName = group.manager.name.toString(); retrurning the string value if the custom u_manager_name field is a string
        }
        return managerName;
    },
    //type: 'AssignmentGroupManagerUtilss'
    //};

    type: 'AssignmentGroupManagerUtils'
});

 

 

 

Hi Robbie,

it is working fine thank you very much for your response and for your help.