Making a Data Feature (field) value available to a script

Hi Community.

I am wondering if a value pulled into the Data Feature (field) of List Detail block be made available to the javascript that I put into the custom code block on the same page?

If so, can someone suggest a code snipped that would demonstrate how that value can be referenced in by section?

Thank you
Boris

Quick answer: look in window.records

Let me know if you need more

1 Like

Thank you @dcoletta.
Really struggling here, not being a javascript developer. :slight_smile:

Do you mind providing a small code snippet example?

Thank you!
Boris

Sure. Suppose you have a page with a List Details block on it, and suppose that the record that the List Details block is displaying has a field called Name whose value is David.

You can write the following in JavaScript to fetch that value out:

<script>
console.log(window.records[keys(window.records)[0]].record.fields.Name);
</script>

Will result in David being printed to the console.

Does that make sense?

Yes. Wonderful! Thank you so much.
I’ll try it and report back on this thread.

Boris

1 Like

Thanks @dcoletta I have tried this myself but without success, and I wonder why?

I have a List Block with a text field displaying FirstName, followed by a code block with:

<script>
console.log(window.records[keys(window.records)[0]].record.fields.FirstName);
</script>

Nothing appears in the code block. Your help would be appreciated, with much thanks.

That code would never have worked. Some rando must have written it and slipped it into my studio when I wasn’t looking. :slight_smile:

Try pasting this code into a custom code block on a page with a List Details block:

<div id="firstName"/>
<script>
    const wait = setInterval(function () {
        if (window.records !== undefined) {
            clearInterval(wait);
            document.getElementById("firstName").innerText = window.records[Object.keys(window.records)[0]].record.fields.firstName;
        }
    },100)
</script>

I get those randos visiting too, making a real mess of my things …

So, thanks David, I’ve done the above … I have the List Details block showing FirstName as expected, but in the Code Block it displays undefined.

Unless there is a quick fix you can see, please don’t spend much time on this.