In servicenow We have a requirement where we have a record on "Request" table and when a "Request" got created from a record producer it will creates a "Request" record and the record will be in "Pending" state once it gets created.
So we have approve and reject buttons on the record level. Whenever user click on "Reject" button to reject the request then we are expecting a dialog box should be pop up on the record and ask the user to "Enter the reason for rejecting the request"
If user gives any reason as comments on that dialog box then the "Request" record should get rejected and that comments should be updated in another record which is "Item" Record and it should go to "Closed Incomplete" state.
Assistant
To achieve this functionality in ServiceNow, you will need to use a combination of client scripts and business rules. Below is a general approach you can take to fulfill this requirement. This solution involves creating a client script to prompt the user for a reason when they attempt to reject the request, and a business rule to handle the record update and transition based on the user's input.
Step 1: Add Reject Button with UI Action
First, ensure you have a "Reject" button available for the request record. You do this via a UI Action.
UI Action Configuration
Name: Reject Request
Action name: reject_request
Table: Request (or the exact table name, if it's a different one)
Condition: current.state == 'pending' (Adjust according to your field names and values)
Type: List and Form
Form button: True
Script:
javascript
// You might want to customize this script based on your exact requirements
function confirmRejection() {
var reason = prompt("Enter the reason for rejecting the request:");
if (reason != null && reason != "") {
g_form.setValue('u_rejection_reason', reason); // Assuming 'u_rejection_reason' is the field to hold rejection reason on Request record
g_form.setValue('state', 'rejected'); // Update this based on your 'Rejected' state value
g_form.save();
}
}
confirmRejection();
Step 2: Create a Business Rule for After State Change
Next, create a business rule that triggers after the state of a request changes to handle the update on the related item record.
Business Rule Configuration
Name: Handle Request Rejection
Table: Request (or your specific table name)
When to run: After, Update
Filter Conditions: State changes to Rejected (Adjust the condition based on your field names and values)
Script:
javascript
(function executeRule(current, previous /*null when async*/) {
// Check if the rejection reason is present
if (current.u_rejection_reason.nil()) {
return; // Exit if there's no rejection reason
}
// Retrieve the related item record. Assuming there's a reference field to item record named 'related_item'
var itemGR = new GlideRecord('item_table_name'); //Replace 'item_table_name' with the actual Item table name
if (itemGR.get(current.related_item)) {
itemGR.state = 'closed_incomplete'; // Set the state to 'Closed Incomplete'. Update the field and value as necessary.
itemGR.u_rejection_comments = current.u_rejection_reason; // Copy the rejection reason. Adjust field names as necessary.
itemGR.update();
}
})(current, previous);
Important Notes
Remember to replace placeholder names like u_rejection_reason, related_item, and item_table_name with the actual field names used in your instance.
The scripts provided here are simplified to illustrate the approach. You'll need to adjust the logic, field names, and conditions based on your specific ServiceNow setup.
Always test any new code in a sub-production or development instance before deploying it to production to ensure it doesn't impact other functionalities.
Depending on your requirements, you might need to adjust the UI Action's script to better handle asynchronous operations or to incorporate a more sophisticated UI prompt than the basic JavaScript prompt function. Consider using ServiceNow's Modal Window API for a more integrated user experience.