I was wondering if anyone could offer some advice or guidance why this code is not working as expected or im approaching it the wrong way...
I have the following code that is calling a configuration element that contains an attribute called “activedirectory” which is a array\composite type that contains 2 values (2 different environments which contain the AD controller and OU container for that domain)
I input the environment variable into this code (Prod or Non-Prod) and expect the code to match the environment name with the relevant value in the array but this is not case as the first value always gets returned regardless of environment name.
var configelement = System.getModule("company.vro.helpers").getConfigElement("activedirectory");
var domains = configelement.getAttributeWithKey("domains").value;
var domain;
for each (var obj in domains) {
if (obj.environment == environment);
domain = obj;
}
if (domain) {
sddcBuildOu = domain.sddcBuildOu;
} else {
throw "Domain not found - please ensure environment is supplied as 'Prod' or 'Non-Prod'"
}
Any feedback would be greatly appreciated
It surely has an effect as it makes the if statement effectively a no-op, causing the assignment domain = obj; to happen on each for loop iteration, and so when the loop ends, the value of the domain variable will always be set to the last element from domains array. Which is clearly incorrect.
Try it with some logging to see whats happening?
Also, at this line
if (obj.environment == environment); // <----- semi-colon here
Note sure if this is having an effect on your processing (it might not but you don't need it there)
It surely has an effect as it makes the if statement effectively a no-op, causing the assignment domain = obj; to happen on each for loop iteration, and so when the loop ends, the value of the domain variable will always be set to the last element from domains array. Which is clearly incorrect.
Thanks guys for the responses, it has been very helpful.
You are right that i am missing curly braces on the if statement around the domain object, once i added them it has fixed the issue.
Thanks again.
