Announcements
I want to use job queue to import all CSV files in a folder. But I have a problem when using the job queue because of DotNet.
I'm using BC14.
Hello everyone.
I am having an issue getting all files from a directory on the server using AL .
We were able to use the File Record to get all files in the folder. How do we do that now with AL Code ?
I change my code from using temp file to use filemanagement.BLOBImportFromServerFile() and then create instream from TempBlob.Blob. And it's works!
Here is the code:
----------
ReadAndImportFileValue(FileToProcess : Text[256];pFileName : Text;pFilePath : Text;CurrDateTime : DateTime)
IF FileToProcess <> '' THEN
BEGIN
CurrentFileName := pFileName;
{_Path:= pFilePath + '\' + pFileName;
FileVar.OPEN(_Path);
FileVar.CREATETEMPFILE;
FileVar.CREATEINSTREAM(StreamInTxt);
DOWNLOADFROMSTREAM(StreamInTxt,'','<TEMP>','',MagicPath);
FileVar.CLOSE;
FOR i := STRLEN(MagicPath) DOWNTO 1 DO
BEGIN
IF MagicPath[i] = '\' THEN
BEGIN
MagicPath := COPYSTR(MagicPath,1,i);
i := 1;
END;
END;}
FileToUpload := pFileName;
FolderName := pFilePath;
{IF ISCLEAR(FileSystemObject) THEN
CREATE(FileSystemObject,TRUE,TRUE);
FileSystemObject.CopyFile(FolderName + '\' + FileToUpload,MagicPath + '\' + FileToUpload);
UPLOADINTOSTREAM('','<TEMP>','',FileToUpload,StreamInTxt);}
FileMGT.BLOBImportFromServerFile(TempBlob,FolderName + '\' + FileToUpload);
TempBlob.Blob.CREATEINSTREAM(StreamInTxt);
CSVBuffer.DELETEALL;
CSVBuffer.LoadDataFromStream(StreamInTxt, ';');
......
----------
Thanks for your help, Jyotsna and Steven!
Here is the global var:
Here is my code:
----------------------
CollectFile(SourceSAPFolder : Text[250];TargetSAPFolder : Text[250])
DateImported := CURRENTDATETIME;
CLEAR(TotalRecord);
Obj := Folder.GetFiles(SourceSAPFolder, '*.CSV');
Lst := Lst.List;
Lst.AddRange(Obj);
FOR L := 0 TO Lst.Count - 1 DO BEGIN
VarFileName := '';
VarFileName := Path.GetFileName(Lst.Item(L));
VarPath := '';
VarPath := Path.GetDirectoryName(Lst.Item(L));
SAPLog.RESET;
SAPLog.SETRANGE("CSV File Name", VarFileName);
IF NOT SAPLog.FINDFIRST THEN BEGIN
ReadAndImportFileValue(STRSUBSTNO('%1\%2',VarPath,VarFileName),VarFileName,VarPath,DateImported);
END;
END;
END;
ReadAndImportFileValue(FileToProcess : Text[256];pFileName : Text;pFilePath : Text;CurrDateTime : DateTime)
IF FileToProcess <> '' THEN
BEGIN
CurrentFileName := pFileName;
FileVar.CREATETEMPFILE;
FileVar.CREATEINSTREAM(StreamInTxt);
DOWNLOADFROMSTREAM(StreamInTxt,'','<TEMP>','',MagicPath);
FileVar.CLOSE;
FOR i := STRLEN(MagicPath) DOWNTO 1 DO
BEGIN
IF MagicPath[i] = '\' THEN
BEGIN
MagicPath := COPYSTR(MagicPath,1,i);
i := 1;
END;
END;
FileToUpload := pFileName;
FolderName := pFilePath;
IF ISCLEAR(FileSystemObject) THEN
CREATE(FileSystemObject,TRUE,TRUE);
FileSystemObject.CopyFile(FolderName + '\' + FileToUpload,MagicPath + '\' + FileToUpload);
UPLOADINTOSTREAM('','<TEMP>','',FileToUpload,StreamInTxt);
CSVBuffer.DELETEALL;
CSVBuffer.LoadDataFromStream(StreamInTxt, ';');
IF CSVBuffer.FINDFIRST() THEN BEGIN
REPEAT
CASE CSVBuffer."Field No." OF
//insert value to table SAPLog
UNTIL CSVBuffer.NEXT() = 0;
END;
END;
----------------------
yes, I'm using on-prem.
thanks for your suggestion, it's work. but I have another error again. Here is the error:
---------------------------
Microsoft Dynamics 365 Business Central Server attempted to issue a client callback to download a file: (CodeUnit 50010 Import ). Client callbacks are not supported on Microsoft Dynamics 365 Business Central Server.
---------------------------
My customer said that they can't use API.
Another question, are you running this in BC On Prem? I suppose yes, otherwise .net interop would not work at all.
The error message suggests that during the process there's a client callback, so that probably means a GUI popups at a certain moment, which is not allowed if you run code via the job queue.
Also, you cannot run client-side .NET in the job queue. Is that the case? Are you using the RunOnClient property? Try to put it on No. Here's an example:
https://github.com/Microsoft/AL/issues/4033
Another solution is to use a web-service instead. If you need to exchange data, why work with files? If SAP has an API, that would be a better aproach, imho.
Hi Steven,
Sorry, I will explain it clearly.
I want to import files from SAP to BC. This files will be Purchase Order.
I build a custom codeunit to import the files. Then I put this codeunit to job queue so the import process will be done automatically. After the files become Purchase Order, existing will be moved to another directory. For moving the files, I'm using File Management Codeunit.
When I'm trying the custom codeunit manually (not using the job queue), it works. But when I put to codeunit, this is the error message:
---------------------------
Microsoft Dynamics 365 Business Central Server attempted to issue a client callback to create a DotNet object: System.IO.Directory (CodeUnit 50010 Import). Client callbacks are not supported on Microsoft Dynamics 365 Business Central Server.
----------------------------
Is there any solution for this? Should I need to put the files, not in the local drive?
Hi Rio,
This sounds a bit strange, could you be a little more precise about the details?
Is the job queue running a certain report/codeunit?
Who developed this codeunit/report, or is it a standard one?
What's the exact error message you see?
Hi Jyotsna,
I want to import multiple automatically, the job queue get error because of dotnet
What is the problem that you are facing exactly?
André Arnaud de Cal... 291,359 Super User 2024 Season 2
Martin Dráb 230,370 Most Valuable Professional
nmaenpaa 101,156