How to do this without client script, In servicenow, i have a ritm and there is a reference field in the ritm called asset, and refer to the harware table. My requirement is if anyone select the asset in this field and then try to save the form, it will check if the ritm variable named transfer is yes. Then it will check for the other items in which this asset is already selected. If yes then prevent the form to submit and show error message with the ritm name in which it is already selected.
Assistant
User
Still able to select if i save it again
Assistant
User
I want to one more condition in same business rule, that if asset is selected in transfer yes variable ritm and someone tries to add that asset in another ritm with conditions transfered no . Then same error i want with the ritm number and query the transfer order table and search the ritm number in transfer table in which the asset is already selected and show the transfer order number also in the error
Assistant
User
Business rule if state is In stock and substate is available then clear reserved for value
Assistant
To achieve the outlined requirement in ServiceNow without using a client script, you can leverage a Business Rule with a combination of server-side scripting and GlideRecords. Below are the steps to achieve this:
Create Business Rule on the RITM Table:
Navigate to System Definition > Business Rules in the Application Navigator.
Click on New to create a new business rule.
Configure the business rule accordingly:
Name: Prevent Duplicate Asset Selection
Table: Requested Item [sc_req_item]
When to run: Select before and Insert/Update (Since we want to prevent saving the form before inserting or updating)
Conditions and Scripting:
Under the Conditions tab, add a condition to check if the transfer variable is yes.
Use the Advanced tab to define the script to handle the database check and error messaging. Here is an example script to get you started:
javascript
// Check if transfer variable is yes
if (current.variables.transfer == 'yes' && current.asset) {
// Query RITMs to see if this asset is already selected elsewhere
var ritmGR = new GlideRecord('sc_req_item');
ritmGR.addQuery('asset', current.asset);
ritmGR.addQuery('sys_id', '!=', current.sys_id); // Exclude the current RITM
ritmGR.query();
if (ritmGR.next()) {
// Asset already selected in another RITM, block the form submission
var message = 'Asset is already selected in another RITM: ' + ritmGR.number;
gs.addErrorMessage(message);
gs.setRedirect(null); // Prevents form submission
current.setAbortAction(true); // Abort the current action
}
}
Explanation:
The script checks if the transfer variable in the RITM is yes and if the asset field is populated.
It then uses GlideRecord to query the sc_req_item table for any records (RITMs) where the asset field has the same value as in the current RITM, excluding the current RITM using sys_id condition.
If such a record is found (ritmGR.next()), it constructs an error message using the RITM number and uses gs.addErrorMessage() to show the error on the form.
gs.setRedirect(null) prevents the form from completing the current save action.
current.setAbortAction(true) aborts the record from being inserted or updated.
Save and Activate:
Once you've configured the business rule, save it and make sure it is active.
This Business Rule will ensure that users cannot select an already-used asset when the transfer variable is marked as yes, effectively ensuring data integrity and preventing multiple transfers of the same asset.
If the Business Rule does not prevent the form from being saved upon a subsequent attempt, it's important to make sure all conditions and logic are correctly implemented and that they factor in both insert and update operations. Here's a revised approach to ensure that the form submission is prevented under all scenarios:
Revisit The Conditions:
Make sure you have correctly set the conditions within the Business Rule to handle both insert and update operations.
Modify the Script to Ensure Robust Handling:
Here's an optimized script that should handle both initial and subsequent save attempts. This is a more robust version that includes explicit checks and handling conditions correctly.
javascript
(function executeRule(current, previous /*null when async*/) {
// Check if transfer variable is yes
if (current.variables.transfer == 'yes' && current.asset) {
// Query RITMs to see if this asset is already selected elsewhere
var ritmGR = new GlideRecord('sc_req_item');
ritmGR.addQuery('asset', current.asset);
ritmGR.addQuery('sys_id', '!=', current.sys_id); // Exclude the current RITM
ritmGR.query();
if (ritmGR.hasNext()) {
// Asset already selected in another RITM, block the form submission
var message = 'Asset is already selected in another RITM: ' + ritmGR.number;
gs.addErrorMessage(message);
current.setAbortAction(true); // Abort the current action and prevent submission
return; // Ensure we exit the script
}
}
})(current, previous);
Specific Configuration of Business Rule:
Name: Prevent Duplicate Asset Selection
Table: Requested Item [sc_req_item]
When to run:before [Insert], before [Update]
Testing Conditions:
Ensure both the transfer variable is checked if it is equal to yes and that other necessary variables or states are properly set.
Note: The current.setAbortAction(true) line is key to preventing the submission. Ensure this is correctly in place and that the script logic exits properly using return if conditions to abort are met.
Additional Checks:
Business Rule Activation: Ensure the rule is active.
Order of Execution: Ensure no other conflicting Business Rules precede this rule in execution order.
Client-side Caching: If dealing with any potential client-side caching issues, clear the browser cache or test in a new private/incognito window.
Debugging:
You can add gs.info() statements to log actions within the script to the system logs to verify the logic's flow and identify where it may fail.
javascript
(function executeRule(current, previous /*null when async*/) {
gs.info('Business Rule Execution Started');
// Check if transfer variable is yes
if (current.variables.transfer == 'yes' && current.asset) {
gs.info('Transfer is yes and asset is selected');
// Query RITMs to see if this asset is already selected elsewhere
var ritmGR = new GlideRecord('sc_req_item');
ritmGR.addQuery('asset', current.asset);
ritmGR.addQuery('sys_id', '!=', current.sys_id); // Exclude the current RITM
ritmGR.query();
if (ritmGR.hasNext()) {
gs.info('Found another RITM with the same asset');
// Asset already selected in another RITM, block the form submission
var message = 'Asset is already selected in another RITM: ' + ritmGR.number;
gs.addErrorMessage(message);
current.setAbortAction(true); // Abort the current action and prevent submission
return; // Ensure we exit the script
}
}
gs.info('No issues detected, proceeding with save');
})(current, previous);
By following these steps and ensuring all conditions are correctly implemented, you should be able to prevent the form submission when the asset is already selected in another RITM, and the transfer variable is set to yes.
To extend the existing Business Rule to include the additional condition where the transfer variable is set to no, we can incorporate another query to check the related transfer_order table for the asset and include pertinent details in the error message. Here’s how we can modify the script:
Steps:
Extend the Existing Business Rule Script:
Update the script to include the additional condition and query the transfer_order table. Here’s an improved version:
javascript
(function executeRule(current, previous /*null when async*/) {
// Check if transfer variable is yes
if (current.variables.transfer == 'yes' && current.asset) {
// Query RITMs to see if this asset is already selected elsewhere
var ritmGR = new GlideRecord('sc_req_item');
ritmGR.addQuery('asset', current.asset);
ritmGR.addQuery('sys_id', '!=', current.sys_id); // Exclude the current RITM
ritmGR.query();
if (ritmGR.hasNext()) {
// Asset already selected in another RITM, block the form submission
var message = 'Asset is already selected in another RITM: ' + ritmGR.number;
gs.addErrorMessage(message);
current.setAbortAction(true); // Abort the current action and prevent submission
return; // Ensure we exit the script
}
}
// Check if transfer variable is no and asset is selected in another RITM with transfer yes
if (current.variables.transfer == 'no' && current.asset) {
// Query the RITM table to check if the asset is already associated with a RITM that has transfer as yes
var ritmTransferGR = new GlideRecord('sc_req_item');
ritmTransferGR.addQuery('asset', current.asset);
ritmTransferGR.addQuery('variables.transfer', 'yes');
ritmTransferGR.addQuery('sys_id', '!=', current.sys_id); // Exclude the current RITM
ritmTransferGR.query();
if (ritmTransferGR.hasNext()) {
// Asset is in another RITM with transfer yes, now check transfer order table
var transferOrderGR = new GlideRecord('transfer_order'); // Replace 'transfer_order' with actual table name
transferOrderGR.addQuery('ritm', ritmTransferGR.sys_id); // Replace 'ritm' with the actual field name in transfer_order table
transferOrderGR.query();
if (transferOrderGR.hasNext()) {
// Generate error message with both RITM and transfer order number
var errorMessage = 'Asset is already selected in another RITM: ' + ritmTransferGR.number + ' with Transfer Order: ' + transferOrderGR.number;
gs.addErrorMessage(errorMessage);
current.setAbortAction(true); // Abort the current action and prevent submission
return; // Ensure we exit the script
}
}
}
})(current, previous);
Explanation:
Initial Check for transfer is yes:
The initial check remains as it was to prevent the form submission if the asset is already chosen in another RITM where transfer is yes.
Additional Condition for transfer is no:
If current.variables.transfer is no and current.asset is selected, it performs another query to see if the asset is associated with any RITM with transfer as yes.
Query the transfer_order Table:
If such an RITM exists, it queries the transfer_order table to find a matching record using the RITM in question.
It constructs an error message that includes both the RITM and transfer order numbers, and adds an error message to the form.
Abort Action:
In both scenarios, if conditions are met, the form submission is prevented using current.setAbortAction(true).
Setting up the Business Rule:
Name: Prevent Duplicate Asset Selection and Transfer Check
Table: Requested Item [sc_req_item]
When to run:before [Insert], before [Update]
Test and Activate:
Ensure all conditions are properly tested and validated, and activate the Business Rule.
By following these steps and enhancing the script logic, you ensure that any attempt to select an asset which is already involved in a transfer, whether yes or no, is properly checked and prevented if conditions are met. This preserves data integrity and prevents double-booking of assets.
To create a Business Rule that clears the value of the reserved_for field when the state is "In Stock" and the substate is "Available", you can follow these steps:
Create a New Business Rule:
Navigate to System Definition > Business Rules in the Application Navigator.
Click on New to create a new business rule.
Configure the business rule with the necessary details.
Configure the Business Rule:
Name: Clear Reserved For when In Stock and Available
Table: Choose the relevant table (for example, Asset [alm_asset], or a table specific to your implementation).
When to run: Set the business rule to run before Update (since we want to clear the field before saving any changes when the conditions are met).
Define the Conditions:
Set the conditions to check for State as "In Stock" and Substate as "Available".
Advanced Condition:
If you prefer, you can put the condition directly into the script for more complex checks. Here, we use the condition builder for simplicity.
State: is In Stock"
Substate: is Available
Script Tab:
In the Advanced tab, check the "Advanced" checkbox. Then, add the following script under the "Script" section:
javascript
(function executeRule(current, previous /*null when async*/) {
// Define the values for state and substate as per your table's dictionary entries
var STATE_IN_STOCK = '<value_for_in_stock>'; // Replace with actual value
var SUBSTATE_AVAILABLE = '<value_for_available>'; // Replace with actual value
// Check if the state is In Stock and substate is Available
if (current.state == STATE_IN_STOCK && current.substate == SUBSTATE_AVAILABLE) {
current.reserved_for = ''; // Clear the reserved_for field
}
})(current, previous);
Deactivate or Adjust Other Business Rules to Prevent Conflicts:
Ensure that no other business rules will conflict with or overwrite this one.
Explanation:
Retrieve State and Substate Values:
Replace 'value_for_in_stock' and 'value_for_available' with the actual values used in your instance for In Stock and Available. These values are typically found in the dictionary entries for the state and substate fields.
Condition Check:
The script checks if the state and substate fields meet the conditions of being "In Stock" and "Available" respectively.
Clear Reserved For Field:
If the conditions are met, the reserved_for field is cleared.
Ensure Execution Order:
Make sure the Business Rule is set to run before the record is saved so that the change is applied correctly.
Finalizing:
Save and activate the Business Rule.
Test the Business Rule to ensure it functions as expected by updating a record that meets the conditions.
If there are specific values for State and Substate based on dictionary entries or choice lists, inspect the choices in the respective table schema and update the script accordingly.
Dictionary Values (Optional Step):
If unsure about the exact values for state and substate, you can check the dictionary entries:
Navigate to the table schema and look for the state and substate fields to find their respective choice values.
By following these steps, you can create a robust Business Rule in ServiceNow that auto-clears the reserved_for field when the conditions specified are met.