clearing the Mail Variables for CodeUnit 397

stormcandistormcandi Member Posts: 27
I am new to this forum and to Microsoft NAV development. I am hoping someone can shed some light on this for me. I have a query that I am reading to get data for an email. Right before I loop to the next record I send the email. The issue I'm having is that the Email Body is not clearing out when it goes through the loop again. So it appends all the next record's data to the first record. I thought once you iterate through a record and go to the next record, that all variables are automatically cleared. I am pasting my code below. Hopefully someone can tell me what I am doing wrong.
CompanyInfo.GET();


PaymentList.SETRANGE(Posting_Date, StartDate);
IF NOT PaymentList.OPEN THEN
  EXIT;
WHILE PaymentList.READ DO BEGIN
  Body := '';
  EmailTo := email address here;
  EmailCC := 'email address here';
  Subject := 'Test Email';
  Mail.AddBodyline(STRSUBSTNO('<table><tr><td>%1</td></tr></table>', PaymentList.Name));
  Mail.AddBodyline('Dear Customer,');
  Mail.AddBodyline('<br><br>');
  Mail.AddBodyline('The below payment has been received:');
  Mail.AddBodyline('<table cellpadding="5" cellspacing="5"><tr><th>Posting Date</th><th>Amount</th></tr>');
  Mail.AddBodyline(STRSUBSTNO('<tr><td>%1</td><td>$%2</td></tr>', PaymentList.Posting_Date, FORMAT(PaymentList.Amount, 0, '<Precision,2:2><Standard Format,0>')));
  Mail.AddBodyline('</table>');
  InvoiceList.SETFILTER(Document_No, PaymentList.Document_No);
  InvoiceList.SETFILTER(Vendor_No, PaymentList.Vendor_No);
  IF NOT InvoiceList.OPEN THEN
    EXIT;
  Mail.AddBodyline('This payment was applied to the below invoice(s):');
  Mail.AddBodyline('<table cellspacing="5" cellpadding="5"><tr><th>Posting Date</th><th>Amount</th></tr>');
  WHILE InvoiceList.READ DO BEGIN
    Mail.AddBodyline(STRSUBSTNO('<tr><td>%1</td><td>$%2</td></tr>', InvoiceList.Posting_Date, FORMAT(InvoiceList.Amount, 0, '<Precision,2:2><Standard Format,0>')));
  END;
  Mail.AddBodyline('</table>');
  InvoiceList.CLOSE;
  Mail.AddBodyline('<br>');
  Mail.AddBodyline('Regards,');
  Mail.AddBodyline('<br><br>');
  Mail.AddBodyline(CompanyInfo.Name);
  Mail.NewMessage(EmailTo,EmailCC,Subject,Body,'',FALSE);
  CLEARALL;
END;
PaymentList.CLOSE;











](*,)

Answers

  • evandeveerdonkevandeveerdonk Member Posts: 49
    Hi Stormcandi,

    apparently, your CLEARALL, in combination with the WHILE ...READ loop, doesn't do the job.
    So you have to consider other options:
    - program a REPEAT..UNTIL and use CLEAR(Mail);
    - create and call a function everytime you read a new rocord, programm the mail-functionality there, and create the variable Mail as a local variable

    Ernst
    http://www.vssolutions NAV-Outlook synchronisation re-invented.
  • stormcandistormcandi Member Posts: 27
    I took part of your suggestion and it worked. Instead of CLEARALL I did Clear(Mail). I never tried the Clear(Mail) and didn't even think about it. Worked like a charm. Thanks so much for your help!

    \:D/
  • evandeveerdonkevandeveerdonk Member Posts: 49
    You are welcome
    http://www.vssolutions NAV-Outlook synchronisation re-invented.
Sign In or Register to comment.