Or Condition in While Loop

alexcharleswort
Tera Expert

Quick scripting question as I'm not entirely experienced with my while loops. This a before insert/update BR:

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

var person = current.u_person;

var mgr = person;

while(mgr.manager != 'af7ef72a89488500e4382dea5d5a1cee'){

mgr = mgr.manager;

}

current.u_manager = mgr;

current.u_division_director = mgr.manager;

})(current, previous);

Basically my while script is waiting for the Manager to be a specific person. This works awesome. and is filling u_manager with the right person every time.   What I want is for it to wait for the Manager to either be one person OR the other. So I tried changing my while too look like

while(mgr.manager != 'af7ef72a89488500e4382dea5d5a1cee' || mgr.manager != '8a8d381d8915c900e4382dea5d5a1cc0'){

....

And...

while(mgr.manager != 'af7ef72a89488500e4382dea5d5a1cee' || '8a8d381d8915c900e4382dea5d5a1cc0'){

....

And...

while(mgr.manager != ('af7ef72a89488500e4382dea5d5a1cee' ||   '8a8d381d8915c900e4382dea5d5a1cc0')){

....

Is it not possible to do this? Am i just getting the format wrong? Should one of those have worked and I just typed it incorrectly?

These just make the page keep spinning...

Thanks in advance for your help!

1 ACCEPTED SOLUTION

That's a good idea, and actually the one I started with, but in this case i found I need to do a while loop so it keeps going until it find the right sys_id to push into the right field.


Alas, I figured it out, I should have used the AND condition. Not the OR condition.



Thanks for taking a look at this!


View solution in original post

4 REPLIES 4

Shishir Srivast
Mega Sage

Hi Alex,



you can use if condition instead of while loop.



  1. if(mgr.manager != 'af7ef72a89488500e4382dea5d5a1cee' || mgr.manager != '8a8d381d8915c900e4382dea5d5a1cc0'){  
  2. ....  


Are you looking to get the manager of the Person?




That's a good idea, and actually the one I started with, but in this case i found I need to do a while loop so it keeps going until it find the right sys_id to push into the right field.


Alas, I figured it out, I should have used the AND condition. Not the OR condition.



Thanks for taking a look at this!


Chuck Tomasi
Tera Patron

Your first one is the best one Alex. You are looking for A || B to be true where A is manager != sysID1   and B is manager != sysID2



Try this instead... since mgr.manager is an object and may not compare equality to a literal string... get the sysID by itself first.



Since I don't see any looping type of objects (e.g. an array, an IF might be better here.



var mgrSysID = mgr.getValue('manager');



if (mgrSysID != 'af7ef72a89488500e4382dea5d5a1cee' || mgrSysID != '8a8d381d8915c900e4382dea5d5a1cc0') {


        mgr = mgr.person;


}


j_martin
Tera Guru

You need to use AND (&&) in that scenario.



With the OR operator you will never exit that loop. If manager DOES equal 'af7ef72a89488500e4382dea5d5a1cee' then your next condition (mgr.manager != '8a8d381d8915c900e4382dea5d5a1cc0') will always evaluate to true.