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...
Answered

import multiple files in a folder

(0) ShareShare
ReportReport
Posted on by 40

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. 

I have the same question (0)
  • Armela Kamenica Profile Picture
    231 on at
    RE: import multiple files in a folder

    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 ?

  • Verified answer
    Rio.Arfn Profile Picture
    40 on at
    RE: import multiple files in a folder

    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!

  • Rio.Arfn Profile Picture
    40 on at
    RE: import multiple files in a folder

    Here is the global var:

    global-var.png

    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;

    ----------------------

  • Rio.Arfn Profile Picture
    40 on at
    RE: import multiple files in a folder

    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.

  • Suggested answer
    Steven Renders Profile Picture
    5,591 Moderator on at
    RE: import multiple files in a folder

    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.

  • Rio.Arfn Profile Picture
    40 on at
    RE: import multiple files in a folder

    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?

  • Steven Renders Profile Picture
    5,591 Moderator on at
    RE: import multiple files in a folder

    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?

  • Rio.Arfn Profile Picture
    40 on at
    RE: import multiple files in a folder

    Hi Jyotsna,

    I want to import multiple automatically, the job queue get error because of dotnet

  • Jyotsna NAV Profile Picture
    585 on at
    RE: import multiple files in a folder

    What is the problem that you are facing exactly?

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…

Mansi Soni – Community Spotlight

We are honored to recognize Mansi Soni as our August 2025 Community…

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

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

#1
Sohail Ahmed Profile Picture

Sohail Ahmed 2,899 Super User 2025 Season 2

#2
Sumit Singh Profile Picture

Sumit Singh 2,444

#3
Jeffrey Bulanadi Profile Picture

Jeffrey Bulanadi 2,304

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans