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 :
Finance | Project Operations, Human Resources, ...
Unanswered

Unable to Attach generated XML File to a Batch Job in Dynamics 365 FO

(0) ShareShare
ReportReport
Posted on by 9

Hello everyone,

 

I’m facing an issue when trying to automatically attach an XML file to a Batch Job in Dynamics 365 Finance & Operations.

 

I am generating an XML file inside a class that extends RunBaseBatch.

After generating the file, I want to attach it to the Batch Job.

 

What works


  • The batch job executes successfully.

  • The XML file is generated correctly.

  • The DocuRef + DocuValue records are created in SQL with the correct:

  • RefRecId = BatchJob RecId

    • RefTableId = 12070 (BatchJob)

    • RefCompanyId = correct company

    • File is saved through IDocumentStorageProvider.SaveFile  

What does NOT work

 

The attachment does not appear in the Batch Job form.

 

I have checked SQL and the DocuRef/DocuValue entries are created correctly, but the UI does not show the attachment.

 

I also tried using the class DocuManagement (method attachFileForReference), but I still get the same result.

 

public void run()
{
    XmlSAFTGenerator xmlSAFTGenerator;
    str result;
    result = xmlSAFTGenerator.GerarArquivoXML(
        _nameFile,
        _auditFileVersion,
        _taxAccountingBasis,
        _startDate,
        _endDate,
        _fiscalYear
    );
    // XML file path
    str xmlFileName = _nameFile + ".xml";
    str xmlFilePath = System.IO.Path::GetTempPath() + xmlFileName;
    // Attach XML to Batch Job
    this.attachFileToBatchJob(_batchJobId, xmlFileName, xmlFilePath);
}

private void attachFileToBatchJob(RefRecId _batchJob, str _xmlFileName, str _filePath)
{
    DocuRef docuRef;
    DocuValue docuValue;
    DocumentLocation location;
    IDocumentStorageProvider storageProvider;
    System.IO.MemoryStream fileStream =
        new System.IO.MemoryStream(System.IO.File::ReadAllBytes(_filePath));
    DocuType docType = DocuType::find("File");
    storageProvider = Docu::GetStorageProvider(docType, true, curUserId());
    ttsBegin;
    docuValue.clear();
    docuValue.FileId            = newGuid();
    docuValue.OriginalFileName  = _xmlFileName;
    docuValue.FileName          = _xmlFileName;
    docuValue.Name              = _xmlFileName;
    docuValue.FileType          = "xml";
    docuValue.StorageProviderId = storageProvider.ProviderId;
    location = storageProvider.SaveFile(
        docuValue.FileId,
        storageProvider.GenerateUniqueName(_xmlFileName),
        "application/xml",
        fileStream
    );
    if (location)
    {
        if (location.get_NavigationUri())
            docuValue.Path = location.get_NavigationUri().ToString();
        if (location.get_AccessUri())
            docuValue.AccessInformation = location.get_AccessUri().ToString();
    }
    docuValue.insert();
    ttsCommit;
    ttsBegin;
    docuRef.clear();
    docuRef.RefTableId   = tableNum(BatchJob);
    docuRef.RefRecId     = _batchJob;
    docuRef.RefCompanyId = curext();
    docuRef.TypeId       = "File";
    docuRef.Name         = _xmlFileName;
    docuRef.ValueRecId   = docuValue.RecId;
    docuRef.insert();
    ttsCommit;
    info(strFmt("SAF-T XML attached to Batch Job %1", _batchJob));
}



Has anyone successfully attached documents to a Batch Job?

Any help, guidance, or working example would be greatly appreciated.

Thank you!


 

I have the same question (0)

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 596 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 570 Super User 2025 Season 2

#3
CU05031448-0 Profile Picture

CU05031448-0 556

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans