Adding data to forms with code

I have a website with a dynamic page on a form. I’m using code to add a “default”/backup string to the message field to help keep track of which page a form was submitted from. It works most of the time, but sometimes the message field comes through completely blank, and I can’t figure out why.

$w.onReady(function () {
    var dataset = $w("#dynamicDataset");
    var currentItem = dataset.getCurrentItem();
    $w("#textBox1").placeholder = "Submit for quote on " + currentItem.combined;
    $w("#textBox1").value = "Submit for quote on " + currentItem.combined;
    $w("#textBox1").onBlur(() =>{
        var textBox = $w("#textBox1");
        if(!textBox.value.includes(currentItem.partNumber)) textBox.value += "\n\n(P/N " + currentItem.partNumber + ")";
    })
});

export function button17_click(event) {
    var textBox = $w("#textBox1");

    var dataset = $w("#dynamicDataset");

    if (textBox.value === "" || textBox.value === "-") textBox.value = "Submit for quote on " + dataset.getCurrentItem().combined;
    else textBox.value += "\n\n(P/N " + dataset.getCurrentItem().partNumber + ")";

}

It is possible currentItem is attempting to get the data before the dataset is ready.

To mitigate this, you can wrap the function with a dataset.onReady() within the page’s onReady() handler.

Example:

$w.onReady(() => {
  $w("#myDataset").onReady(() => {
    //Do something with dataset
  });
});

Learn more through the following:

Dynamic Dataset onReady
Dynamic Dataset getCurrentItem

It’s been about a week since I implemented this, and I don’t think I’ve seen a single instance of the issue since then. I think I can consider this fixed. Thanks!