Transferring script from a background script to a script include and business rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2024 01:17 AM
I'm trying the value of the Shared Service field of an SPC record based on the value of a field within a Department record. At the moment, I've got it working in a background script as below:
var test = new GlideRecord('u_cmdb_ci_capability_provisioning');
var provider = '1a1441c7c3fd1e909447fc37050131ae';
var capability = '42151005dbb2b748d84b9a2adb9619a4';
var retired = 7;
test.addQuery('u_provider', provider);
test.addQuery('u_capability', capability);
test.addQuery('install_status', '!=', retired);
test.query();
gs.log("The count is " + test.getRowCount());
if(test.getRowCount() > 1){
gs.log("The total number of records is " + test.getRowCount());
test.u_shared_service = true;
test.updateMultiple();
}
else if (test.getRowCount() == 1) {
while(test.next()){
gs.print('The value of the Shared Service Recipient field is ' + test.name);
if (test.u_recipient.u_ocir_shared_service_recipient == true) {
gs.log('This is true');
test.u_shared_service = true;
test.update();
}
else{
gs.log('This is false');
test.u_shared_service = false;
test.update();
}
}
}
However, what I'm wanting to do is transfer this functionality so that it works on a script include when it's triggered by a business rule. I've tried doing at as the following in my script include:
I'm wanting my my script include to refer to the current SPC records that are being updated by the affected department record. Does anyone know how I do this? The background script works because I've specified the Provider and Capability fields, but how do I do it in a script include where it refers to the current values?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2024 07:00 AM
I've now updated my business rule to be the following:
I've updated the parameters in my script include to be:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2024 07:20 AM
Hi @matthew_hughes ,
You are passing non-declare variable to addQuery which will not execute.
change from test.addQuery('install_status', '!=', retired);
To test.addQuery('install_status', '!=', 'retired');
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2024 07:26 AM
Good catch, @Runjay Patel. I hadn't paid attention to the install_status statement. Since this is a custom table, I'm not sure of how it's set up, but the others in the CMDB are number fields, so you would need to use the corresponding number of the install_status in this addQuery statement, not the display name.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2024 07:33 AM
Hi @matthew_hughes and @JenniferRah ,
If you are using OOB status filed the use number 7. like below.
test.addQuery('install_status', '!=', '7');
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-19-2024 07:39 AM
Hi @Runjay Patel I've already got variable declared:
Retired = 7
The code will run as expected if I specify the sys ID of a record from the 'u_cmdb_ci_capability_provisioning' table or the provider and capability from the ''u_cmdb_ci_capability_provisioning' table. I'm wanting the change to run if the 'OCIR Shared Service Recipient' field within the 'Recipient' field changes.