Hadrian Posted September 29, 2011 Share Posted September 29, 2011 got a rule i am trying to use to validate the existence of a file starting with the current years last two digits and decrementing to 07 with a pad function in it(11, 10, 09, 08, and 07). I am not sure if the way i am going about it is correct since the loop variable i have either stops at the first or the last and does not actually verify the existence of the years in between. I tried a trycatch but i thought that perhaps that was too complicted as it did not work either. What am i missing here?? if (FusionPro.inValidation) { Rule("OnJobStart") } my_account = Field("Account"); my_ast = my_account.indexOf('*'); my_dash = my_account.indexOf('-'); my_uscore = my_account.indexOf('_'); my_asset = my_account.slice(my_ast + 1, my_dash); poster = ReplaceSubstring(my_asset, "CPS", "CPM"); function zeroPad(num,count) { var numZeropad = num + ''; while(numZeropad.length < count) { numZeropad = "0" + numZeropad; } return numZeropad; } CBI_Year = Right(GetYear(Today()), 2); for (i <= CBI_Year; i = 6; i--){ var number1 = i; var count1 = 2; var lessyear = "-" + zeroPad(i,count1); if (CreateResource(ImagePath + poster + lessyear + ".pdf", "graphic", false).exists) return CreateResource(ImagePath + poster + lessyear + ".pdf", "graphic", false); else return NullResource(); } Link to comment Share on other sites More sharing options...
esmith Posted September 29, 2011 Share Posted September 29, 2011 I'm no Dan Korn, but I think that this code is faulty: CBI_Year = Right(GetYear(Today()), 2); for (i <= CBI_Year; i = 6; i--) I believe that your code will return a string value of "11". Your FOR loop should start with the value followed by the condition which I think you meant to be (i=CBI_Year; i<=6; i--) and I'm guessing that you may need to convert a string to a number in there somewhere for the FOR loop to work correctly. I also think that a graphic rule can only return one item, but your rule is trying to return more than one image via the loop. It may be necessary to return the file names to an array by putting this code in an OnRecordStart callback rule and then pulling the values from the array in separate graphic rules applied to multiple graphic frames on your template. Link to comment Share on other sites More sharing options...
Hadrian Posted September 29, 2011 Author Share Posted September 29, 2011 even if i test with say for (i=11; i>6; i--) the file i actually have in existence (CPM2036-10.pdf) is not seen since the first version of the loop (CPM2036-11.pdf)errors out and does not verify the next file. Maybe i need a break or an array since they are fixed years. Just dont want to go in every year and add the current year.... Link to comment Share on other sites More sharing options...
esmith Posted September 29, 2011 Share Posted September 29, 2011 for (i=11; i>6; i--) This loop would never execute because the initial value (11) is already greater than the condition (6). And I still think a graphic rule can only return one image so putting a return inside a loop is a bad idea. Link to comment Share on other sites More sharing options...
Dan Korn Posted September 29, 2011 Share Posted September 29, 2011 This loop would never execute because the initial value (11) is already greater than the condition (6). No, that's not true. The final expression of the for statement is i--, which decrements the counter variable. This is a perfectly valid way to start with a larger number and count down to a smaller one. Most examples you'll see of a for loop probably happen to use an increment (++) statement to drive the iteration, but that's not a requirement by any means. And incrementing and decrementing aren't the only possibilities either; you can do just about anything you want in that final statement to drive the loop. And I still think a graphic rule can only return one image so putting a return inside a loop is a bad idea. Again, I disagree. The rule is looping until it finds something it can use, and then returns it. That's perfectly valid as well. EDIT: It's valid, unless you're unconditionally returning from the loop, as the original post does. See my next post. Link to comment Share on other sites More sharing options...
Dan Korn Posted September 29, 2011 Share Posted September 29, 2011 Again, I disagree. The rule is looping until it finds something it can use, and then returns it. That's perfectly valid as well. Actually, I take that back. Well, partially. It's fine to conditionally return from a loop, and keep looping otherwise, but the rule as written always returns in the first iteration, whether it finds the first graphic or not. You don't want to give up and return NullResource() until you've tried all the possibilities. So, I think what you want to do is delete that last "else" and move the "return NullResource();" statement to after the final closing brace "}" of the for loop. Also, it's great that you wrote your own zeroPad function, but the built-in FormatNumber function can do that for you. Finally, if you want to test for the existence of a resource, you need to set the last parameter of CreateResource() to true, not false. So the loop could look like this: for (var i = Right(GetYear(Today()), 2); i >= 7; i--) { var lessyear = "-" + FormatNumber("00", i); var path = ImagePath + poster + lessyear + ".pdf"; Print("Trying path: " + path); var r = CreateResource(path, "graphic", true); if (r.exists) return r; } return NullResource();Now, I don't know whether the rest of your code to parse out the field value and actually find the graphic at the path you've built up is correct or not, since I have neither the data nor the output files and their locations, but the Print statement above might help with that. Look in the log (.msg) file for the paths it's looking for. Link to comment Share on other sites More sharing options...
Hadrian Posted September 30, 2011 Author Share Posted September 30, 2011 Thanks for the tips Eric and Dan. Your code worked Dan. Gives me a better perspective on what and how i can execute these types of references as i need to overhaul our dedicated environment js. Much appreciated guys. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.