Populate fields with unique values from form entries

New Here ,
Feb 20, 2019

Copy link to clipboard

Copied

I have a form that users are to enter their initials or ID# after each step is complete. There can be several users completing this form leading to several unique entries. At the end of the form the users will print the document and physically sign their name next to their initials. I'm trying to figure out how to have the initials automatically populate next to a signature box.

I've created a field hierarchy called Init "Init.10, Init.11,... " to enter initials for each job step and fields at the end of the form to put the initials/ID#'s (see below).

I've been able to use the following code I found to iterate through the "Init" fields and display the values but it only displays the first value it sees. I'm thinking I need to have the values put into an array and put the first initial into the "Initial.A" field then use an if-else statement to check the contents of the array against "Initial.A" and if it matches go to the next element in the array and place that in "Initial.B" and repeat. If that's correct I'm having trouble seeing how to do that.

I played with pushing the aNames.value to the arr array and setting arr[0] equal to Initial.A but it didn't work. Any suggestions or guidance would be helpful.

Thanks,

Bill

// get the parent object

var oNames = this.getField("Init");

var arr = [];

// get array or terminal children

var aNames = oNames.getArray();

this.getField("Sig").display = display.hidden;

// walk the tree:

for(i in aNames) {

if(aNames[i].value != "") {

this.getField("Sig").display = display.visible;

this.getField("Initial.A").value = aNames[i].value;

}

}

TOPICS
Acrobat SDK and JavaScript

Views

243

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

1 Correct Answer

Adobe Community Professional , Feb 27, 2019
Thom Parker Adobe Community Professional , Feb 27, 2019
Ok, so let me paraphrase the goal here.  1)  Collect all the values from the "Init" fields.2)  Filter it to removed blank and non-unique entries.3)  Write these remaining entries into the "Initial" fields.So here's some code to do this.// #1 collect all values into an arrayvar aAllValues = this.getField("Init").getArray().map(function(a){return a.value});// #2 Filter to remove blank/non-unique entriesvar rgEmpty = /^\s*$/;var aUsed = [];  // This is for collecting the unique valuesfor(var i=0;i<...

Likes

Translate

Translate
Jump to answer Jump to answer
Adobe Community Professional ,
Feb 20, 2019

Copy link to clipboard

Copied

If the names all follow a pattern, which they do, then the technique is to extract the unique identifier of the "Init" element, and then use it to build the names of the other fields needed.

In this case the unique identifier is the last character of the name ("Init.A", "Init.B", etc)

var cID = aNames.name[aNames.name.length-1];

Then the matching "Sig" field is

this.getField("Sig." + cID);

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Bastaw AUTHOR
New Here ,
Feb 20, 2019

Copy link to clipboard

Copied

Thanks Thom, unfortunately I can't ensure that users will always put their in initials in an "A" or "B" field.

The fields i'm pulling initials from are labeled as below. The Init.JS100 or Init.JS110 indicate which job step (they increment in 10's so these are step 10 and 11). There could be initials "BB" in the Init.JS90 (step 9) & Init.JS100 (step 10) field and "TP" in the Init.JS110 (step 11) field. I'm trying to have the program look through all the Init fields and see there are two unique initials and place one in the Initial.A field and one in the Initial.B field

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Bastaw AUTHOR
New Here ,
Feb 27, 2019

Copy link to clipboard

Copied

I've had some luck plugging away at the approach above in the console. I was able to use the  getArray to put all of the "Init" field values into an array and then use an if statement to only print the values that were not blank.

I'm still having trouble getting only unique values in that array then assigning each of those unique values to one of the "Initial" fields. I've tried to use both filter() and include() but didn't get it working.

I decided to try a different approach, making a document level function and calling it in the On Blur action for each of the "Init" fields. My goal was to look at the field value and compare it to an array and only push that value if its not already in the array. When I put this in the PDF and test it in console by printing the array value I get undefined.

Am i heading in the right direction here?

Document level:

var arr = [];

function CheckInit(x,y)

{

  for(var i = 0; i < y.length; i++){

    if (x != y.value) {

        y.push(x);

    }

  }

}

On Blur:

CheckInit(event.value,arr);

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Feb 27, 2019

Copy link to clipboard

Copied

Ok, so let me paraphrase the goal here. 

1)  Collect all the values from the "Init" fields.

2)  Filter it to removed blank and non-unique entries.

3)  Write these remaining entries into the "Initial" fields.

So here's some code to do this.

// #1 collect all values into an array

var aAllValues = this.getField("Init").getArray().map(function(a){return a.value});

// #2 Filter to remove blank/non-unique entries

var rgEmpty = /^\s*$/;

var aUsed = [];  // This is for collecting the unique values

for(var i=0;i<aAllValues.length;i++)

{

     if((aAllValues !=null) && !rgEmpty.test(aAllValues) && !aUsed .some(function(a){return a==aAllValues}))

             aUsed.push(aAllValues);

}

    

// #3 put into fields - For this I'm renaming the fields with a number "Initial0", Initial1" etc

for(var i=0;i<aUsed.length;i++)

{

     if(i>3) break;

     this.getField("Initial" + i).value = aUsed;

}

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Bastaw AUTHOR LATEST
New Here ,
Mar 06, 2019

Copy link to clipboard

Copied

Thanks Thom, this worked great. I notice if a field in the document was updated the initials table would update with the new field value however if the value in the field was deleted and left blank the initial would stay. I added an else statement to the filter so that if an initial was removed the array would update and remove that array element.

// #1 collect all values into an array

var aAllValues = this.getField("Init").getArray().map(function(a){return a.value});

// #2 Filter to remove blank/non-unique entries

var rgEmpty = /^\s*$/;

var aUsed = [];  // This is for collecting the unique values

for(var i=0;i<aAllValues.length;i++)

{

   if((aAllValues[i] !=null) && !rgEmpty.test(aAllValues[i]) && !aUsed .some(function(a){return a==aAllValues[i]}))

   aUsed.push(aAllValues[i]);

   else if((aAllValues[i] ==null) || rgEmpty.test(aAllValues[i]) )

  {

   aUsed.splice(i,1);

  }

}


// #3 put into fields - For this I'm renaming the fields with a number "Initial0", Initial1" etc

   for(var i=0; i<aUsed.length; i++)

{

   if(i>5) break;

   this.getField("Initial." + i).value = aUsed[i];

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more