Dear community, I ask for your help in finding an error in my code. I can't find it myself :(
Task: go from the lower position in the organizational structure to upper division and get all the divisions, including the upper one.
My code seems logical to me, but when running it loops on one of the nodes, for example, on the Depute object (if you run it from the Engineer 1.1 object.).
I ask the respected community for help in solving the problem.
My code:
var gl = Context.getSelectedLanguage(); var startObjectOccs = ArisData.getSelectedObjOccs(); var myArr = []; for (var i=0;i<startObjectOccs.length;i++){ findUnits(startObjectOccs[i]); } for (var j=0;j<myArr.length;j++){ Dialogs.MsgBox("Found divisions: " + myArr[i]); } function findUnits(occ){ var currCnxArr = occ.Cxns(Constants.EDGES_INOUT); Dialogs.MsgBox("Check (length of the links array): " + currCnxArr.length); //var unitArr = []; for (i=0; i<currCnxArr.length;i++){ if (currCnxArr[i].getSource().ObjDef().TypeNum() == 45){ //if the source of connection is a position Dialogs.MsgBox("Check: Next node-position " +currCnxArr[i].getSource().ObjDef().Name(gl)); findUnits(currCnxArr[i].getSource()); } else if (currCnxArr[i].getSource().ObjDef().TypeNum() == 43 && currCnxArr[i].getDefinition().TypeNum() == 7){ //if the source of connection is a division Dialogs.MsgBox("Check: Next node-division " + currCnxArr[i].getSource().ObjDef().Name(gl)); //currCnxArr[j].getDefinition().TypeNum() == 9 myArr.push(currCnxArr[i].getSource().ObjDef().Name(gl)); findUnits(currCnxArr[i].getSource()); } } return; }
I don't know if this fixes your problem, but it'll most certainly help:
In your findUnits function you have a for loop that iterates with a variable i that isn't declared in your function, thus your function lets the variable i bleed into the global scope of your program:
You should correct
for (i=0; i<currCnxArr.length;i++){
to
for (var i=0; i<currCnxArr.length;i++){
Then in your second output loop you don't use j and instead use i for the array position, making it pretty unpredictable what it'll actually output:
for (var j=0;j<myArr.length;j++){
Dialogs.MsgBox("Found divisions: " + myArr[i]);
}
should be
for (var j=0;j<myArr.length;j++){
Dialogs.MsgBox("Found divisions: " + myArr[j]);
}