Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
15

JavaScript error with displaying previously stored data

Community Beginner ,
Feb 21, 2024 Feb 21, 2024

I have two tables.  The first table has form ields like ID, Result (option from a drop down), Details (open text field), Score (number).  Based on an answer ("N/C") selected in Result and that row's Score, I want the 2nd table to sort descending and display the ID (into DefectID) and Details (into DefectDetails) (in respective read-only form fields) from each row that had "N/C" selected in Result. 

 

I have checked my field names and all numbering starts at 0.  I think I am close, but I get errors when trying to figure out how to call previously stored information.  It's throwing errors on the forEach loop.  Most recent error is ReferenceError: invalid assignment left-hand side.  Here's what I have...

var defectObjs = []; // empty array to store details 
for (i = 1; i < 6; i++) {
if (this.getField(Result).value == "N / C") { // checking if n/c
defectObjs.push({
RefID: "RefID" + i});
({   Details: "Details" + i});
({ Score: "Score" + i});
};

// Sort defectObjs 
defectObjs.sort((x, y) => {
if (x.Score > y.Score)
return 1;
else if (y.Score > x.Score)
return -1;
else
return 0;
});

// forEach on defectObjs that starts counting at 0. 
defectObjs.forEach((item, index) => {
"DefectID"+index = item.RefID;
"DefectDetails"+index = item.Details;
});

 Thanks in advance for any wisdom you can provide! 

TOPICS
JavaScript , PDF , PDF forms
1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Feb 22, 2024 Feb 22, 2024

You're script has some complexity to it. So you'll need to do some debug to figure out what's going on. 

There's no obvious code errors. The first loop builds the "defectObjs" array from elements where the "ResultX" field contains "N/C".

So that's the first thing to check. Run the intial loop in the console window. Then look at whats returned to the "defectObjs" array.

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 21, 2024 Feb 21, 2024
"DefectID"+index = item.RefID;

You can't create variables this way.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 21, 2024 Feb 21, 2024

I updated a few items.  Now I don't get any errors, but when I activate the button the JS is running from nothing happens - nothing appears in my 2nd table.  

 

var defectObjs = []; // empty array to store details about defects
for (i = 1; i < 5; i++) {
	if (this.getField(Result).value == "N / C") { // checking if n/c
		defectObjs.push({
			RefID: "AltRefID" + i,
			Details: "Details" + i,
			Score: "Score" + i
			// Set any additional fields here. JSON objects can be ANYthing
		});
}}

// Sort defectObjs here
defectObjs.sort((x, y) => {
	if (x.Score > y.Score)
		return 1;
	else if (y.Score > x.Score)
		return -1;
	else
		return 0;
});

// forEach on defectObjs that starts counting at 0. 
defectObjs.forEach((item, index) => {
	getField("DefectID"+index).value = item.RefID;
	getField("DefectDetails"+index).value = item.Details;
});
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 21, 2024 Feb 21, 2024

If  Result is a field name, then you are missing quotes.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 22, 2024 Feb 22, 2024

Ar the end of the script add this:

console.show();
console.println(defectObjs.length);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 22, 2024 Feb 22, 2024

Thanks for a reply!  I did try adding quotes around Result (yes that is the field name).  And added the console lines but no luck yet.

 

I found another post that seems a bit similar to my quest.  They use checkboxes instead of text fields.  But I am trying to study it and see if it is doing something that I am not currently doing.  

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 22, 2024 Feb 22, 2024

I'm even closer now!  I was missing declaring a variable in the first section.  

Now it is putting text in the 2nd table.  Except the text is the field names instead of the value of the fields

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 22, 2024 Feb 22, 2024

Post the full code you have now.

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 22, 2024 Feb 22, 2024

This code is displaying the field names (e.g., AltRefID4, Details4) in the 2nd table.  I've tried a few things unsuccessfully to get it instead to display the text that is in those fields.  I tried adding getField and .value around the items identified in the push.  Just like the "var result...", I also tried adding vars for the items in the push under the "var result" line.  

var defectObjs = []; // empty array to store details 
for (i = 0; i < 5; i++) {
	var result = "Result" + i;
	if (this.getField(result).value == "N / C") { // checking if n/c
		defectObjs.push({
			RefID: "AltRefID" + i,
			Details: "Details" + i,
			Score: "Score" + i
		});
}}

// Sort defectObjs here
defectObjs.sort((x, y) => {
	if (x.Score > y.Score)
		return -1;
	else if (y.Score > x.Score)
		return 1;
	else
		return 0;
});

//forEach on defectObjs that starts counting at 0.
defectObjs.forEach((item, index) => {
	getField("DefectID" + index).value = item.RefID;
	getField("DefectDetails" + index).value = item.Details;
});

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 22, 2024 Feb 22, 2024

"I tried adding getField and .value around the items identified in the push"

This is correct.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 22, 2024 Feb 22, 2024

When I do the following, nothing appears in the 2nd table (not even the field names anymore).  I have tried this.getField too with no luck. 

var defectObjs = []; // empty array to store details 
for (i = 0; i < 5; i++) {
	var result = "Result" + i;
	if (this.getField(result).value == "N / C") { // checking if n/c
		defectObjs.push({
			RefID: getField("AltRefID" + i).value,
			Details: getField("Details" + i).value,
			Score: getField("Score" + i).value
		});
}}

// Sort defectObjs here
defectObjs.sort((x, y) => {
	if (x.Score > y.Score)
		return -1;
	else if (y.Score > x.Score)
		return 1;
	else
		return 0;
});

//forEach on defectObjs that starts counting at 0.
defectObjs.forEach((item, index) => {
	getField("DefectID" + index).value = item.RefID;
	getField("DefectDetails" + index).value = item.Details;
});

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 22, 2024 Feb 22, 2024

Are any errors reported in the console window?

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 22, 2024 Feb 22, 2024

Nope.  It let's me click OK and it goes away.  Then I go and click the button that I have it on.  I have even made sure I "Clear Form" and re-enter values and then click the button.  

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 22, 2024 Feb 22, 2024

You're script has some complexity to it. So you'll need to do some debug to figure out what's going on. 

There's no obvious code errors. The first loop builds the "defectObjs" array from elements where the "ResultX" field contains "N/C".

So that's the first thing to check. Run the intial loop in the console window. Then look at whats returned to the "defectObjs" array.

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 22, 2024 Feb 22, 2024
LATEST

Thanks for everyone's helpful responses!  I got it working.  It was a stupid human error with the fields on the page.  🙂

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