Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

Livecycle 'for loop' needed to review numeric fields and pull top 2 or 3 highest and display corresponding information in a text field

Avatar

Level 1

Hi,

I have dynamic form that allows users to enter data into a number of tables compiled of numeric and text fields. I have a created a summary table to automatically summarize  (auto-populate) the information the user entered in the fields in the previous tables. Some of the items I want to put in my summary table include the Top 3 xxxx, meaning I want to use javascript or formcalc to create a loop that will refer to a set of fields, and identify which fields have the top 3 (highest 3 based on percentage values). Then, once the top 3 numeric field have been identified, i want to display in a text field, the text fields associated with the top 3 numeric fields.

My form is VERY long and complicated so I dont want to post it here, but imagine you have the following:

Car Crashes by speed limit:

Speed          Percentage of Crashes

20 mph          [user entered numeric field]

25 mph          [user entered numeric field]

30 mph          [user entered numeric field]

35 mph          [user entered numeric field]

40 mph          [user entered numeric field]

Where the speed limits (e.g., 20 mph, 30 mph) are text fields with those speeds provided, and the fields in the Percentage of Crashes column are numeric fields that the form user will fill out.

I will then have a summary table:

Top 3 speed limits for car crashes:      [auto-populated field that displays speed associated with highest percentage of crashes]

                                                             [auto-populated field that displays speed associated with 2nd highest percentage of crashes]

                                                             [auto-populated field that displays speed associated with highest percentage of crashes]

I am on a very short timeline, please let me know if you can help, and/or know if this is possible to do. I am brand new to for-loops in LiveCycle. Thanks!

13 Replies

Avatar

Level 10

Hi,

This sample might get you started,https://sites.google.com/site/livecycledesignercookbooks/home/belindaj57056943.pdf?attredirects=0&d=...

The JavsScript code is in the calculate event of the subform that contains the summary fields, called Summary in the sample, and looks like;

var result = [];

var rows = Table1.resolveNodes("Row1[*]");

for (var i = 0, limit = rows.length; i < limit; i++) {

    var row = rows.item(i);

    if (!row.Percentage.isNull)
    {

        result.push({"speed":row.Speed.rawValue, "Percentage":row.Percentage.rawValue});

    }

}

result.sort(function (left, right) {

  return right.Percentage - left.Percentage ;

});

First.rawValue = (result[0] != undefined) ? result[0].speed : "";

Second.rawValue = (result[1] != undefined) ? result[1].speed : "";

Third.rawValue = (result[2] != undefined) ? result[2].speed : "";

Obviously you will have to change the field names to suit what you have used, but hopefully will help.

Regards

Bruce

Avatar

Level 1

Thanks Bruce. I am trying to adapt what you sent me to my actual form. In my example, would the percentages to be pulled from the last column on the right-hand side of Table 2 in this example:  SpeedExample2.pdf - Google Drive can you explain how i would add a reference to that column using the code you provided? For example, what would I replace "percentage" and "speed" with? Thanks!

Avatar

Level 10

Hi, I am not sure how the percentage field is being set in your example, they seem to be static text fields, are you going to be using floating fields.  In my sample they were numeric fields.

Avatar

Level 1

Sorry, I just made a quick example to show you, I must have forgotten to edit the field. The percentage field will be a numeric field, but it will be read only, there will be javascript in the calculate event of the percentage fields that will automatically populate the field with the percentage based on the information entered by the user in previous form fields.

Avatar

Level 7

this is a very cool sample. Thank you!

Avatar

Level 10

I've updated the sample, so there is a numeric field for the percentage

var result = [];

var rows = Table2.resolveNodes('#subform.[assist.role == "TR"]');

for (var i = 0, limit = rows.length; i < limit; i++) {

    var row = rows.item(i);

    if (!row.PercentageFatalCrashes.isNull)

  {

        result.push({"speed":row.resolveNode'#draw').value.text.value,            

                     "Percentage":row.PercentageFatalCrashes.rawValue});

  }

}

// now sort descending order of percentage

result.sort(function (left, right) {

  return right.Percentage - left.Percentage ;

});

First.rawValue = (result[0] != undefined) ? result[0].speed : "";

Second.rawValue = (result[1] != undefined) ? result[1].speed : "";

Third.rawValue = (result[2] != undefined) ? result[2].speed: "";

The main differences to the early script is your form has different names for the rows of the table, so the resolveNode expression is a little different, assist.role == "TR" means get the table rows, not the headers and footers.  Also you are using a static text object for the speed, where I used a read only text field, so the reference is now .resolveNode('#draw').value.text.value.

I have called the percentage field, PercentageFatalCrashes, if you call it something else then remember to update the if statement in the code above as well as the result.push() that is lines 5 and 8.

Sample link is;

https://sites.google.com/site/livecycledesignercookbooks/home/SpeedExample2a.pdf?attredirects=0&d=1

Hope this helps

Bruce

Avatar

Level 7

this sample does not seem to be working?

Avatar

Level 10

Seems to work for me,  are you getting an error message?

Avatar

Level 7

No error but no results either.

Avatar

Level 10

With the values I hard coded I get the following result;\

Capture.PNG

I've tried Reader 9 and Reader DC, which version are you using.

Avatar

Level 7

I got it. What I was expecting is that as with the original sample the changes were going to be reflected in the first table. Once I changed the fields in the table from Read Only to User Entered Optional I could add my own values and have the last table update.

Thanks for the sample.

Avatar

Level 1

Thanks for sharing this, I am trying to apply it to my scenario and I cannot get it to work. In my scenario, the values in the Percentage Fatal Crashes field are each presented in a pre-existing table and the values are calculated based on information previously entered into the form. The values are displayed in percentage format (in case that matters) How would I change the code?https://drive.google.com/file/d/1fJLUNYnm4PvWOl18bJMpyOqpp_E_ZSB_/view?usp=sharing

Here is a link to a version of the form that I created to test the syntax.  SpeedExample3-19-2018.pdf - Google Drive
In my sample version, the user enters data into "Table 3" (Page 1) and "Table Crashes by Year" (Page 2) and "Table 32" (Page 3). The column "Percentage of pedestrian crashes that were KSI" is the equivalent of the column "Percentage Fatal Crashes"in the example Bruce provided.  Based on the information entered and calculated in the form, I want the text fields listed under "Top 3 Speeds" (Page 4) to automatically populate using something like the for loop syntax Bruce provided above. 

Any additional help would be much appreciated! Thanks!

Avatar

Level 1

I think my issue is in adapting this line of code: result.push({"speed":row.resolveNode('#draw').value.text.value

How can I adapt that command to review the speeds from the Posted Speed column in Table 32 of the example I just shared.