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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Change the filename of an SSRS report when emailed through X++

(0) ShareShare
ReportReport
Posted on by 213

Hi

I have the following code (excerpt only, not full method) to both email a SSRS report and save it as a file:

    // create email contract
    emailContract = new SrsReportEMailDataContract();
    
    // fill in the email contract details
    emailContract.parmAttachmentFileFormat(SRSReportFileFormat::PDF); 
    emailContract.parmSubject("My Report"); 
    emailContract.parmTo("someone@someone.com");   
    
    // email report
    certificateController.parmReportContract().parmPrintSettings().printMediumType(SRSPrintMediumType::Email);
    certificateController.parmReportContract().parmPrintSettings().parmEmailContract(emailContract);
    certificateController.parmReportContract().parmPrintSettings().fileFormat(SRSReportFileFormat::PDF);
    certificateController.parmReportContract().parmPrintSettings().fileName(tSaveParameters.CertificateUNCPath + '\\' +  fileNamePDF);
    
    certificateController.runReport();    
    
    // generate file for archiving
    certificateController.parmReportContract().parmPrintSettings().printMediumType(SRSPrintMediumType::File);
    certificateController.parmReportContract().parmPrintSettings().overwriteFile(true);
    certificateController.parmReportContract().parmPrintSettings().fileFormat(SRSReportFileFormat::PDF);
    certificateController.parmReportContract().parmPrintSettings().fileName(tMFilesParameters.FLW_MFilesCertificateUNCPath + '\\' +  fileNamePDF);

    certificateController.runReport();

When the email is received, the report as an email attachment is the AOT name of the report eg. Reportname.PrecisionDesign1.pdf

When the report is saved to a file share, it is the name I gave it.

Can I change the name of the attachment on the email, in the same way I change the name of the saved file?

Many thanks in advance!

*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    239,688 Most Valuable Professional on at

    Isn't the problem in the fact that you aren't providing a valid file name?

    You use a path (not just a file name) when sending the file via e-mail, therefore you're trying to create an attachment called \\SomeLocation\MyReport.pdf, which isn't a valid file name. Instead, you should use something like MyReport.pdf. Give it a try and let's see...

  • cjohnson300 Profile Picture
    213 on at

    Sorry, any one got any ideas?!

  • Suggested answer
    Jie G Profile Picture
    Microsoft Employee on at

    Hi cjohnson300, 

    You can use 

    certificateController.parmReportContract().parmPrintSettings().fileName(fileNamePDF);


    instead of 

    certificateController.parmReportContract().parmPrintSettings().fileName(tSaveParameters.CertificateUNCPath + '\\' +  fileNamePDF);

    I have tested and confirmed that certificateController.parmReportContract().parmPrintSettings().fileName(fileNamePDF) will set the attached PDF with the filename you gave it.

  • cjohnson300 Profile Picture
    213 on at

    Thanks Martin.  

    As suggested, I changed the line to this:

       certificateController.parmReportContract().parmPrintSettings().fileName('myreport.pdf');


    but the email I received still has the attachment named as [report name].[Design name].pdf

    1738.tempsnip.png

    Thanks for your help!

  • cjohnson300 Profile Picture
    213 on at

    Hi

    Many thanks for the suggestion, but as you can see from my later post, it didn't work for me, unless I did something else wrong?!

  • Jie G Profile Picture
    Microsoft Employee on at

    Hi cjohnson300,

    Which version of AX 2012 are you using? Which report are you sending as an attachment?

    Please check the filename( ) of the SrsPrintDestinationSettings class.

    /// <summary>
    /// Gets or sets the file name.
    /// </summary>
    /// <param name="_value">
    /// The name of the file to set; optional.
    /// </param>
    /// <returns>
    /// The current file name.
    /// </returns>
    /// <exception cref="M:Exception::Error">
    /// The directory does not exist.
    /// </exception>
    ///
    [DataMemberAttribute]
    public str fileName(str _value = fileName)
    {
        #WinAPI
        str directory;
    
        // if setter is called, then get directory and check if valid, else throw error.
        if (_value && !prmisDefault(_value))
        {
            directory = System.IO.Path::GetDirectoryName(_value);
    
            // if the directory is empty then use the users temp path.
            if (!directory)
            {
                directory = SRSPrintDestinationSettings::getTempFilePath();
    
                // create the file name by combining the temp path the filename.
                fileName = System.IO.Path::Combine(directory, _value);
            }
            else
            {
                if (printMediumType == SRSPrintMediumType::File && !System.IO.Directory::Exists(directory))
                {
                    throw error(strFmt("@SYS72247", directory));
                }
    
                // if directory is valid, then just assign the value to member variable.
                fileName = _value;
            }
    
            // if file name is changed, user may be prompted for overwriting existing file; 
            //pass false to allow this prompt to happen
            this.parmOverwriteFileIsSet(false);
        }
    
        return fileName;
    }


    You can also set a break point and debug to trace the filename.

  • cjohnson300 Profile Picture
    213 on at

    Using AX2012 R2 CU6 and the report is a bespoke report for our organisation.

    At this point in the code, the filename is as I was hoping but I can only presume something else is overwriting it later?

    Also, within the SRSReportRunController there is a number of variables set to the name of the report, I would expect one of these to be the name of the attachment?  Does anyone know which method of which class does the actual attaching of the file and / or the sending of the email?

    54051.Capture.PNG

  • Suggested answer
    Jie G Profile Picture
    Microsoft Employee on at
    Hi cjohnson300,
    Here is the call stack when sending a standard report BankCodaDetails.
    [c]    \Classes\SysINetMail\sendMailAttachEx                                                                  37
    [c]    \Classes\SysINetMail\sendMailAttach                                                                    16
    [c]    \Classes\SrsReportRunMailer\emailReport                                                                40
    [c]    \Classes\SrsReportRunPrinter\toEmail                                                                   36
    [c]    \Classes\SrsReportRunPrinter\printReport                                                               38
    [c]    \Classes\SrsReportRunService\runReport                                                                 43
    [c]    \Classes\SrsReportRunImpl\runReport                                                                     7
    [c]    \Classes\SrsReportRunController\runReport                                                              94
    [c]    \Classes\RdlItemTransController\main                                                                   21
  • cjohnson300 Profile Picture
    213 on at

    Many thanks Ada

    I have traced it as far as this method SrsReportDataContract::parmReportName

    [DataMemberAttribute('ReportName')]
    public SRSCatalogItemName parmReportName(SRSCatalogItemName _reportName = reportName)
    {
        // if setter is called and the new report name is different than the existing name, then set the report path also.
        if(_reportName != reportName)
        {
            reportName = _reportName;
            reportPath = ''; // reset the path, so that next call to parmReportPath will get the new path.
        }
    
        return reportName;
    }

    But the variables _reportName and reportName are the same (the wrong string!).  I guess I need to set a DataMemberAttribute in the report?

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 16

#2
Harisgillani Profile Picture

Harisgillani 4

#2
Nagendra Varma K Profile Picture

Nagendra Varma K 4

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans