Make Comment and Additional comment mandatory when RITM is moved into different state

BKSharma
Giga Contributor

Hi,

 

I've below 2 requirements - 

1. When a RITM is moved by an 'agent' from 'Open' to 'Work In Progress', "Additional Comments" (instead of Work Notes) must be mandatory.
2. When a RITM is moved by an 'Assignment Group member (non-assignee)' from 'Open' to 'Work In Progress', both "Additional Comments" and "Work Notes" must be mandatory.

 

How I can achieve above 2 things. 

1 ACCEPTED SOLUTION

@BKSharma 

Something like this but please enhance

I assume you are checking some agent role in your 1st point.

Script Include: It should be client callable

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

    isAgent: function() {
        var userId = this.getParameter('sysparm_user_id');
        // Check if user has 'agent' role
        if (gs.getUser().hasRole('agent'))
            return 'true';
        return 'false';
    },

    isAssignmentGroupMemberNotAssignee: function() {
        var userId = this.getParameter('sysparm_user_id');
        var ritmSysId = this.getParameter('sysparm_ritm_sysid');

        var ritmGR = new GlideRecord('sc_req_item');
        if (!ritmGR.get(ritmSysId))
            return 'false';

        var assignmentGroup = ritmGR.assignment_group.toString();
        var assignee = ritmGR.assigned_to.toString();

        // Check if user is in assignment group but not assigned_to user
        var groupMemberGr = new GlideRecord('sys_user_grmember');
        groupMemberGr.addQuery('group', assignmentGroup);
        groupMemberGr.addQuery('user', userId);
        groupMemberGr.query();

        var isMember = groupMemberGr.hasNext();
        var notAssigned = (userId != assignee);

        if (isMember && notAssigned) {
            return 'true';
        }
        return 'false';
    },

});

onChange client script: State field

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue !== '2') // 2 = Work In Progress state, adjust as needed
        return;

    var ga = new GlideAjax('RITMAgentGroupCheck');
    ga.addParam('sysparm_name', 'isAgent');
    ga.addParam('sysparm_user_id', g_user.userID);

    ga.getXMLAnswer(function(agentResponse) {
        if (agentResponse === 'true') {
            // Agent moving RITM: Additional Comments mandatory only
            g_form.setMandatory('additional_comments', true);
            g_form.setMandatory('work_notes', false);
        } else {
            // Check assignment group member condition
            var ga2 = new GlideAjax('RITMAgentGroupCheck');
            ga2.addParam('sysparm_name', 'isAssignmentGroupMemberNotAssignee');
            ga2.addParam('sysparm_user_id', g_user.userID);
            ga2.addParam('sysparm_ritm_sysid', g_form.getUniqueValue());

            ga2.getXMLAnswer(function(memberResponse) {
                if (memberResponse === 'true') {
                    // Assignment group member (non-assignee): Both mandatory
                    g_form.setMandatory('additional_comments', true);
                    g_form.setMandatory('work_notes', true);
                } else {
                    // Otherwise, clear mandatory (optional)
                    g_form.setMandatory('additional_comments', false);
                    g_form.setMandatory('work_notes', false);
                }
            });
        }
    });
}

I believe I have answered your question and you can enhance it further based on your requirement and developer skills.

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

View solution in original post

9 REPLIES 9

@BKSharma 

you can check and pass the logged in user & form assignment group to GlideAjax function and then in that check if that user is part of that group or not

based on that derive your logic.

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

Hi @Ankur Bawiskar ,

Can you please provide me sample On Change Client Script and Glide Ajax for this functionality ?

@BKSharma 

Something like this but please enhance

I assume you are checking some agent role in your 1st point.

Script Include: It should be client callable

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

    isAgent: function() {
        var userId = this.getParameter('sysparm_user_id');
        // Check if user has 'agent' role
        if (gs.getUser().hasRole('agent'))
            return 'true';
        return 'false';
    },

    isAssignmentGroupMemberNotAssignee: function() {
        var userId = this.getParameter('sysparm_user_id');
        var ritmSysId = this.getParameter('sysparm_ritm_sysid');

        var ritmGR = new GlideRecord('sc_req_item');
        if (!ritmGR.get(ritmSysId))
            return 'false';

        var assignmentGroup = ritmGR.assignment_group.toString();
        var assignee = ritmGR.assigned_to.toString();

        // Check if user is in assignment group but not assigned_to user
        var groupMemberGr = new GlideRecord('sys_user_grmember');
        groupMemberGr.addQuery('group', assignmentGroup);
        groupMemberGr.addQuery('user', userId);
        groupMemberGr.query();

        var isMember = groupMemberGr.hasNext();
        var notAssigned = (userId != assignee);

        if (isMember && notAssigned) {
            return 'true';
        }
        return 'false';
    },

});

onChange client script: State field

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue !== '2') // 2 = Work In Progress state, adjust as needed
        return;

    var ga = new GlideAjax('RITMAgentGroupCheck');
    ga.addParam('sysparm_name', 'isAgent');
    ga.addParam('sysparm_user_id', g_user.userID);

    ga.getXMLAnswer(function(agentResponse) {
        if (agentResponse === 'true') {
            // Agent moving RITM: Additional Comments mandatory only
            g_form.setMandatory('additional_comments', true);
            g_form.setMandatory('work_notes', false);
        } else {
            // Check assignment group member condition
            var ga2 = new GlideAjax('RITMAgentGroupCheck');
            ga2.addParam('sysparm_name', 'isAssignmentGroupMemberNotAssignee');
            ga2.addParam('sysparm_user_id', g_user.userID);
            ga2.addParam('sysparm_ritm_sysid', g_form.getUniqueValue());

            ga2.getXMLAnswer(function(memberResponse) {
                if (memberResponse === 'true') {
                    // Assignment group member (non-assignee): Both mandatory
                    g_form.setMandatory('additional_comments', true);
                    g_form.setMandatory('work_notes', true);
                } else {
                    // Otherwise, clear mandatory (optional)
                    g_form.setMandatory('additional_comments', false);
                    g_form.setMandatory('work_notes', false);
                }
            });
        }
    });
}

I believe I have answered your question and you can enhance it further based on your requirement and developer skills.

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

@BKSharma 

Hope you are doing good.

Did my reply answer your question?

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