Skip to main content
Participating Frequently
February 21, 2024
Answered

JavaScript error with displaying previously stored data

  • February 21, 2024
  • 1 reply
  • 2400 views

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! 

This topic has been closed for replies.
Correct answer Thom Parker

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.  


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.

 

1 reply

Bernd Alheit
Community Expert
Community Expert
February 21, 2024
"DefectID"+index = item.RefID;

You can't create variables this way.

CT1119Author
Participating Frequently
February 22, 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;
});
Nesa Nurani
Community Expert
Community Expert
February 22, 2024

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