How to match two Variables and based on that approval get created in RITM

Ajay Singh2
Tera Contributor

Hello All,

 

There is a requirement like in a catalog form there is two field with Reference type and one field is Variable field with name u_frpa_fnrole_owner and second field is Variable set internal name is u_functional_role_access inside this variable which is Reference field name is u_data_owner_field and both are reference type field with sys_user table and if i select same user in both field than an additional approval is not required only one approval required but if i select both diffrenet user than 2 approvals is required. For example, if Aman is the ABC Owner, provides her approval and then is also one of the Data Owner approvers listed, a 2nd approval is not required to generate for Aman.

How to achieve this in workflow if condition see below code i am getting only yes if user is different in data_owner_field.

answer = ifScript();

function ifScript() {
    gs.log('ifScript started');

    var abcOwner = current.variables.u_frpa_fnrole_owner ? current.variables.u_frpa_fnrole_owner.toString().trim() : '';
    gs.log('abcOwner (u_frpa_fnrole_owner) sys_id: "' + abcOwner + '"');

    var mrvsUsers = [];
    var mrvsGR = new GlideRecord('sc_multi_row_question_answer');
    mrvsGR.addQuery('parent_id', current.sys_id);
    mrvsGR.addQuery('variable_set', 'u_functional_role_access');
    mrvsGR.query();

    gs.log('MRVS record count: ' + mrvsGR.getRowCount());

    while (mrvsGR.next()) {
        // Try using 'sc_item_option' instead of 'u_data_owner_field'
        var user = mrvsGR.getValue('sc_item_option');
        if (user) {
            user = user.toString().trim();
            if (mrvsUsers.indexOf(user) === -1) {
                mrvsUsers.push(user);
            }
            gs.log('Found MRVS user: "' + user + '"');
        }
    }

    gs.log('MRVS data owners found: ' + mrvsUsers.map(function(u){ return '"' + u + '"'; }).join(', '));

    if (!abcOwner) {
        gs.log('Functional Role Owner (abcOwner) missing. Returning no');
        return 'no';
    }

    var allUsers = mrvsUsers.slice();
    if (allUsers.indexOf(abcOwner) === -1) {
        allUsers.push(abcOwner);
    }

    var uniqueUsers = [];
    allUsers.forEach(function(user) {
        if (user && uniqueUsers.indexOf(user) === -1) {
            uniqueUsers.push(user);
        }
    });

    gs.log('Unique users: ' + uniqueUsers.map(function(u){ return '"' + u + '"'; }).join(', '));
    gs.log('Unique users count: ' + uniqueUsers.length);

    if (uniqueUsers.length === 1) {
        gs.log('Single unique user - one approval needed. Returning yes');
        return 'yes';
    } else {
        gs.log('Multiple unique users - multiple approvals needed. Returning no');
        return 'no';
    }
}
 
Thank You
4 REPLIES 4

Shashank_Jain
Kilo Sage

 

answer = ifScript();

function ifScript() {
    gs.log('ifScript started');

    var abcOwner = current.variables.u_frpa_fnrole_owner ? current.variables.u_frpa_fnrole_owner.toString().trim() : '';
    gs.log('abcOwner (u_frpa_fnrole_owner) sys_id: "' + abcOwner + '"');

    var mrvsUsers = [];
    var mrvsGR = new GlideRecord('sc_multi_row_question_answer');
    mrvsGR.addQuery('parent_id', current.sys_id);
    mrvsGR.addQuery('variable_set', 'u_functional_role_access');
    mrvsGR.query();

    gs.log('MRVS record count: ' + mrvsGR.getRowCount());

    while (mrvsGR.next()) {
        var rowSysId = mrvsGR.getValue('sys_id');
        gs.log('Processing MRVS row sys_id: ' + rowSysId);

        // Now query the variable u_data_owner_field inside this MRVS row
        var varGR = new GlideRecord('sc_multi_row_question_answer');
        varGR.addQuery('parent_id', rowSysId);
        varGR.addQuery('variable', 'u_data_owner_field');
        varGR.query();

        if (varGR.next()) {
            var user = varGR.getValue('value');
            if (user) {
                user = user.toString().trim();
                if (mrvsUsers.indexOf(user) === -1) {
                    mrvsUsers.push(user);
                }
                gs.log('Found MRVS user (u_data_owner_field): "' + user + '"');
            }
        } else {
            gs.log('No u_data_owner_field variable found in MRVS row ' + rowSysId);
        }
    }

    gs.log('MRVS data owners found: ' + mrvsUsers.map(function(u){ return '"' + u + '"'; }).join(', '));

    if (!abcOwner) {
        gs.log('Functional Role Owner (abcOwner) missing. Returning no');
        return 'no';
    }

    var allUsers = mrvsUsers.slice();
    if (allUsers.indexOf(abcOwner) === -1) {
        allUsers.push(abcOwner);
    }

    var uniqueUsers = [];
    allUsers.forEach(function(user) {
        if (user && uniqueUsers.indexOf(user) === -1) {
            uniqueUsers.push(user);
        }
    });

    gs.log('Unique users: ' + uniqueUsers.map(function(u){ return '"' + u + '"'; }).join(', '));
    gs.log('Unique users count: ' + uniqueUsers.length);

    if (uniqueUsers.length === 1) {
        gs.log('Single unique user - one approval needed. Returning yes');
        return 'yes';
    } else {
        gs.log('Multiple unique users - multiple approvals needed. Returning no');
        return 'no';
    }
}

@Ajay Singh2 , try this script with logs.

 

If this works, please mark it as helpful/accepted — it keeps me motivated and helps others find solutions.
Shashank Jain – Software Engineer | Turning issues into insights

@Shashank_Jain  getting below logs and while selecting different user in both field its going to yes.

 

 

@Ajay Singh2 , Try this

 

answer = ifScript();

function ifScript() {
    gs.log('--- ifScript started ---');

    // Get the Functional Role Owner
    var abcOwner = current.variables.u_frpa_fnrole_owner ? current.variables.u_frpa_fnrole_owner.toString().trim() : '';
    gs.log('Functional Role Owner (u_frpa_fnrole_owner): ' + abcOwner);

    // Get MRVS data as JSON
    var mrvsData = current.variables.u_functional_role_access;
    var mrvsUsers = [];

    if (mrvsData) {
        try {
            var rows = JSON.parse(mrvsData);
            gs.log('Total MRVS Rows: ' + rows.length);

            for (var i = 0; i < rows.length; i++) {
                var row = rows[i];
                if (row.u_data_owner_field) {
                    var dataOwner = row.u_data_owner_field.toString().trim();
                    if (dataOwner && mrvsUsers.indexOf(dataOwner) === -1) {
                        mrvsUsers.push(dataOwner);
                        gs.log('Found MRVS Data Owner: ' + dataOwner);
                    }
                }
            }
        } catch (e) {
            gs.logError('Failed to parse MRVS JSON: ' + e.message);
            return 'no'; // Fail-safe
        }
    } else {
        gs.log('No MRVS data found in u_functional_role_access');
    }

    if (!abcOwner) {
        gs.log('Functional Role Owner not provided. Returning "no"');
        return 'no';
    }

    // Combine abcOwner and MRVS users, check uniqueness
    var allUsers = mrvsUsers.slice(); // clone
    if (allUsers.indexOf(abcOwner) === -1) {
        allUsers.push(abcOwner);
    }

    var uniqueUsers = [];
    allUsers.forEach(function(user) {
        if (user && uniqueUsers.indexOf(user) === -1) {
            uniqueUsers.push(user);
        }
    });

    gs.log('Unique Users: ' + uniqueUsers.join(', '));
    gs.log('Unique Users Count: ' + uniqueUsers.length);

    if (uniqueUsers.length === 1) {
        gs.log('Only one unique user - one approval required. Returning "yes"');
        return 'yes';
    } else {
        gs.log('Multiple users involved - multiple approvals required. Returning "no"');
If this works, please mark it as helpful/accepted — it keeps me motivated and helps others find solutions.
Shashank Jain – Software Engineer | Turning issues into insights

@Shashank_Jain not getting approval for both different user selected alsways yes condition get triggered. See below logs.