True/false field to empty after update

Christian Alm
Tera Contributor

Hey guys, have a need to empty a true/false field after update. Where should I empty it to get this to work correctly?

 

Scenario is that we need to have a "customer visible" true/false field defaulting to false, but the user can set it to true for a specific update -> this update is sent via a business rule to our integration platform and from there forwarded to the customer via REST. This true/false value can NOT stay true after the update, ie. the next update done to the ticket should default to false again unless the user sets it again to true.

 

Regards,

Christian

1 ACCEPTED SOLUTION

Maddysunil
Kilo Sage

@Christian Alm 

I think to achieve the requirement of emptying (setting to false) a true/false field after an update, you can write after update business rule:

 

(function executeRule(current, previous /*null when async*/) {
    // Check if the true/false field is currently true
    if (current.true_false_field) {
        // If true, set it to false
        current.true_false_field = false;
        gs.info("True/false field has been reset to false.");
    }
})(current, previous);

 

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

View solution in original post

5 REPLIES 5

Maddysunil
Kilo Sage

@Christian Alm 

I think to achieve the requirement of emptying (setting to false) a true/false field after an update, you can write after update business rule:

 

(function executeRule(current, previous /*null when async*/) {
    // Check if the true/false field is currently true
    if (current.true_false_field) {
        // If true, set it to false
        current.true_false_field = false;
        gs.info("True/false field has been reset to false.");
    }
})(current, previous);

 

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Shubham26
Tera Guru

Hi @Christian Alm,

 

I hope, you are well.

I gone through your scenarios, look like you are updating field "customer visible" as true with BR (run on some condition), and call REST API to send data to your customer. Now you have to reset field value as default (False).

 

I believe, you are calling Rest API through BR, and getting response once you sending data. So In same BR, you can update "customer visible" field as False with below Script, once your receive success response (200 Code or similar).

 

current.setValue("u_customer_visible", false);

 

 

Please Accept answer and Mark Shubham26_0-1710752361243.pngCorrect if this solves your query and also mark Shubham26_1-1710752361248.pngHelpful if you find my response worthy based on the impact.

 

Thank You.

 

Regards,

Shubham Gupta

Amit Pandey
Kilo Sage

Hi @Christian Alm 

 

I believe you must be triggering the integration with the business rule. Upon successful API response, you can reset the field to its default value (False) using following script in the same Business Rule-

 

 

current.setValue("u_customer_visible", false); 
gs.info("The customer visible field has been reset to false after successful response.");

 

 

Regards,

Amit Pandey

Sujit Jadhav
Tera Guru

Hello @Christian Alm ,

 

To ensure that the "customer visible" true/false field defaults to false after each update, you can achieve this by utilizing a combination of a Business Rule and a Script Include in ServiceNow.

Here's a high-level approach to implement this:

Business Rule:

Create a Business Rule on the table where the "customer visible" field resides (e.g., incident, problem, etc.).
Configure the Business Rule to trigger on the before update event.
In the Business Rule script, call a Script Include method to reset the field value to false before the update is committed to the database.
Script Include:

Create a Script Include that contains a function to reset the "customer visible" field to false.
This function will be called from the Business Rule to perform the reset operation.
Here's an example implementation:

Script Include (ResetCustomerVisibleField):

Create a Script Include named ResetCustomerVisibleField.
Define a function inside the Script Include to reset the "customer visible" field to false:


var ResetCustomerVisibleField = Class.create();
ResetCustomerVisibleField.prototype = {
initialize: function() {},

resetToFalse: function(currentRecord) {
// Reset the "customer visible" field to false
currentRecord.customer_visible = false;
}
};

 

2. Business Rule:

Create a Business Rule named Reset Customer Visible Field.
Set the Advanced field to true to run the Business Rule in a specific order.
Configure the conditions for the Business Rule as needed.
Write the script to call the resetToFalse function from the Script Include:(function executeRule(current, previous /*null when async*/) {
// Instantiate the ResetCustomerVisibleField Script Include
var resetField = new ResetCustomerVisibleField();

// Call the resetToFalse function to reset the field to false
resetField.resetToFalse(current);
})(current, previous);     


                                                                                                                                                               
Please mark my answer Correct/Helpful, If applicable


Regards,

Sujit