i have a catalog variable (not catalog field) of reference type 'Qualtiy approver'

LVPhanee
Tera Contributor

My requestion is when the catalog varaiable name is changed only Notiifcation should be triggered, right now if any change happens on state or priority field of sc_task

I have sc_take (where Quality analyst variable ), Business rule on the sc_task table screen shot and current logic with below condition 

LVPhanee_2-1781085386077.png

 

 and 

LVPhanee_1-1781085158503.png

 



and code is writtne like this 

''

(function executeRule(current, previous) {

    var ritm_QA = new GlideRecord('sc_req_item');

    if (ritm_QA.get(current.request_item)) {

        var groupMemberId = ritm_QA.variables.quality_approver;

        if (groupMemberId) {

            var memberGR = new GlideRecord('sys_user_grmember');

            if (memberGR.get(groupMemberId)) {

                var userSysId = memberGR.user;

               // gs.log(" BR TRIGGERED USER SYS_ID: " + userSysId);

                gs.eventQueue(
                    'rd.quality_approver.assigned',
                    current,
                    userSysId.toString(),
                    ''
                );
            }
        }
    }

})(current, previous);''

Can you help by added the required logic so that if only Quality Approver ( catalog variable ) is changed then only email should be triggered. 

7 REPLIES 7

pr8172510
Tera Guru

Hi @LVPhanee,

Since quality_approver is a catalog variable on the RITM and not a field on sc_task, your current Business Rule will execute whenever the sc_task record is updated (state, priority, assignment group, etc.).

To trigger the notification only when the Quality Approver variable changes, compare the current and previous values of the variable:

(function executeRule(current, previous) {

    var ritm = new GlideRecord('sc_req_item');
    if (!ritm.get(current.request_item))
        return;

    var currentQA = ritm.variables.quality_approver.toString();

    var prevRitm = new GlideRecord('sc_req_item');
    if (!prevRitm.get(current.request_item))
        return;

    var previousQA = previous.request_item.variables.quality_approver.toString();

    if (currentQA == previousQA)
        return; // No change, do not trigger notification

    var memberGR = new GlideRecord('sys_user_grmember');
    if (memberGR.get(currentQA)) {

        gs.eventQueue(
            'rd.quality_approver.assigned',
            current,
            memberGR.user.toString(),
            ''
        );
    }

})(current, previous);


A better approach would be to move this logic to a
Business Rule on sc_req_item (or on the variable update process) and use:

current.variables.quality_approver.changes()

because the variable belongs to the RITM, not the SCTASK.

This ensures the event is queued only when the Quality Approver value changes and not when State, Priority, or other SCTASK fields are updated.

 

   /////////// var prevRitm = new GlideRecord('sc_req_item');

//////////////    if (!prevRitm.get(current.request_item))

/////////////        return;  ( your suggested code as prev is also taking current i changed it to Previuos in the above 2nd line 

Here Ritm and prevRitm both are same right.  so i changed the second one to 

  if (!prevRitm.get(previous.request_item))



But as you suggested i executed it got be same sys_id 


and log screen shot is 

LVPhanee_2-1781089166723.png

 

 

Can you please share an alternative thought to make it workable.

Ankur Bawiskar
Tera Patron

@LVPhanee 

something like this

(function executeRule(current, previous /*null when async*/ ) {

    // Replace 'your_variable_name' with the actual internal name of your variable
    var currentVar = current.variables.your_variable_name;
    var previousVar = previous.variables.your_variable_name;

    // Check if the variable has actually changed
    if (currentVar != previousVar) {
        // your logic here
        var ritm_QA = new GlideRecord('sc_req_item');

        if (ritm_QA.get(current.request_item)) {

            var groupMemberId = ritm_QA.variables.quality_approver;

            if (groupMemberId) {

                var memberGR = new GlideRecord('sys_user_grmember');

                if (memberGR.get(groupMemberId)) {

                    var userSysId = memberGR.user;

                    // gs.log(" BR TRIGGERED USER SYS_ID: " + userSysId);

                    gs.eventQueue(
                        'rd.quality_approver.assigned',
                        current,
                        userSysId.toString(),
                        ''
                    );
                }
            }
        }
    }

})(current, previous);

💡 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

I tried this 
for this line of code when i kept gs.logs i got the same sys_id ( quality approver name i changed in catalog task, but i got only the last name  sysid only ( never got the old name sysid)

// Replace 'your_variable_name' with the actual internal name of your variable
var currentVar = current.variables.your_variable_name;
var previousVar = previous.variables.your_variable_name;
Can you suggest any other alternative.