MarcomCentral (PTI) and FusionPro User Communities

MarcomCentral (PTI) and FusionPro User Communities (http://forums.pti.com/index.php)
-   The JavaScript Library (http://forums.pti.com/forumdisplay.php?f=23)
-   -   Switching pages with on record start (http://forums.pti.com/showthread.php?t=5140)

gdellaiera May 22nd, 2018 11:40 AM

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;


}

scotts May 22nd, 2018 02:50 PM

Re: Switching pages with on record start
 
Quote:

Originally Posted by gdellaiera (Post 21178)
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.

gdellaiera May 22nd, 2018 04:40 PM

Re: Switching pages with on record start
 
Thanks for the look. That makes a lot of sense. I will give it a try!

step May 23rd, 2018 06:37 AM

Re: Switching pages with on record start
 
Quote:

Originally Posted by scotts (Post 21179)
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 (Post 21179)
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);


gdellaiera May 23rd, 2018 07:29 AM

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?

gdellaiera May 23rd, 2018 08:55 AM

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.

step May 23rd, 2018 07:03 PM

Re: Switching pages with on record start
 
Quote:

Originally Posted by gdellaiera (Post 21182)
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 (Post 21182)
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 (Post 21182)
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.

gdellaiera May 24th, 2018 06:50 AM

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!

gdellaiera May 25th, 2018 10:24 AM

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';

step May 29th, 2018 06:24 AM

Re: Switching pages with on record start
 
Quote:

Originally Posted by gdellaiera (Post 21192)
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 (Post 21192)
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.


All times are GMT -7. The time now is 09:04 AM.

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