Passing Parameters from a script include into a Business Rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2024 03:15 AM
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:
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2024 03:27 AM
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2024 03:35 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2024 06:19 AM
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2024 07:51 AM
Hi @Abhay Kumar1 It would be called from a Business rule.