Skip to main content
Participant
September 16, 2021
Question

Auto Field Creation based on percentage

  • September 16, 2021
  • 1 reply
  • 870 views

Friends I need a help

 

I have a pdf from, in the form I have a Field for Owner name and another field for Percentage Owned. If the user put 33% in the form I want to have a new field auto created with another owner name and the Percentage until we reach 100%. I am so new to form and the Java Script. Any help will be greatly appresheated. 

This topic has been closed for replies.

1 reply

BarlaeDC
Community Expert
Community Expert
September 24, 2021

Hi,

 

This is not as easy as it would appear, if you have a number of fields that  you would like to show and hide then you could do it that way.

 

for example: you decide that 5 homeowners are enough so you could have:

HomeownerOne HomeownerOnePercentage Shown, once this is entered it would then show HomeownerTwo if the percentage was not 100%, but these fields would need to be created before and would have to have space.

 

We can help code this if it is a way you would like to go?

Participant
September 24, 2021

Sir, that would be awesome. Can you please help me code that?

BarlaeDC
Community Expert
Community Expert
September 27, 2021

Hi,

 

I am assuming you have a table link set up of fields:

 

HomeOwnerOne - HomeOwnerOnePercentage

HomeOwnerTwo - HomeOwnerTwoPercentage

etc....

 

Then you can put this code as a Document JavaScript

- Click search tools and enter "Javascript", and select the "Document JavaScript" one

- In the dialog that appears enter a name and click add

- then paste in the code below

 

// This checks we have at least 100%
// if you want it to be exactly 100% we will need to error on fields and that makes it much more complicated
function checkTotal( total){
    return total > 99;
}

// helper function that takes an array of fields, 
// makes them all hidden, this is to cover changing the values and hiding unneeded fields
function hideFields ( fields){
    showHideFields ( fields, display.hidden);
}

// helper function that takes an array of fields,
// makes them all shown.
function showFields ( fields){
   showHideFields ( fields, display.visible);
}

// helper function for the above two methods.
function showHideFields ( fields, showHide) {
    for ( var i = 0; i < fields.length; i++){
        fields[i].display = showHide;
    }
}

// Function that actually does the work
function checkPercentage( event)
{
    // set total to 0 to start, make sure we are always starting from the same point
    var total = 0;

    // Get all fields we need
    // This could be done as we need them but I find this cleaner
    // Feel free to change the name between the "", to your field names
    var Home1Percent = this.getField("HomeOne.percentage");
    var Home2Percent = this.getField("HomeTwo.percentage");
    var Home2Owner= this.getField("HomeTwo.owner");
    var Home3Percent = this.getField("HomeThree.percentage");
    var Home3Owner = this.getField("HomeThree.owner");
    var Home4Percent = this.getField("HomeFour.percentage");
    var Home4Owner = this.getField("HomeFour.owner");
    var Home5Percent = this.getField("HomeFive.percentage");
    var Home5Owner = this.getField("HomeFive.owner");

    // check first field and add it to the total
    // if the first field was the one changed use the event.value, quirk of Acrobat
    // otherwise use the value stored in the field
     total += (event.target.name == "HomeOne.percentage") ? Number(event.value) : Number(Home1Percent.value);
     if ( checkTotal (total)) {
         // if above 99% then hide all other fields
        hideFields ( [Home2Owner, Home2Percent, Home3Owner, Home3Percent, Home4Owner, Home4Percent, Home5Owner, Home5Percent]);
     } else if ( Home2Percent.display === display.hidden){
         // if not above 99% and the 2nd field is not showing, show it.
         showFields ( [Home2Owner, Home2Percent]);
         // exit to allow the user to update the fields.
         return;
     }

     // if we are visible, include in the count.
     if ( Home2Percent.display === display.visible){
         total += (event.target.name == "HomeTwo.percentage" ? Number( event.value) : Number ( Home2Percent.value));
        if ( checkTotal ( total)){
            hideFields ([ Home3Owner, Home3Percent, Home4Owner, Home4Percent, Home5Owner, Home5Percent]);
        } else if ( Home3Percent.display === display.hidden){
            showFields ( [ Home3Owner, Home3Percent]);
            return;
        }
     }
     if ( Home3Percent.display === display.visible){
         total += (event.target.name === "HomeThree.percentage" ? Number(event.value) : Number ( Home3Percent.value));
        if ( checkTotal ( total)){
            hideFields ([ Home4Owner, Home4Percent, Home5Owner, Home5Percent]);
        } else if ( Home4Percent.display === display.hidden){
            showFields ( [ Home4Owner, Home4Percent]);
            return;
        }
    }
    if ( Home4Percent.display === display.visible){
        total += (event.target.name === "HomeFour.percentage" ? Number(event.value) : Number ( Home4Percent.value));
        if ( checkTotal ( total)){
            hideFields ([  Home5Owner, Home5Percent]);
        } else if ( Home5Percent.display === display.hidden){
            showFields ( [ Home5Owner, Home5Percent]);
            return; 
        }
    }
     return;
}

The code is very verbose, hopefully that makes it easier to understand, and could be simplified later.

 

Once you have added that code, you need to go to each percentage field and open the properties.

- then select "Validate" tab

- Select " Run custom validation script"

- Enter "checkPercentage( event);"

- Click "OK"

 

Then it should work, if it doesn't be sure to check out the console for errors, and post them here.