Script help ! : A record should not be closed if it does not have a tag

Servicenow lear
Tera Contributor

We have a requirement:  

A user should not be able to move a record to close state if there is no tag associated to it on sn_si_task table.

 

I have written code but not working:

BR before for when state changes to closed complete

 

(function executeRule(current, previous /*null when async*/) {

      // Check if the record is being closed
  var closedState = 3// Replace with the correct state number for "Closed"
  var otherState = 2// Replace with the correct state number for the other state (e.g., "New")

  if (current.state == closedState && current.state.changes() && current.state.changes().previous == otherState) {
    // Check if the record has any tags associated with it
    if (current.tags.nil()) {
      // Prevent the incident from being closed and display an error message
      current.state.setValue(previous.state); // Revert the state change
      gs.addErrorMessage("Cannot close the record without a tag. Please add a tag before closing the incident.");
    }
  }

 

10 REPLIES 10

sushantmalsure
Mega Sage
Mega Sage

Hi @Servicenow lear 

Assuming your closed state is 3 and other state or previous state is 2.

 

Please change following line you code to this:

 if (current.state == closedState && current.state.changes() && previous.state == otherState) 

 

This condition will be applied when even record changes its state from 2 to 3 and then checks if tags are nil.

as per your script this is the only scenario when you want it show error message so corrected above line.

 

Try ,test it, if you still see any issue , please share the screenshot and use case you are trying with

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,Sushant Malsure

Sushant ,

 

Thanks for the response, 

I have made the code change and still able to close the record without a tag.

use case: User has to have a tag associated with the record before moving it to closed state.

 

(function executeRule(current, previous /*null when async*/) {

      // Check if the incident is being closed
  var closedState = 3// Replace with the correct state number for "Closed"
  var otherState = 2// Replace with the correct state number for the other state (e.g., "New")

  if (current.state == closedState && current.state.changes() && previous.state == otherState) {
    // Check if the incident has any tags associated with it
    if (current.tags.nil()) {
      // Prevent the incident from being closed and display an error message
      current.state.setValue(previous.state); // Revert the state change
      gs.addErrorMessage("Cannot close the incident without a tag. Please add a tag before closing the incident.");
    }
  }

Community Alums
Not applicable

Hi @Servicenow lear ,

 

As far as I see you want to prevent record from moving to Closed state when there is no Tags associated with it.

Then why do you need this line of code: if (current.state == closedState && current.state.changes() && previous.state == otherState)?

 

Just make an BR on before - Update - with condition:

[State changes from New to Closed]

AND

[Tags is Empty]

 

Actions tab:

[Abort action] ticked.

[Add message] ticked -> Input the message content.

 

 

Tags does not provide that option in query builder