Condition Email with or without attachment

So Mohamed:

First of all the upload button you have is called $w(‘#FileAttachment’) - which you are using in your code.

Now is your assertion that because you have connected your dataset to the $w(‘#FileAttachment’) upload button somehow then the current item from your data set will be dynamically updated whenever you make a file selection using the upload button?

If so I think that is probably a bad assumption. If you want to guarantee that the current item has been updated with an element value you need to save the item first which will have the effect of wix-dataset reading the element values and updating the data collection and the dataset data copy.

What you should be doing is using the value of the element directly then you will get the effect you are looking for.

So NOT this

if ($w("#FileAttachment").value !== 0) {
    const matches = item.fileAttachment.match(convertRegex);
    documentUrl = `docs.wixstatic.com/ugd/${matches[1]}?dn=${matches[2]}`;
}

You should be doing this:

if ($w("#FileAttachment").value.length > 0) {
    // The upload button returns an array and so could return 
    // multiple files selections but we will use the first one
    // Note: you might want to add a lightbox dialogue to alert 
    // the user if they select more than one file which one they 
    // are going to be sending
    const fileName = $w("#FileAttachment").value[0];
    const matches = fileName.match(convertRegex);
    documentUrl = `docs.wixstatic.com/ugd/${matches[1]}?dn=${matches[2]}`;
}

Think of the dataset as read only, only containing information that is in the data collection. So if you haven’t put something in the data collection via a dataset save() or new() then if the data in any bound element changes these changes probably won’t be in your copy of the current item.

Make sense?
Steve