Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Abort action based on some value.

asasas
Kilo Contributor

I have a field name "counter", which count the number of times a task is reassigned to another person using "Assigned to" field.

Based   upon counter value (say 3) i.e. if same task is reassigned more than 3 times to another person, it should not allow to change.

I got answer where in database it is not allowing to change (once I refresh the page) but in "Incident form " it is allowing i.e. it is reassigned to another person in "Assigned to" field.
I want to stop user from changing in "Assigned to" field also   .

I am using Business rule and my code is :

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

  var counter;

  if(current.assignment_group.changes())

  {

  counter=current.u_counter=0;

    }

else if(current.assigned_to.nil())

  {

          current.u_counter=0;

    }

else if(current.u_counter>3)

  {

                gs.addInfoMessage("You excede more than 3 time   ! Now you cannot changed Assigned to field");

                current.state=previous.state;  

                gs.setValue('assigned_to', current.state);

            current.setAbortAction(true);  

            gs.setRedirect(current.state);  

  }

  else

  {

  current.u_counter += 1;

  }

})(current, previous);

Please help.

9 REPLIES 9

Geoffrey2
ServiceNow Employee
ServiceNow Employee

This assumes that your condition is: current.assigned_to.changes()




(function executeScript(current) {



      if (current.assignment_group.changes() || current.assigned_to.nil()) {


              current.u_counter = 0;


      }


      else {


              if (current.u_counter > 3) {


                      current.setAbortAction(true);


                      gs.addInfoMessage("This task has been reassigned more than 3 times. You cannot reassign it again.");


              } else {


                      current.u_counter++;


              }


      }



})(current);


asasas
Kilo Contributor

Dear Geoffrey



My problem is I have to stop user from further selecting/changing (not allowing   more than 3 times) in the assigned to field



Code is working fine, but i have to stop user from further reallocating to another person.


Geoffrey2
ServiceNow Employee
ServiceNow Employee

That's exactly what the code does.



But you should really be handling it with a Client Script on the form. That Business Rule should stay there in case the Incident is updated from the List View.


To handle it on the Client-side, I would create a display Business Rule:



g_scratchpad.counter = current.u_counter;


g_scratchpad.assignment_group = current.assignment_group;



Then a Client Script that executes when Assigned to changes


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading || oldValue == newValue)


      return;



  g_form.hideFieldMsg('assigned_to', true);



  if (newValue == '')


      return;



  var counter = parseInt(g_scratchpad.counter);


  var isSameGroup = g_scratchpad.assignment_group == g_form.getValue('assignment_group');



  if (isSameGroup && counter > 3) {


      g_form.setValue('assigned_to', oldValue);


      var msg = 'This task has been reassigned more than 3 times. You cannot reassign it again.';


      g_form.showFieldMsg('assigned_to', msg, 'error');


  }


}



The above Business Rule will then increment the counter when the form is saved.


asasas
Kilo Contributor

I think you code will work, but i new to SN, so i am not getting where to write this code.



2nd code i am writing in client script but i am not getting where to write those two line of code(1st code)