Passing Parameters from a script include into a Business Rule

matthew_hughes
Kilo Sage

I've been writing the following script include to carryout two checks on the 'u_cmdb_ci_capability_provisioning':

  • If a provider has multiple recipients for the same capability
    or
  • If there is only one Recipient and that recipient has ‘OCIR Shared Service Recipient’ = True

 

My script include is below:

 

var LBGSPCSharedServicefield = Class.create();
LBGSPCSharedServicefield.prototype = {


        existingSPCS: function() {

            var shared_service = false;

            var recipient = this.getParameter('recipient');
            var provider = this.getParameter('provider');
            var capability = this.getParameter('capability');
            //var spcRecord = this.getParameter('spcRecord');
            var spcRecord = '6670148fdb17c894581b96d5db96190c';
            var department = 'c04a04bddbb608d080b2f43c0c961901';
            var retired = 7;

            var spcRecords = new GlideRecord('u_cmdb_ci_capability_provisioning');
            spcRecords.addQuery('u_provider', provider);
            spcRecords.addQuery('u_capability', capability);
            spcRecords.addQuery('install_status', '!=', retired);
            spcRecords.addQuery('sys_id', '!=', spcRecord);
            spcRecords.query();
           

            if (spcRecords.next()) {
                gs.log('The only record to show is ' + spcRecords.getValue('sys_id'));
                //shared_service = true;
                //spcRecords.u_shared_service = true;
                //spcRecords.update();
                //gs.print('The Shared Service field has now been updated to' + spcRecords.getValue('u_shared_service'));
                return true;

            } else {
                var recipientDepartment = new GlideRecord('cmn_department');
                recipientDepartment.addQuery('u_active', true);
                recipientDepartment.addQuery('u_ocir_shared_service_recipient', true);
                recipientDepartment.addQuery('sys_id', department);
                recipientDepartment.query();
                if (recipientDepartment.next()) {
                    var recipientRecords = new GlideRecord('u_cmdb_ci_capability_provisioning');
                    recipientRecords.addQuery('install_status', '!=', retired);
                    recipientRecords.addQuery('u_recipient', recipientDepartment);
                    recipientRecords.query();
                    if (recipientRecords.next()){
                        gs.log('MH SPC record is ' + recipientRecords.getValue('sys_id'));
                        //shared_service = true;
                        //recipientRecords.u_shared_service = true;
                        //recipientRecords.update();
                        return true;
                    }
                    else {
                        return false;
                        //shared_service = false;
                    }
                }
            }return shared_service;
        },

    type: 'LBGSPCSharedServicefield'
};
 
 
When I had this reviewed, I was told that I need to pass in a gliderecord of the record I'm checking, however, I don't know what this is or how to do that. 
 
The specification of my function needs to be:

 

Input: a GlideRecord containing an SPC record

Output: A true/false value of whether it meets the criteria or not.

 

If someone could explain to me what I need to do to pass the gliderecord of the record I'm checking as well as the specification, that would be great.


5 REPLIES 5

Abhay Kumar1
Giga Sage

@matthew_hughes Currently, your script is trying to query the records internally using GlideRecord objects based on specific criteria. Instead, you need to pass in the GlideRecord of the record you're checking directly as a parameter to your function. This will allow the function to use the existing record without querying for it again.

Input: A GlideRecord object representing an SPC record (e.g., from the u_cmdb_ci_capability_provisioning table).

Output: A boolean (true/false) indicating whether the record meets the specified criteria.

Pls try if helps.

Hi @Abhay Kumar1 What do I need to do to pass the gliderecord of the record as a parameter? I've not really done script includes before.

@matthew_hughes Refactor code:

var LBGSPCSharedServicefield = Class.create();

LBGSPCSharedServicefield.prototype = {

    initialize: function() {},

 

    /**

     * Checks if the provided SPC record meets shared service criteria.

     * @param {GlideRecord} spcRecord - The SPC GlideRecord object to check.

     * @returns {boolean} - Returns true if the record meets shared service criteria, otherwise false.

     */

    isSharedService: function(spcRecord) {

        // Validate the input GlideRecord

        if (!spcRecord || !spcRecord.isValidRecord()) {

            gs.log('Invalid input: The provided record is not a valid GlideRecord.');

            return false;

        }

 

        // Get the department from the provided SPC record

        var department = spcRecord.getValue('department');

        if (!department) {

            gs.log('No department specified on the SPC record.');

            return false;

        }

 

        // Query the recipient department

        var recipientDepartment = new GlideRecord('cmn_department');

        recipientDepartment.addQuery('u_active', true);

        recipientDepartment.addQuery('u_ocir_shared_service_recipient', true);

        recipientDepartment.addQuery('sys_id', department);

        recipientDepartment.query();

 

        // Check if a matching recipient department is found

        if (recipientDepartment.next()) {

            // Query the u_cmdb_ci_capability_provisioning table for active records

            var recipientRecords = new GlideRecord('u_cmdb_ci_capability_provisioning');

            recipientRecords.addQuery('install_status', '!=', 'retired');

            recipientRecords.addQuery('u_recipient', recipientDepartment.getUniqueValue());

            recipientRecords.query();

 

            // Return true if a matching recipient record is found

            if (recipientRecords.next()) {

                gs.log('MH SPC record found: ' + recipientRecords.getValue('sys_id'));

                return true;

            } else {

                gs.log('No active recipient records found.');

                return false;

            }

        }

 

        gs.log('No matching recipient department found.');

        return false;

    },

 

    type: 'LBGSPCSharedServicefield'

};

 

Returns true if a matching record is found; otherwise, it returns false.

 

I am not sure how you calling but we can use something like :

var spcRecord = new GlideRecord('u_cmdb_ci_capability_provisioning');

if (spcRecord.get('sys_id', 'your_spc_record_sys_id')) {

    var sharedServiceUtil = new LBGSPCSharedServicefield();

    var isShared = sharedServiceUtil.isSharedService(spcRecord);

    gs.log('Is shared service: ' + isShared);

}

The function now accepts a GlideRecord as input (spcRecord).

It follows the requested input/output specification.

The code includes logging for debugging purposes but can be removed or commented.

 

Please try if helps.

 

 

Hi @Abhay Kumar1  It would be called from a Business rule.