Skip to main content

Notifications

Microsoft Dynamics AX forum
Answered

How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

Posted on by Microsoft Employee

Hello I have a task.

I want to run a X++ job that much identify a folder say in C drive. The folders will be numbered like personnel numbers in HcmWoker table. Each folder may consists of some files of types PDF,word,jpeg and so on.

The X++ job must go to the particular folder and add all these files as attachments to the particular record numbered according to the folder name(personnel number).

Can anybody give some ideas? How I can begin? Or C# is suitable for this? Or you have X++ code?

For the movement I am able to attach documents when I run the X++ job, I will browse manually and find the file and attach it to the document.

Now I wanted this process must get automated that whenever I run the Job the job must automatically go and upload all the files.

  • Faisal Fareed Profile Picture
    Faisal Fareed 10,794 User Group Leader on at
    RE: How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

    No trouble - Am glad your requirement get resolved. CHeers

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

    Yes Fareed. I was not able to see your solution. But I will try to use later the execution time  calculation which is used in the link you have given.  

  • Faisal Fareed Profile Picture
    Faisal Fareed 10,794 User Group Leader on at
    RE: How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

    Excellent - it worked for you.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

    Thanks to all for your help.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

    Hi Baber and all,

    By following different things I am able to attach documents.

    Now the context is that I want to attach certificates for the employees in HcmWorker table may contain documents like scanned certificates(jpeg,mpeg) PDF files and so on.

    The documents will be arranged in a folder named according to the  personnel number field in HCMWorker table like 000001,000542 and so on. In each of these folders documents to be uploaded for each employee will be there.

    Next thing. What maximum file sizes combinedly can I attach? All other ideas as per your experiences?

  • Verified answer
    syed baber Profile Picture
    syed baber 11,627 on at
    RE: How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

    Hi Mahesh,

    Recenlty, I did the same thing in one of the project I was working on. Below is the code to traverse through the directory and return the files in container variable:

    private container getListOfImportFiles()

    {

       System.Array                files;

       InteropPermission           permission;

       counter     fileCounter;

       container   fileList;

       ;

       permission  = new InteropPermission(InteropKind::ClrInterop);

       permission.assert();

       files = CLRInterop::staticInvoke("System.IO.Directory", "GetFiles", YourDirectoryPath, "*.csv");

       for (fileCounter = 0; fileCounter < ClrInterop::getAnyTypeForObject(files.get_Length()); fileCounter++)

       {

           fileList = conins(fileList, conLen(fileList)+1, ClrInterop::getAnyTypeForObject(files.GetValue(fileCounter)));

       }

       CodeAccessPermission::revertAssert();

       return fileList;

    }

    After this, you can write the code to read the files one by one. Code is shown below:

    FileName            importFileName;

    Counter     counter;

       container   fileList;

       FileIOPermission    permission;

       CommaTextIo         file;

       container           line;

       int                 recordCount;

    if (conlen(fileList) > 0)

       {

           for (counter =1; counter <= conlen(fileList); counter++)

           {

               importFileName = conPeek(fileList, counter);

               permission = new FileIOPermission(importFileName, "RW" );

               permission.assert();

               file = new CommaTextIo(importFileName, #io_read);

               if (!file || file.status() != IO_Status::Ok)

               {

                   throw error("File not found");

               }

               file.inFieldDelimiter(',');

               line = file.read();

               while (file.status() == IO_Status::Ok)

               {

                   recordCount++;

                   // process the file here by using the container variable 'line'

                   line = file.read();

               }

               file = null;          

               CodeAccessPermission::revertAssert();

           } // End for loop

       } // End outer If clause

    For document attachment, refer to the below code:

    private void attachDoc(RefTableId _refTableId, RefRecId _refRecId, selectableDataArea _refCompanyId, FileName _name)

    {  

       DocuRef docuRef;

       DocuActionArchive archive;

       FileName fileName;

       ;

       // Get the file name only.

       fileName = conPeek(fileNameSplit(_name), 2);

       docuRef.clear();

       docuRef.RefRecId = _refRecId;

       docuRef.RefTableId = _refTableId;

       docuRef.RefCompanyId = _refCompanyId;

       docuRef.Name = fileName;

       docuRef.TypeId = YourTypeID;

       docuRef.insert();

       archive = new DocuActionArchive();

       archive.insertDocuValue(docuRef, _name);

       //archive.add(docuRef, _name);

    }

    Please verify and let me know if you have further queries.

    Thanks,

    Baber.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

    I will check it Faisal.

  • Suggested answer
    Faisal Fareed Profile Picture
    Faisal Fareed 10,794 User Group Leader on at
    RE: How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

    Hi Mahesh,

    I did this bit of work few days ago and blog it here daxture.blogspot.com.au/.../retrieving-files-from-directory-through.html

    For attaching it as attachments please have a look on DocuRef class as suggested by Rachit in above link.

    If you need more help please do let me know

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

    Okay Rachit Garg and jens I will try it.

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to access a list of files from a folder and attach it to a record in Ax 2012 using X++?

    Check the WINAPI class, it contains functions to operate Folders.

    There are also .NET functions you can use e.g. check out the System.IO.Directory Namespace/class.

    To attach the document to a record you have to create the Docu* table records (DocuRef, DocuValue,...).

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 9th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Dynamics 365 Community Newsletter - August 2024

Catch up on the latest D365 Community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,186 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 227,996 Super User 2024 Season 2

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans