I'm trying to compare the content of a Pick List to a string in a script using the "===" operator but the result is always false. What am I missing?
Solved! Go to Solution.
Solved by Andrew.Shaw. Go to Solution.
Comparison operators are used in logical statements to determine equality or difference between variables or values. Therefore, it is very important to understand data types when you try to access PLM 360 fields through scripting.
Let’s take a simple example:
You’ve created a workspace where users can save training webinars. One of the fields is a Pick List from which the user has to select the author of the webinar (the Pick List gets its data from another workspace). A workflow manages the various states the webinar has to go through before being made public and transitions have precondition scripts that restrict them to specific groups, i.e. only the author or his/her manager can transition from state C to state D.
So, you’ve written a script such as this:
var author = item.AUTHOR;
var user1 = Security.loadUser(login);
var authorCheck = user1.lastName + ", " + user1.firstName;
if (Security.inGroup(userID,'Manager')===true){
isOK = true;
} else {
if (author === authorCheck) {
isOK = true;
} else {
isOK = false;
}
}
returnValue(isOK);
This script will always return isOK as false and finding the reason can be a bit tricky. The culprit here is the data type of the “author” variable. Logic would tell you that a Pick List will return a string but that is true only if said Pick List gets its data from defined values. In other cases, the data type returned is Object.
So, to make your script work, you need to modify the script like this: var author = String(item.W_AUTHOR);.
To help you out for your next scripts, here’s a table that shows the data type of each field type:
If you want to run your own tests, here’s the script you need: println(typeof (item.FIELD_NAME));