Problem with an If condition in a workflow.

crhistopherjuar
Kilo Expert

Hi Experts!

I have an Issue with an If condition in a workflow.

Here the scenario:

When all the Catalog Task are closed with the 'Close Complete' state, the workflow has to follow the 'Yes' path, and when all the Catalog Task are closed by a different state, the workflow has to follow the 'NO' path.

The IF activity has the next condition.

var rec = new GlideRecord('sc_task');

rec.addQuery('request_item', current.sys_id);

//rec.addQuery('state', '=' ,3);

rec.query();

while(rec.next()){

if (rec.state != 3){

answer = 'no';

}

else if (rec.state == 3){

answer = 'yes';

}

}

The execution of the answer 'NO' works, but when the execution answer is 'YES', both conditions are triggered at the same time.

find_real_file.png

I hope that you can help me.


Regards

Crhistopher

1 ACCEPTED SOLUTION

It may not be looping but it is definitely passing through the if condition twice (once from the branch activity and once from the workflow).   The answer cannot simultaneously be 'Yes' and 'No' in a scripted condition, so unless you've modified the activity's transition conditions the highlighting you are seeing is from the two separate evaluations.



So the issue may be less related to the scripting and more to the workflow structure.   If you intend for it to be evaluated twice, the highlighting should not really be an issue.   It's just a representation of what happened.   If it should only pass from one or the other, you need to change the structure so that it is mutually exclusive and only 1 branch makes it to the if activity.


View solution in original post

8 REPLIES 8

Joe McCarty1
ServiceNow Employee
ServiceNow Employee

It appears to me that the workflow looped back around on the activity and ran it again.   It is likely the first pass (or maybe many) were Yes and the last pass was no.   Look at the workflow context workflow history related list to get an idea of how it's progressing.



Hopefully, the subflow has some waits in other wise it will just loop until the condition sends it down another path.



You may be better off with a scripted wait for condition depending on what you are trying to do and what those subflows do.


It may not be looping but it is definitely passing through the if condition twice (once from the branch activity and once from the workflow).   The answer cannot simultaneously be 'Yes' and 'No' in a scripted condition, so unless you've modified the activity's transition conditions the highlighting you are seeing is from the two separate evaluations.



So the issue may be less related to the scripting and more to the workflow structure.   If you intend for it to be evaluated twice, the highlighting should not really be an issue.   It's just a representation of what happened.   If it should only pass from one or the other, you need to change the structure so that it is mutually exclusive and only 1 branch makes it to the if activity.


Abhinay Erra
Giga Sage

use this code in the advanced section of the if activity.



answer = ifScript();



function ifScript() {


      var rec = new GlideRecord('sc_task');


      rec.addQuery('request_item', current.getValue('sys_id'));


      rec.query();


      var rec1 = new GlideRecord('sc_task');


      rec1.addQuery('request_item', current.getValue('sys_id'));


      rec1.addQuery('state',3);


      rec1.query();


      if(rec.getRowCount()==rec1.getRowCount()){


              return 'yes';


      }


      else{


              return 'no';


}


}


Hi Abhinay E,


I tested the code and still having the same problem