Record index '1' is out of range / cannot read property 'fields' from undefined

I’m attempting to reference the same text value (in this instance @dealerNameOne@, the field is from a detail table) twice on the same page.

When I do it just once, it will find and replace just fine. When I add the second, it throws two errors:
Record index ‘1’ is out of range / cannot read property ‘fields’ from undefined

I tried searching through the documentation, but perhaps I overlooked something. I’m just trying to understand this error and how to best fix, as I’ll be referencing the same field in my document multiple times. Thanks in advance,

I did find a work around by applying an ID to each one, and creating a duplicate script which references those specific IDs. However, if I can gain some understanding about what’s going on in the background that makes it unable to find and replace multiple text values, that would be very appreciated!

Also, is there a way to have a single script reference multiple IDs/classes conditionally?

It sounds like you may have just had something wrong with your loop. It was going out of bounds because it was trying to iterate past the last index of your detail table.

This is just from the drag and drop functionality. All I’m doing is dragging and dropping the field from the data model on the right pane, into my designer. However, unless I give it an ID, and adjust the script accordingly it won’t locate all instances of @sometext@. I have to give @sometext@ an identifier each time.

Is this always the case? Pardon my ignorance, just getting used to this product. :slight_smile:

Since you’re dragging and dropping out of a detail table, it’s kind of normal. The default script assumes that you’re attempting to show the next iteration from the detail table. A standard use for detail records would be something like an invoice where you’re going to be displaying a variable number of lines of an invoice. So the software works under the assumption that anything in a detail table is meant to be iterated through.

If you had 2 records in your detail table, for example, the script would not have produced an error, but still wouldn’t have given you the result you’re after. You’d have had the entry from detail 1 in the first and detail 2 in the second.

Here’s the script that produces:

results.each(function(index) {
var field, result = “”;
field = record.tables[“detail”][index].fields[“Description”];
if (field !== “”) result += field;
this.html(result);
});

Results is equal to the text you’re searching for. So this script is going to go through each result it finds. If there are 20 results, it’s looping 20 times. For each one it sets an index and then uses that index to retrieve a value from the detail table. For every instance it finds on the page, it will attempt to insert a new value from the next detail record.

Now here’s the standard script for something that is not in a detail table.

var field, result = “”;

field = record.fields[“DueDate”];
if (field !== “”) result += field;

results.html(result);

Note that it doesn’t loop through the data. That whole results.each structure is missing. It instead applies the exact same fixed field to every instance of results that it finds.

So an easier way of accomplishing what you’re attempting to do would be to modify the default script to look more like the second one.

var field, result = “”;

field = record.tables[“detail”][0].fields[“Description”];
if (field !== “”) result += field;

results.html(result);

Here we’ve removed the loop entirely. We’re just calling exactly the one field that we want and we’re displaying it on the page. It’s going to show the same value in every instance of the text selector that it finds.

This may not be the ‘right’ way for what your attempting either, however. Ultimately, you’ve got data in a detail table. Wouldn’t you need to be able to display all of the records in that detail at some point? If so, are you going to be manually placing items on the page for each one? What happens if the number of detail records becomes variable? You might need to reconsider how you’re going about the placement of this data.

Thank you so much, this is exactly what I i was looking for! I think you’re right, and since my knowledge of the datamapper is somewhat limited, I’m working with what I know – so far. I appreciate you taking the time to write out this response.