Setting a field to true based on a script include and business rules

matthew_hughes
Kilo Sage

I've created a script include and two business rules to update the 'Shared Service' field based on the below logic:

I can see that this rule triggers and updates the USB if necessary, when

  1. A recipient is updated
  2. A department is updated to have the new ‘OCIR Shared Service Recipient’ field set to True, or to False. (if the OCIR Shared Service Recipient’ field is set to False, then the shared service field only gets set to false if it also does not meet the other rule of 'If a provider has multiple recipients for the same capability)
  • If a provider has multiple recipients for the same capability and If a Recipient has ‘OCIR Shared Service Recipient’ = True then the USB shared service field = True
  • If a provider has multiple recipients for the same capability and If a Recipient has ‘OCIR Shared Service Recipient’ = False, then the USB shared service field = True
  • If a provider does not have multiple recipients for the same capability and If a Recipient has ‘OCIR Shared Service Recipient’ = True then the USB shared service field = True
  • If a provider does not have multiple recipients for the same capability and If a Recipient has ‘OCIR Shared Service Recipient’ = False then the USB shared service field = False

The script include that I've created is:

var LBGSPCSharedServicefield = Class.create();
LBGSPCSharedServicefield.prototype = {
    currentData: function(current) {
        gs.log('MH The current UBS record is ' + current.sys_id);
        gs.log('MH The current Capability is ' + current.u_capability);
        gs.log('MH The current Provider is ' + current.u_provider);
        gs.log('MH The current Recipient is ' + current.u_recipient);
        gs.log('MH The current Status is ' + current.install_status);

        var retired = 7;

        var shared_service = false;

        var spcRecords = new GlideRecord('u_cmdb_ci_capability_provisioning');
        spcRecords.addQuery('u_provider', current.u_provider);
        spcRecords.addQuery('u_capability', current.u_capability);
        spcRecords.addQuery('install_status', '!=', retired);
        spcRecords.addQuery('sys_id', '!=', current.sys_id);
        //spcRecords.setLimit(10);
        spcRecords.query();
        gs.log('MH Number of records found: ' + spcRecords.getRowCount());

        if (spcRecords.hasNext()) {
            gs.info('The Sys ID of this record is ' + spcRecords.getValue('sys_id'));
            shared_service = true;
            spcRecords.u_shared_service = true;
            spcRecords.updateMultiple();
        } else {
            shared_service = false;
        }
        current.u_shared_service = shared_service;
        current.update();
    },


    previousData: function(previous, current) {
        var retired = 7;

        var shared_service = false;

        if ((previous.u_provider != current.u_provider) || (previous.u_capability != current.u_capability) || (previous.install_status != current.install_status)) {

            var spcRecordsPrevious = new GlideRecord('u_cmdb_ci_capability_provisioning');
            spcRecordsPrevious.addQuery('u_provider', previous.u_provider);
            spcRecordsPrevious.addQuery('u_capability', previous.u_capability);
            spcRecordsPrevious.addQuery('install_status', '!=', retired);
            spcRecordsPrevious.addQuery('sys_id', '!=', current.sys_id);
            //spcRecordsPrevious.setLimit(10);
            spcRecordsPrevious.query();
            gs.log('MH Number of records found: ' + spcRecordsPrevious.getRowCount());

            // if we have found some records ...
            while (spcRecordsPrevious.next()) {
                // if there is only 1 record and ..
                // the capability or provider has changed or the current record is retired ...
                if ((spcRecordsPrevious.getRowCount() == 1) && ((previous.u_provider != current.u_provider) || (previous.u_capability != current.u_capability) || (current.install_status == retired))) {
                    // this can't be a shared service
                    spcRecordsPrevious.u_shared_service = false;
                } else {
                    // this is a shared service
                    spcRecordsPrevious.u_shared_service = true;
                }

                // update the record (but do not allow recursive business rules to run)
                spcRecordsPrevious.setWorkflow(false);
                spcRecordsPrevious.update();
            }

            gs.log('MH The current UBS record is ' + spcRecord);
            gs.log('MH The previous Capability is ' + previousCapability);
            gs.log('MH The previous Provider is ' + previousProvider);
            gs.log('MH The previous Recipient is ' + previousRecipient);
        }
    },

    currentDepartment(current) {
            var spcRecord = 'f38b83601b59fd5cede5c315464bcb71';
            var sharedService = false;
            var retired = 7;

            var spcRecords = new GlideRecord('u_cmdb_ci_capability_provisioning');
            //spcRecords.addQuery('sys_id', spcRecord);
            spcRecords.addQuery('install_status', '!=', retired);
            spcRecords.addQuery('u_recipient', current.sys_id);
            spcRecords.query();
            gs.log("MH Total number of records is " + spcRecords.getRowCount());
            gs.log('MH The affected recipient is ' + spcRecords.u_recipient);
            while (spcRecords.next()) {
                if (spcRecords.u_provider == current.u_provider && spcRecords.u_capability == current.u_capability) {
                    sharedService = true;
                }
                if (spcRecords.u_provider != current.u_provider && spcRecords.u_capability != current.u_capability)  {
                    if (spcRecords.u_recipient.u_ocir_shared_service_recipient == true) {
                        sharedService = true;
                        gs.log("MH u_ocir_shared_service_recipient is set to true");
                        gs.log("MH Shared Service is set to " + sharedService);
                        //spcRecords.u_shared_service = true;
                        //spcRecords.updateMultiple();
                    } else {
                        sharedService = false;
                        gs.log("MH u_ocir_shared_service_recipient is set to false");
                        gs.log("MH Shared Service is set to " + sharedService);
                        //spcRecords.u_shared_service = false;
                        //spcRecords.updateMultiple();
                    }
                }
           
            spcRecords.setWorkflow(false);
            spcRecords.u_shared_service = sharedService;
            spcRecords.update();
            }

    },
    type: 'LBGSPCSharedServicefield'
};
 
The business rule to trigger the update from the capability provisioning table is:
matthew_hughes_5-1731927683999.png

 

 
matthew_hughes_2-1731927601078.png

 

 

The Business Rule  to trigger the update from the department table is: 

matthew_hughes_3-1731927644320.png

 

matthew_hughes_4-1731927663568.png

 

However, what I find is that if I set the 'OCIR Shared Service Recipient' field to false in a department record, it automatically sets all of the capability records with that department as the 'Recipient' to false. 

 

matthew_hughes_6-1731927837848.png

For the above two records, the 'Shared Service' field should be set to True as the provider has multiple recipients for the same capability.

 

Does anyone know where I'm going wrong and what I need to change?

 

 

0 REPLIES 0