0

Trying to e-mail with record in PDF attached

Hi all!

Is it me or is mailing with attachments (record in PDF format) still not functioning properly? I have read many threads on this issue and as long as I am working with Ninox I still haven't figured out what the problem (sometimes) seems to be...

In my table Invoices, I have a button to e-mail the invoice. The button coded as usual (printAndSaveRecord & importFile), attachment is file(this, xPdf, xFile) and then upon pressing the button, the error appears that Ninox cannot find the PDF. Now, I have read the threads and it seems that Ninox cannot find the file as it has not yet been generated before and/or uploaded into the cloud. However, this error also occurs when I am working in browser window. Second time I try to e-mail the file, it works.

I have tried to fix this with the "do as server" command, but this makes no difference.

I have tried to put some other code between printAndSaveRecord and the attachment command in the e-mail 'module' of the code in order to give Ninox some extra time. But also this makes no difference.

Then it gets even crazier: in another Table called Shipments, I use the exact same code, the e-mail is sent, but no attachment is actually attached...

Logic dictates it must be me doing something wrong, but I cannot seem to figure out what it is. Only thing I can think of is that in the Shipment table the e-mail is sent upon a 'trigger after update' and in the Invoice table the e-mail is sent upon clicking a button. Could this be why the PDF is not attached? And if so; it makes no sense to me why not.

In general: is anyone else also experiencing these issues? How can they be fixed? I have tried generating a PDF upon creating the record (trigger on new record in the table), but this also does not work.

6 replies

null
    • Mel_Charles
    • 2 yrs ago
    • Reported - view

    I'm using several buttons with various send email+ attachment (cloud version) which is used some 10 times day and apart from when setting up and testing (approx 9 months ago) I have never had any issues.

    can I suggestion you upload your script and we can try and replicate it?

    • Martijn_Burghoorn
    • 2 yrs ago
    • Reported - view

    Hi Mel,

    My coded button in the Invoices table is:

     

    if 'INVOICE TO'.'RELATION TYPE' = 1 then
    let x := Id;
    let xCust := 'INVOICE TO';
    let xPDF := printAndSaveRecord(this, "INVOICE");
    let xFile := 'INVOICE NUMBER' + ".pdf";
    importFile(this, xPDF, xFile);
    for a in select CONTACTS where RELATIONS = xCust and STATUS = 1 and 'E-MAIL INVOICES' = true do
    sendEmail({
    from: userEmail(),
    to: a.'E-MAIL',
    subject: 'INVOICE NUMBER',
    text: " ",
    attachments: file(this, xFile),
    html: "Dear " + a.TITLE + " " + a.'LAST NAME' + ",<br><br>Please find attached invoice " + ".<br><br>Thank you and best regards,<br><br>" + user() + "<br><br><b><u>ATTACHMENTS:</u></b><ul><li>" + 'INVOICE NUMBER' + "</li></ul>"
    })
    end;
    STATUS := 3;
    SENT := true
    else
    if 'INVOICE TO'.'RELATION TYPE' = 2 then
    let x := Id;
    let xCust := 'INVOICE TO';
    let a := (select RELATIONS where Id = xCust and 'E-MAIL INVOICES' = true);
    let xPDF := printAndSaveRecord(this, "INVOICE");
    let xFile := 'INVOICE NUMBER' + ".pdf";
    importFile(this, xPDF, xFile);
    sendEmail({
    from: userEmail(),
    to: a.'GENERAL E-MAIL',
    subject: 'INVOICE NUMBER',
    text: " ",
    attachments: file(this, xFile),
    html: "Dear " + a.TITLE + " " + a.'LAST NAME' + ",<br><br>Please find attached invoice " + ".<br><br>Thank you and best regards,<br><br>" + user() + "<br><br><b><u>ATTACHMENTS:</u></b><ul><li>" + 'INVOICE NUMBER' + "</li></ul>"
    });
    STATUS := 3;
    SENT := true
    end
    end;

     

    The code in the Shipments table is actually the same (it's a Shipment form instead of an Invoice form obviously). Only difference is that in the Shipments table it is a 'trigger after update' event and not a button. In the Shipments table, the E-mail is sent, but the attachment is not there.

    Code is:

     

    if 'WAREHOUSE STATUS' = 1 then
    'SHADOW WAREHOUSE STATUS' := 1
    else
    if 'WAREHOUSE STATUS' = 2 then
    'WAREHOUSE IN' := now();
    'SHADOW WAREHOUSE STATUS' := 2
    else
    if 'WAREHOUSE STATUS' = 3 then
    'SHADOW WAREHOUSE STATUS' := 3;
    let x := Id;
    let i := (create LOG);
    i.(SHIPMENTS := x);
    i.('LOG NUMBER' := text(record(COUNTERS,6).RESULT));
    let c := record(COUNTERS,6).(COUNTER + 1);
    record(COUNTERS,6).(COUNTER := c);
    i.('CREATED BY' := upper(text(user())));
    popupRecord(i)
    else
    if 'WAREHOUSE STATUS' = 4 then
    if 'INCO TERM' = null then
    let x := 'SHADOW WAREHOUSE STATUS';
    alert("INCO TERM EMPTY");
    'WAREHOUSE STATUS' := x
    else
    'SHADOW WAREHOUSE STATUS' := 'WAREHOUSE STATUS'
    end
    else
    if 'WAREHOUSE STATUS' = 5 then
    'SHADOW WAREHOUSE STATUS' := 5
    else
    if 'WAREHOUSE STATUS' = 6 then
    'SHADOW WAREHOUSE STATUS' := 6
    end
    end
    end
    end
    end
    end;
    if CUSTOMER.'RELATION TYPE' = 1 then
    let x := Id;
    let xCust := CUSTOMER;
    for a in select CONTACTS where RELATIONS = xCust and STATUS = 1 and 'E-MAIL SHIPMENT INFO' = true do
    do as server
    let xPDF := printAndSaveRecord(this, "INVOICE FORM");
    let xFile := 'SHIPMENT NUMBER' + ".pdf";
    importFile(this, xPDF, xFile);
    sendEmail({
    from: userEmail(),
    to: a.'E-MAIL',
    subject: 'SHIPMENT NUMBER' + ": STATUS UPDATE",
    text: " ",
    html: "Dear " + a.TITLE + " " + a.'LAST NAME' + ",<br><br>Please be informed that the status of shipment " + 'SHIPMENT NUMBER' + " has been changed to:<br><br><b>" + 'WAREHOUSE STATUS' + "</b><br><br>Best regards,<br>" + user() + "<br><br><b><u>ATTACHMENTS:</u></b><br><ul><li>" + 'SHIPMENT NUMBER' + "</li></ul>",
    attachment: file(this, xFile)
    })
    end
    end
    end

    • John_Halls
    • 2 yrs ago
    • Reported - view

    Hi Martijn

     

    I tested some code based on your construction and it sent emails perfectly.

     

    In your code to send Invoices you have

     

    let a := (select RELATIONS where Id = xCust and 'E-MAIL INVOICES' = true);

     

    This sets a to be an array even if it has a single element. Either use for a in (select RELATIONS... or let a := first(select RELATIONS...

     

    Is RELATIONS the customer table referenced with 'INVOICE TO' from the Invoice table?

     

    Both this code and the Shipment code can be significantly tidied up if you would like me to post examples.

     

    Regards John

    • Martijn_Burghoorn
    • 2 yrs ago
    • Reported - view

    Hi John,

     

    Thank you... I really can't discover why it doesn't work. The e-mail from my Invoices table is indeed being sent. But it gives an error every first time I use the button. The second time it works.

    I have tried other constructions for the Shipment table and in all cases the attachment is NOT sent with the e-mail. Do you have any ideas on that?

     

    Yes, the RELATIONS table is indeed the INVOICE TO field.

    The array created is necessarry. I want the e-mail to be sent to all the contacts within the company (Relations type = 1) that still work at the company (status = 1) and are the ones that need to receive inoices ('e-mail invoices = true') or in case the customer is a private person (Relations type = 2): relations that need to receive invoices ('e-mail invoices' = true).

    • Martijn_Burghoorn
    • 2 yrs ago
    • Reported - view

    Hi John, 

     

    further to the above: did you test in a browser or in the mac app? I just did a complete a-z test run in browser and emailing invoices went ok (mailing from shipment table still not!). Then I retried from the Mac app and got the error mailing invoices again:

     

    Schermafbeelding 2021-11-29 om 16.48.00

     

    Also I noticed that several functions I have build in the Mac app do not function properly in the browser... minor things as a properly working loop up field referring to another table did not work well in safari window.

    Am I the only one facing these things? If not: is it not strange that some things don't work well in the app and do work in browser or vice versa? 

    • Mel_Charles
    • 2 yrs ago
    • Reported - view

    Hi Martijn

    I'm totally cloud. scanning down your script, the core essential code is doin what I am doing abd as stated I'm working fine.

    I can't comment on the mac app though as don't use it

     

    Can I suggest you build a little test rig - strip out all that is not relevant

    one table and fire something off it this works add in a bit more

    my order shipment is a somewhat simple affair but here is my code if this helps :-)

    if Status < 2 or Status > 3 then
    alert("job Status is not in production")
    else
    Status := 3;
    if not 'Email+' then
    alert("No valid contact in email field")
    else
    if DispFlag = 1 then
    alert("Dispatched Confirmation already sent!")
    else
    let thisBody := first((select SystemFile).OrderDispatchedLetter);
    let myEmail := userEmail();
    let myAtt := first(files(record(SystemFile,1)));
    thisBody := replace(thisBody, "{Ord Company}", text('Ord Company'));
    thisBody := replace(thisBody, "{Order Name}", text('Order Name'));
    thisBody := replace(thisBody, "{Job Bag}", text('Job Bag'));
    thisBody := replace(thisBody, "{Order Date}", text('Order Date'));
    thisBody := replace(thisBody, "{Order Name}", 'Order Name');
    thisBody := replace(thisBody, "{Order Ref}", 'Order Ref');
    thisBody := replace(thisBody, "{Qty}", text(Qty));
    thisBody := replace(thisBody, "{Desc}", Desc);
    thisBody := replace(thisBody, "{Del Company}", text('Del Company'));
    thisBody := replace(thisBody, "{Del Address}", text('Del Address'));
    DispFlag := 1;
    sendEmail({
    from: myEmail,
    to: 'Email+',
    subject: "Your order has been dispatched by 118 Printgroup",
    text: "Goods Dispatched Confirmation",
    html: thisBody,
    attachments: myAtt
    });
    alert("Goods Dispatched Email has been Sent");
    DisplayMenu := 1
    end
    end
    end

     

    only real difference here is that I am picking up an attachemnt in a remote (system) table

    works perfect (cloud version)

Content aside

  • 2 yrs agoLast active
  • 6Replies
  • 359Views