Script include does not work

Mira1992
Kilo Sage

Hello community,

I want to use a script include in my dynamic filter.
Dynamic filter looks like this:

Mira1992_0-1727184222655.png

My script include looks like this:

var getIAmDelegate = Class.create();
getIAmDelegate.prototype = {
    initialize: function() {
    },

    getIAmDelegate: function() {  // Keep the same method name
        // Get the current logged-in user's sys_id
        var currentUserSysID = gs.getUserID();

        // Create a GlideRecord object for the 'sys_granular_delegate' table
        var grDelegate = new GlideRecord('sys_granular_delegate');
        var now = new GlideDateTime();

        // Query 'sys_granular_delegate' where the current logged-in user is the delegate
        // and the delegation is valid (within the start and end dates)
        grDelegate.addEncodedQuery('delegate=' + currentUserSysID + '^starts<=' + now + '^ends>=' + now);
        grDelegate.query();

        // Initialize an array to store the matching user sys_id values
        var delegateArr = [];

        // Loop through the 'sys_granular_delegate' records
        while (grDelegate.next()) {
            var userSysID = grDelegate.getValue('user');  // Get the user sys_id (delegator)

            // Now, query the 'sys_m2m_delegate_rule' table to validate the delegation rule
            var grDelegateRule = new GlideRecord('sys_m2m_delegate_rule');
           
            // Add a query to check if the 'delegate' field in 'sys_m2m_delegate_rule' matches the current 'sys_granular_delegate' sys_id
            grDelegateRule.addEncodedQuery('delegate=' + grDelegate.getValue('sys_id'));  // Use the sys_id of 'sys_granular_delegate'
            grDelegateRule.query();

            // If a matching rule is found
            while (grDelegateRule.next()) {
                // Compare the delegation_rule from sys_m2m_delegate_rule directly with the expected GRC delegation rule sys_id
                if (grDelegateRule.getValue('delegation_rule') === '5a9a9d631b541210b635ddb69b4bcbbb') {
                    delegateArr.push(userSysID);  // Add the user sys_id to the array if delegation_rule matches
                }
            }
        }

        // Return the array of user sys_ids (delegators)
        return delegateArr;
    },

    type: 'getIAmDelegate'
};


Use case scenario:
I am using this dynamic filter in the GRC Modules. Entity owner (IS DYNAMIC) I am Delegate

In other words, when a user is set as a delegate with the specified delegation rule, he/she should see his/her Controls and also Controls where the Entity owner is an user, who created a delegation.

But for some reason my script include does not work.

I also had this script include, which worked perfectly, but it had to be adjusted:

function getIAmDelegate() {
var grDelegate = new GlideRecord('sys_granular_delegate');
var now = new GlideDateTime();
grDelegate.addEncodedQuery('delegate=' + gs.getUserID() + '^starts<=' + now + '^ends>=' + now);
grDelegate._query();

var delegateArr = [];
while (grDelegate._next()) {
delegateArr.push(grDelegate.getValue('user'));
}

return delegateArr;
}


Would you know, why does not it work?
I am a bit lost and to be honest, not that experienced in scripting.




1 ACCEPTED SOLUTION

Ok, so the resolution is: stupid me.
It really was the way I was calling the script.

What works for me now is this: new global.getIAmDelegate().getIAmDelegate();

getIAmDelegate() worked in my previous script so I presumed it will work in this one as well.
Also, thank you for your advice about the good practice. I will rename my function.

Thanks a lot, Brian!!

View solution in original post

3 REPLIES 3

Brian Lancaster
Tera Sage

try in the script field getIAmDelegate().getIAmDelegate(). Typically you have to call the script include and the function unless you create a specific type of script include which I cannot remember the name of. Also I don't think it is good practice to have the script include and function names the same.

Hello Brian,

thanks a lot for your reply.
I've tried getIAmDelegate().getIAmDelegate() in the script field but it still does not work.

I have the same script as a background script and it gives me correct values.
My background script:

// The logged-in user sys_id
var currentUserSysID = 'eda3ca921b440e50b635ddb69b4bcb3b';  // Replace this with the current logged-in user's sys_id if needed

// Create a GlideRecord object for the 'sys_granular_delegate' table
var grDelegate = new GlideRecord('sys_granular_delegate');
var now = new GlideDateTime();

// Query 'sys_granular_delegate' where the current logged-in user is the delegate
// and the delegation is valid (within the start and end dates)
grDelegate.addEncodedQuery('delegate=' + currentUserSysID + '^starts<=' + now + '^ends>=' + now);
grDelegate.query();

// Initialize an array to store the matching user sys_id values
var delegateArr = [];

// Log a message to indicate script start
gs.info('--- Starting Background Script to Print All sys_ids and Values ---');

// Loop through the 'sys_granular_delegate' records
while (grDelegate.next()) {
    var userSysID = grDelegate.getValue('user');  // Get the user sys_id (delegator)
    var delegateSysID = grDelegate.getValue('delegate');  // Get the delegate sys_id

    // Log the sys_id of the current 'sys_granular_delegate' record and associated fields
    gs.info('sys_granular_delegate Record sys_id: ' + grDelegate.getValue('sys_id'));
    gs.info('Delegate sys_id (current user): ' + delegateSysID);
    gs.info('User sys_id (delegator): ' + userSysID);

    // Now, query the 'sys_m2m_delegate_rule' table to validate the delegation rule
    var grDelegateRule = new GlideRecord('sys_m2m_delegate_rule');
   
    // Add a query to check if the 'delegate' field in 'sys_m2m_delegate_rule' matches the current 'sys_granular_delegate' sys_id
    grDelegateRule.addEncodedQuery('delegate=' + grDelegate.getValue('sys_id'));  // Use the sys_id of 'sys_granular_delegate'
    grDelegateRule.query();

    // If a matching rule is found
    while (grDelegateRule.next()) {
        // Log the sys_id of the current 'sys_m2m_delegate_rule' record and associated fields
        gs.info('sys_m2m_delegate_rule Record sys_id: ' + grDelegateRule.getValue('sys_id'));
        gs.info('sys_created_by: ' + grDelegateRule.getValue('sys_created_by'));
        gs.info('delegation_rule sys_id: ' + grDelegateRule.getValue('delegation_rule'));

        // Compare the delegation_rule from sys_m2m_delegate_rule directly with the expected GRC delegation rule sys_id
        if (grDelegateRule.getValue('delegation_rule') === '5a9a9d631b541210b635ddb69b4bcbbb') {
            gs.info('Matched GRC Delegation Rule for user sys_id: ' + userSysID);
            delegateArr.push(userSysID);  // Add the user sys_id to the array if delegation_rule matches
        }
    }
}

// Log the final array of user sys_ids (delegators)
gs.info('Final delegateArr (List of User sys_ids): ' + delegateArr.toString());

// Log a message to indicate script end
gs.info('--- Finished Background Script ---');



Ok, so the resolution is: stupid me.
It really was the way I was calling the script.

What works for me now is this: new global.getIAmDelegate().getIAmDelegate();

getIAmDelegate() worked in my previous script so I presumed it will work in this one as well.
Also, thank you for your advice about the good practice. I will rename my function.

Thanks a lot, Brian!!