Condition Email with or without attachment

OK I have a couple more suggestions.

  1. Sprinkle some console.log statements around your code to check on the value of key variables etc. Just to make sure the values you are expecting are actually being set correctly.
    So add a console.log($w(“#FileAttachment”)); just before you test for the value (this will show you the whole element object and its contents - you could also use this console.log($w(“#FileAttachment”).value);).
    Add console.log(matches); immediately before you assign to documentUrl and add console.log(documentUrl); after you assign to documentUrl.
    This will show you what you are generating as the code executes. Of course you can add other log statements but this will help you narrow down the problem.
  2. Change the code you have where you are attaching the file. Basically if there is no file to attach then just don’t attach one. So instead of this
const body = `Name: ${$w("#CustomerName").value}
  \rEmail: ${$w("#CustomerEmail").value}
  \rPhone Number: ${$w("#CustomerPhoneNumber").value}
  \rEnquiry: ${$w("#CustomerEnquiry").value}
  \rFile: ${documentUrl}`;

do this

let body = `Name: ${$w("#CustomerName").value}
  \rEmail: ${$w("#CustomerEmail").value}
  \rPhone Number: ${$w("#CustomerPhoneNumber").value}
  \rEnquiry: ${$w("#CustomerEnquiry").value}`;
  if (documentUrl) {
    body += `\rFile: ${documentUrl}`;
  }

This will prevent null being used in the body string which is likely where one of your problem is coming from.

Share the output if you are still having problems