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

Community Alums
Not applicable

Hi @Servicenow lear ,

 

So the tags is in another table. Then the condition will be [State changes from New to Closed].

Script:

(function executeRule(current, previous /*null when async*/) {
    // == Query the sn_si_task table ==
  var gr = new GlideRecord('sn_si_task');
 gr.addQuery(...);
gr.query();
    if (!gr.next()) {
      // Prevent the incident from being closed and display an error message
      current.setAbortAction(true);
      gs.addErrorMessage("Cannot close the incident without a tag. Please add a tag before closing the incident.");
    }
  

I don't know the field in sn_si_task table so cannot build the query for you but you just need to query by your incident sys_id to find if there is any tags for that record in sn_si_task table. 

Thats the thing, query builder does not refer the tag table or anything related to tag , it only has option to call security tag field which we are not using

Community Alums
Not applicable

Hi @Servicenow lear ,

 

I am not sure the what is the 'Tag' field you are mentioning because in the Security Incident Response Task record, there is only the OOTB 'Security tags' field exists. Is that a custom field that you created? What is the field type? If reference type then what is the referenced table?

 

Try looking at the Form layout to see where it comes from / Go to the Field's dictionary also

 

In the Filter condition of BR, if it is added to form by dot-walking then try to 'Show related field' option and dot-walk to get the field.

generic tag which gets saved on label table.

 

Servicenowlear_0-1690818334831.png

 

Harish Bainsla
Kilo Patron
Kilo Patron

Create Business Rule

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


if (current.state == 4 && current.state.changesTo(4)) {
if (!current.tags.nil()) {
return;
} else {

current.state.setDisplayValue(previous.state.getDisplayValue());
gs.addErrorMessage("Cannot move the record to 'Closed' state without associated tags.");
current.setAbortAction(true);
}
}

})(current, previous);