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!