Objects like "gr" pass information as a reference. If you are working with objects it is always a good practice to explicitly convert objects to a native type (if possible). Otherwise you may end up getting into issues where gr just serves the last order multiple times.
If you are unsure of what it means, try running below code in background script of your personal developer instance.
(function() {
var incNumbers = [];
var incRecord = new GlideRecord("incident")
incRecord.setLimit(3);
incRecord.query();
while(incRecord.next()) {
incNumbers.push(incRecord.number));
}
gs.info("Incident numbers are "+incNumbers.join(","));
})();
Code will print the last number three times, as the array used the reference of incident numbers than actual values. A simple toString() method will allow you to get actual number values.
(function() {
var incNumbers = [];
var incRecord = new GlideRecord("incident")
incRecord.setLimit(3);
incRecord.query();
while(incRecord.next()) {
incNumbers.push(incRecord.number.toString()));
}
gs.info("Incident numbers are "+incNumbers.join(","));
})();
You can use either of toString(), getValue(), getDisplayValue() methods depending on the type of field you are working with.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Comments
Post a Comment