web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Attachment of file from batch job running on server

(0) ShareShare
ReportReport
Posted on by 434
  • Dear All,

     

    I have to attach file from batch job which are scheduled on server. I have used below codes and it is not working.

    same code using add(docuref,_name) method works if i run batch job manually and this is due to class docuactionarchive class is running on client and also using winapi methods which runs on client.

    then i Tried to use method insertDocuValue(docuRef, _name) then also it is not working 

    i have application server, database server and on users system client is installed so they will be providing me path of file  which needs to be attached from there system to destination folder which is mentioned in active directory.

    data will be provided in sql table, so first i am reading and then written below code but it is not working when batch server is running.

    it enters path provided in doruref table but file is not copied to destination folder , so when i am trying to open file it says file does not exist on destination folder path.

    tsBegin;

    docuRef.clear();

    docuRef.RefRecId = _InventBatch.RecId;

    docuRef.RefTableId = tableNum("InventBatch");

    docuRef.RefCompanyId = curext();

    docuRef.Name = _name;

    info(_name);

    docuRef.TypeId = type;

    docuRef.insert();

    //this code will be used for attachement of file

    archive = new GOD_DocuActionArchive();

    //archive.add(docuRef, _name); 

    // above commented code is used to attach from clients even if batch job run manually through menu items it works fine.

    archive.insertDocuValue(docuRef, _name); 

     // later this method is used but it is not generating sequence no and not copying file to destination folder.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Sohaib Cheema Profile Picture
    49,438 User Group Leader on at

    Two noticeable points

    1. Class DocuActionArchive is made to run on Client, so if you want to run it on server, make a copy of it and set RunOn=Server

    2. Further going into DocuActionArchiveYourCopy. Add method has three doubtful areas which you need to fix

      1. Code Like _docuRef.dataSource()

        When you attach a document from Rich Client and EP, you have caller DataSource. But when you are calling it using Batch there is no caller DS. So those lines of code will fail where dataSource call is being used.

      2. All calls of WinAPI

        Any call to WinAPI is client based. Might be your Batch Job code is running on server. In that case you have to use WinAPServer. You can see below 2 class of WinAPI inside DocuActionArchiveYourCopy

    1. WinAPI::fileSize

    2. WinAPI::copyFile

      So, let me summarize solution

    1. Create a copy of DocuActionArchive and set it runon Server. Let name it as DocuActionArchiveYourCopy

    2. Inside add method of DocuActionArchiveYourCopy, check by code as below

      If(running on server)

      {

                      Call to WinAPIServer

      }

      Else

      {

                      Call to WinApi //a it is now/

      }

      //it is important to put if condition in batch jobs for such cases.

       

  • Suggested answer
    Evan Adamson Profile Picture
    105 on at

    I copied the DocuActionArchive class and modified the add method as Shohaib suggested in another thread. Below is the code which I tested and works. I added another parameter so we could use it on the server and client. It still needs a little work as you can see when running on the server, and the attachFilename is null it may fail. I didn't test this case. It works the way it is for what we needed it for.

    void add(DocuRef _docuRef, Filename _filename = '', boolean _runOnServer = false)

    {

       Filename            attachFilename;

       Filename            docuFilename;

       Filename            onlyFilename;

       container           fileName;

       boolean             docuRules;

       DocuRef             docuReference;

       int                 fileSize;

       boolean             isFileSizeOk;

       int                 hWnd = 0;

       Microsoft.Dynamics.AX.Framework.OfficeAddin.SharePoint.AuthenticationType docuAuthType;

       #File

       #XppTexts

       // PBA begin

       attachFilename = _filename;

       if(!attachFilename && !_runOnServer)

       // PBA end

       {

           if (_docuRef.dataSource() != null && _docuRef.dataSource().formRun() != null)

           {

               hWnd = _docuRef.dataSource().formRun().hWnd();

           }

           attachFilename = WinAPI::getOpenFileName(

               hWnd,

               ["@SYS26054", #AllFiles], /* *.* */

               #emptyString,

               "@SYS26798");

       }

       if (! attachFilename)

           return;

       fileName = Docu::splitFilename(attachFilename);

       _docuRef.Name = conpeek(fileName, 1);

       _docuRef.doUpdate();

       [onlyFilename,curFileType]  = Docu::splitFilename(attachFilename);

       docuRules = Docu::validateExtension(curFileType);

       isFileSizeOk = true;

       if (docuRules)

       {

           if(_runOnServer)

           {

               fileSize = WinAPIServer::fileSize(attachFilename);

           }

           else

           {

               fileSize = WinAPI::fileSize(attachFilename);

           }

           isFileSizeOk = Docu::validateFileSize(_docuRef.docuType() ,fileSize);

       }

       if (!docuRules || !isFileSizeOk)

       {

           ttsbegin;

           docuReference = DocuRef::findRecId(_docuRef.RecId, true);

           docuReference.delete();

           ttscommit;

           if (_docuRef.dataSource())

           {

               _docuRef.dataSource().reread();

               _docuRef.dataSource().refresh();

           }

           if (!docuRules)

               error("@SYS99219");

           else

               error("@SYS97286");

           return;

       }

       super(_docuRef);

       if (this.mustArchiveFiles())

       {

           docuFilename = _docuRef.path()

                           + onlyFilename

                           + #FileNameDelimiter

                           + smmDocuments::getNonExistingFileName(numSeq,_docuRef.path(),this.fileType())

                           + #FileExtensionDelimiter

                           + this.fileType(); //new Filename

                           //uses curFileType

           if (_docuRef.docuType().FilePlace == DocuFilePlace::SharePoint)

           {

               docuAuthType = DocuSharePointParameters::ConvertAuthenticationType(DocuSharePointParameters::GetAuthenticationType(docuFilename));

               Microsoft.Dynamics.AX.Framework.OfficeAddin.SharePoint.DocumentUploader::UploadDocument(attachFilename, new System.Uri(docuFilename), docuAuthType, false);

           }

           else

           {

               if(_runOnServer)

               {

                   WinAPIServer::copyFile(attachFilename, docuFilename);

               }

               else

               {

                   WinAPI::copyFile(attachFilename, docuFilename);

               }

           }

       }

       else

       {

           docuFilename = attachFilename;

       }

       this.insertDocuValue(_docuRef,docuFilename);

       curFileType = #emptyString;

    }

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans