web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested answer

Dynamic Attachment in email Based on lines content

(3) ShareShare
ReportReport
Posted on by
I am working on a procedure that should generate and add as attachment dynamic reports based on line content.
I have 2 tables IFS Tender Committee(header) and IFS Tender Committee member(line). The report has no Issue I have tested it using print action on a page. I want to automate the process so that each committee member receives a unique report based on the lines.
Currently when I set range 
( CommitteMembers.Reset;
  CommitteMembers.SetRange("Document No.", IFSTenderCommitte."Document No.");
  if CommitteMembers.FindSet then begin
  RecRef.GetTable(CommitteMembers);
  TempBlob.CreateOutStream(OutStr);
  Report.SaveAs(Report::"Appointment letters", '', ReportFormat::Pdf, OutStr, RecRef);
  TempBlob.CreateInStream(InStr);)
only the first report is being generated and emailed to all members. The other details like message body are unique for each member. 
I have tried several options and I am always getting a new error.
 
  • Suggested answer
    Dr Gomathi MVP, MCT Community lead, MLE Profile Picture
    on at
    Dynamic Attachment in email Based on lines content

    Solution: Modify the Code to Generate Reports per Member

    Try modifying your loop to generate the report separately for each committee member:

     

    CommitteMembers.Reset();
    CommitteMembers.SetRange("Document No.", IFSTenderCommitte."Document No.");
    if CommitteMembers.FindSet() then begin
        repeat
            RecRef.GetTable(CommitteMembers);
            
            // Create a new TempBlob for each iteration
            Clear(TempBlob);
            TempBlob.CreateOutStream(OutStr);
            
            // Generate report uniquely for each member
            Report.SaveAs(Report::"Appointment letters", '', ReportFormat::Pdf, OutStr, RecRef);
            
            TempBlob.CreateInStream(InStr);
            
            // Send email to each member with their unique report
            SendEmailToMember(CommitteMembers."Email", InStr, CommitteMembers."Member Name"); 
            
        until CommitteMembers.Next() = 0;
    end;
    
    Send email function
     
    procedure SendEmailToMember(MemberEmail: Text; Attachment: InStream; MemberName: Text)
    var
        Mail: Codeunit "Email Message";
        EmailSender: Codeunit "Email";
    begin
        Mail.Create(EmailScenario::Default);
        Mail.SetSender('your-email@example.com', 'Your Name');
        Mail.AddRecipient(MemberEmail, MemberName, MailRecipientType::To);
        Mail.SetSubject('Appointment Letter');
        Mail.SetBody('Dear ' + MemberName + ', Please find your appointment letter attached.');
        Mail.AddAttachment('Appointment_Letter.pdf', 'application/pdf', Attachment);
        
        EmailSender.Send(Mail, Enum::"Email Send Option"::"Send Immediately");
    end;
    
     
    Please try this and let me know
  • Gerardo Rentería García Profile Picture
    on at

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans