ThePorge Posted December 9, 2020 Share Posted December 9, 2020 My Setup: I have a datafile that has 16 items. The count for each item varies between 200 and 2000. Each item prints on a label that is 16up as a Cut & Stack. (16 print files when done). I have the Chunk breaks stack set to true in the On Job Start Rule. In the On Record start rule I have if (FusionPro.Composition.inputRecordNumber == 1) { myCount = 0; } if (Field("Switch") == "Changed") { myCount += 1; FusionPro.Composition.OpenNewOutputFile("141069_" + myCount + "-" + Field("Brand") + "-Label." + FusionPro.Composition.outputFormatExtension); } This works fine....However if I declare the Variable in the first if statement "var myCount = 0" I will get a file 1, but the rest are NaN. Can I get a clue as to why this is happening? Tks Quote Link to comment Share on other sites More sharing options...
Dan Korn Posted December 10, 2020 Share Posted December 10, 2020 This is the difference between a variable with local scope and a variable with global scope. These explain the concept of scope pretty well: https://www.w3schools.com/js/js_scope.asp https://developer.mozilla.org/en-US/docs/Glossary/Scope https://scotch.io/tutorials/understanding-scope-in-javascript Basically, when you say "var" inside the rule (not necessarily inside the "if' block), it creates a local variable, local to that *instance* of the rule, so next time you run the rule, it's as if that variable doesn't exist, i.e. it was never declared. When you don't use "var", it automatically creates a global variable, which stays around, outside the scope of that rule, and is always available. I do recommend using "var" in most cases, because you generally don't want global variables and subtle side effects from using the same variable name in different rules. However, in this case, you do want a global variable. The recommended thing to do here is to declare the global variable explicitly in the JavaScript Globals area, by clicking that button on the Rules dialog. In the JavaScript Globals, you can declare it like so: var myCount = 0; Then you don't need to initialize it in the OnRecordStart rule. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.