Go Back   MarcomCentral (PTI) and FusionPro User Communities > Software-Related Talk > The JavaScript Library

Notices

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old May 22nd, 2018, 11:40 AM
gdellaiera gdellaiera is offline
Regular Contributor
 
Join Date: Nov 2016
Posts: 78
Default Switching pages with on record start

I am trying to create a banner with a few fields and some size changes based upon one of the fields.

I have 3 sizes for banners (18x12, 60x30, 72x36) based upon the number of years selected
1-5 = 18x12
10-15 = 18x12
20-25 = 60x30
30+ = 72x36

With 3 layouts for the background
Style 1
Style 2
Style 3

The field for "Years of service" is entered in manually while the background is based on a pick list of the 3 styles.

I believe that OnRecordStart would be my best bet for this, but I'm not getting the result I was hoping for.

I have set all pages to "Body, unused" and then I get no records to show. Originally I had all pages set to "Body" and each page displayed, which was not what I was looking for either.

I couldn't find anything that was similar enough for me to build off of on the forum yet, so I decided to post my javascript here and maybe someone would be able to find out what I am missing.

Thanks for the look and any and all help!


var Background = Field("Background");

if (Field("Years of Service") >=1 && Field("Years of Service")<= 5) + Field("Background") == "Style 1";
return ("Style 1 18x12");


if (Field("Years of Service") >=10 && Field("Years of Service")<= 15) + Field("Background") == "Style 1";
return ("Style 1 18x12");


if (Field("Years of Service") >=20 && Field("Years of Service")<= 25) + Field("Background") == "Style 1";
return ("Style 1 60x30");


if (Field("Years of Service") >=30 + (Field("Background") == "Style 1"));
return ("Style 1 72x36");


if (Field("Years of Service") >=1 && Field("Years of Service")<= 5) + Field("Background") == "Style 2";
return ("Style 1 18x12");


if (Field("Years of Service") >=10 && Field("Years of Service")<= 15) + Field("Background") == "Style 2";
return ("Style 2 18x12");


if (Field("Years of Service") >=20 && Field("Years of Service")<= 25) + Field("Background") == "Style 2";
return ("Style 2 60x30");


if (Field("Years of Service") >=30 + (Field("Background") == "Style 2"));
return ("Style 2 72x36");


if (Field("Years of Service") >=1 && Field("Years of Service")<= 5) + Field("Background") == "Style 3";
return ("Style 3 18x12");


if (Field("Years of Service") >=10 && Field("Years of Service")<= 15) + Field("Background") == "Style 3";
return ("Style 3 18x12");


if (Field("Years of Service") >=20 && Field("Years of Service")<= 25) + Field("Background") == "Style 3";
return ("Style 3 60x30");


if (Field("Years of Service") >=30 + (Field("Background") == "Style 3"));
return ("Style 3 72x36");



switch(Background) {

case "Style 1 18x12":
FusionPro.Composition.SetBodyPageUsage("Style 1 18x12",true);
FusionPro.Composition.SetBodyPageUsage("Style 1 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 1 72x36",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 72x36",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 72x36",false);
break;

case "Style 1 60x30":
FusionPro.Composition.SetBodyPageUsage("Style 1 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 1 60x30",true);
FusionPro.Composition.SetBodyPageUsage("Style 1 72x36",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 72x36",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 72x36",false);
break;

case "Style 1 72x36":
FusionPro.Composition.SetBodyPageUsage("Style 1 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 1 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 1 72x36",true);
FusionPro.Composition.SetBodyPageUsage("Style 2 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 72x36",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 72x36",false);
break;

case "Style 2 18x12":
FusionPro.Composition.SetBodyPageUsage("Style 1 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 1 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 1 72x36",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 18x12",true);
FusionPro.Composition.SetBodyPageUsage("Style 2 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 72x36",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 72x36",false);
break;

case "Style 2 60x30":
FusionPro.Composition.SetBodyPageUsage("Style 1 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 1 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 1 72x36",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 60x30",true);
FusionPro.Composition.SetBodyPageUsage("Style 2 72x36",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 72x36",false);
break;

case "Style 2 72x36":
FusionPro.Composition.SetBodyPageUsage("Style 1 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 1 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 1 72x36",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 72x36",true);
FusionPro.Composition.SetBodyPageUsage("Style 3 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 72x36",false);
break;

case "Style 3 18x12":
FusionPro.Composition.SetBodyPageUsage("Style 1 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 1 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 1 72x36",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 72x36",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 18x12",true);
FusionPro.Composition.SetBodyPageUsage("Style 3 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 72x36",false);
break;

case "Style 3 60x30":
FusionPro.Composition.SetBodyPageUsage("Style 1 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 1 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 1 72x36",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 72x36",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 60x30",true);
FusionPro.Composition.SetBodyPageUsage("Style 3 72x36",false);
break;

case "Style 3 72x36":
FusionPro.Composition.SetBodyPageUsage("Style 1 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 1 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 1 72x36",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 2 72x36",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 18x12",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 60x30",false);
FusionPro.Composition.SetBodyPageUsage("Style 3 72x36",true);
break;


}
Reply With Quote
  #2  
Old May 22nd, 2018, 02:50 PM
scotts scotts is offline
Junior Community Member
 
Join Date: Feb 2011
Location: Zeeland, MI, USA
Posts: 41
Default Re: Switching pages with on record start

Quote:
Originally Posted by gdellaiera View Post
I am trying to create a banner with a few fields and some size changes based upon one of the fields.


var Background = Field("Background");

if (Field("Years of Service") >=1 && Field("Years of Service")<= 5) + Field("Background") == "Style 1";
return ("Style 1 18x12");

switch(Background) {


case "Style 1 18x12":

}

Just looking at this part, your variable "Background" is always going to equal the Field("Background"), so the switch will never work. Let's say Field("Background") is equal to "18x12", and you are trying to make the variable to equal the return of your 'if' statement, correct.



You could change your variable to be...
Code:
var Style1A = "Style 1 18x12";
var Style1B = "Style 1 18x12";
And your return to be...
Code:
if (Field("Years of Service") >=1 && Field("Years of Service")<= 5) + Field("Background") == "Style 1";
    return Style1A;

if (Field("Years of Service") >=10 && Field("Years of Service")<= 15) + Field("Background") == "Style 1";
    return Style1B;
And your SetBodyPageUsage to be...without the switch
Code:
case "Style1A":
        FusionPro.Composition.SetBodyPageUsage("Style 1 18x12",true);
        FusionPro.Composition.SetBodyPageUsage("Style 1 60x30",false);
        FusionPro.Composition.SetBodyPageUsage("Style 1 72x36",false);
        FusionPro.Composition.SetBodyPageUsage("Style 2 18x12",false);
        FusionPro.Composition.SetBodyPageUsage("Style 2 60x30",false);
        FusionPro.Composition.SetBodyPageUsage("Style 2 72x36",false);
        FusionPro.Composition.SetBodyPageUsage("Style 3 18x12",false);
        FusionPro.Composition.SetBodyPageUsage("Style 3 60x30",false);
        FusionPro.Composition.SetBodyPageUsage("Style 3 72x36",false);
Hope this helps.
Reply With Quote
  #3  
Old May 22nd, 2018, 04:40 PM
gdellaiera gdellaiera is offline
Regular Contributor
 
Join Date: Nov 2016
Posts: 78
Default Re: Switching pages with on record start

Thanks for the look. That makes a lot of sense. I will give it a try!
Reply With Quote
  #4  
Old May 23rd, 2018, 06:37 AM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 955
Default Re: Switching pages with on record start

Quote:
Originally Posted by scotts View Post
You could change your variable to be...
Code:
var Style1A = "Style 1 18x12";
var Style1B = "Style 1 18x12";
Both of those variables are set to the same value. Is that intentional?
Quote:
Originally Posted by scotts View Post
And your return to be...
Code:
if (Field("Years of Service") >=1 && Field("Years of Service")<= 5) + Field("Background") == "Style 1";
    return Style1A;

if (Field("Years of Service") >=10 && Field("Years of Service")<= 15) + Field("Background") == "Style 1";
    return Style1B;
I don't think that will work. If I'm understanding correctly, Gdellaiera is trying to determine which page to use here based on the "Years of Service" field and appending the style to the end of the string. So the correct syntax would be:
Code:
/**
 * When the vale of the "Years of Service" field is between 10-15, the
 * page size should be 18x12 regardless of the style selected in the
 * "Background" field. So set the `page` variable to the appropriate size
 * and append the style to the end to dynamically cover all three styles. 
 */
if (Field("Years of Service") >=10 && Field("Years of Service")<= 15) {
  var page = "18x12 " + Field("Background");
}
I think a better way to accomplish this task would be:
Code:
// Get the "Years of Service" value and cast it as an integer for comparisons.
// Default the value to `1` when the field is empty.
var yearsOfService = Int(Field("Years of Service")) || 1;
// Get the style value from the "Background" field.
// Default to `Style 1`.
var background = Field("Background") || 'Style 1';
// Use a switch statement to determine which page size we should be using. We'll
// set the `size` variable to the first expression that evaluates to `true`.
switch (true) {
  // Check to see if the `yearsOfService` is 15 or less. If it is, we can set the
  // size to 18x12 and none of the other expressions in this `switch` will be evaluated.
  case yearsOfService <= 15: size = '18x12'; break;
  // If the `yearsOfService` is more than 15, we will check to see if the
  // `yearsOfService` field is 25 or less. If it is, set the size to 60x30.
  case yearsOfService <= 25: size = '60x30'; break;
  // If neither of the two previous cases were true, that means the `yearsOfService`
  // is greater than 25, so we can set the size to 72x36.
  default: size = '72x36';
}
// Since you've already set all of the body pages to `false`, you only need to
// enable the correct page.
FusionPro.Composition.SetBodyPageUsage(background + " " + size, true);
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
  #5  
Old May 23rd, 2018, 07:29 AM
gdellaiera gdellaiera is offline
Regular Contributor
 
Join Date: Nov 2016
Posts: 78
Default Re: Switching pages with on record start

Ste Pennell, you are almost correct with the syntax. I don't just have 3 pages of the specific sizes (18x12, 60x30 & 72x36), but 9 pages, one for each size and style due to text frames being completely different throughout.

The years will only be entered in values between 1-5, 10-15, 20-25 or 30 +, none of the in-between values.

Each year range determines a size for the banner and adding the style from a drop down list would determine which page of the template to be used.

Does this make more sense?
Reply With Quote
  #6  
Old May 23rd, 2018, 08:55 AM
gdellaiera gdellaiera is offline
Regular Contributor
 
Join Date: Nov 2016
Posts: 78
Default Re: Switching pages with on record start

Scotts, for the most part this script works except once I remove the switch, I get a syntax error on the first 'case'. I'm trying to work through it now and will update with fix once it is working.
Reply With Quote
  #7  
Old May 23rd, 2018, 07:03 PM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 955
Default Re: Switching pages with on record start

Quote:
Originally Posted by gdellaiera View Post
I don't just have 3 pages of the specific sizes (18x12, 60x30 & 72x36), but 9 pages, one for each size and style due to text frames being completely different throughout.
Yes, I know. But you only have 3 size options. The switch statement determines which size to use. From there, the style is appended to the size to create the page name.

Quote:
Originally Posted by gdellaiera View Post
The years will only be entered in values between 1-5, 10-15, 20-25 or 30 +, none of the in-between values.
Are you saying that the value of the "Years of Service" field is a string value representing a range of numbers (like "1-5") or just that the user will never enter a number outside of those specified ranges (like 6)? The latter of which shouldn't make a difference and the code should still work. But, if the values are string ranges, you could modify the code like this:
Code:
var background = Field("Background") || 'Style 1';
switch (Field("Years of Service")) {
  case '1-5':
  case '10-15':
    size = '18x12';
    break;
  case '20-25':
    size = '60x30';
    break;
  default: size = '72x36';
}
FusionPro.Composition.SetBodyPageUsage(background + " " + size, true);
Quote:
Originally Posted by gdellaiera View Post
Each year range determines a size for the banner and adding the style from a drop down list would determine which page of the template to be used.

Does this make more sense?
Right, and again, that's what the code I posted is doing. It determines the size needed based on the "Years of Service" and adds the style selected from the drop down list to it.
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
  #8  
Old May 24th, 2018, 06:50 AM
gdellaiera gdellaiera is offline
Regular Contributor
 
Join Date: Nov 2016
Posts: 78
Default Re: Switching pages with on record start

Yes, the years will only be entered in those specific ranges (company policy for these items).

I've gone ahead and added this into the OnRecordStart and all records are composed on the 72x36 (default size) template, no matter what number of years you are putting in.

This is the first code that I have put in that actually produces 1 record instead of the 9 or 0 from before, and I do understand a little of what the code is doing. The styles are coming through just fine though.

I appreciate all the help on this. Albeit still a little confused, my knowledge base is growing on this. Hopefully I have this working today. Thanks again step!

Last edited by gdellaiera; May 24th, 2018 at 08:46 AM..
Reply With Quote
  #9  
Old May 25th, 2018, 10:24 AM
gdellaiera gdellaiera is offline
Regular Contributor
 
Join Date: Nov 2016
Posts: 78
Default Re: Switching pages with on record start

Step,

I have one question: Why add a default size and no numerical case for the largest size? Everything else makes sense to me but this. And no matter what size I change to, the output will only remain that size when I run any record through it. Thanks for the explanation.

default: size = '72x36';
Reply With Quote
  #10  
Old May 29th, 2018, 06:24 AM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 955
Default Re: Switching pages with on record start

Quote:
Originally Posted by gdellaiera View Post
Why add a default size and no numerical case for the largest size? Everything else makes sense to me but this.
Because cases have been set for all other scenarios. If none of the cases evaluated to true, then it stands to reason that the only remaining option would be the 72x36 size. Check out this reference on switch statements to get a better understanding.

Quote:
Originally Posted by gdellaiera View Post
And no matter what size I change to, the output will only remain that size when I run any record through it.
That's leads me to believe that you haven't accurately described the values your "Years of Service" field contain. I can't really help you any further without seeing an example of your data/template.
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
Reply

Tags
fusion pro, javascript, onrecordstart

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 12:16 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) 2011, PTI Marketing Technologies™, Inc.