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)

A particular block of code not getting executed in Batch Job

(0) ShareShare
ReportReport
Posted on by 530

Hi,

I have written a batch job that picks up files from a folder and places them in another folder and updates the operation in a certain table. This batch job was running fine, but after adding a certain code to check if there aren't any files in folder using if condition the else part is not getting executed. 

The same code is running when tested in a simple job, but in a batch job it is not running.

I don't get any errors.

What might the problem be?

How to use debugger in a batch job in AX 2009?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Anton Venter Profile Picture
    20,345 Super User 2025 Season 2 on at

    Hi, have a look at this article. It is a walkthrough on how to debug a batch job.

  • 5400 Profile Picture
    7,162 on at

    are you using WinAPIServer or WinAPI for file checking.

    It should be WinAPIServer. can you send the code snippet with error.

  • Victor Tang Profile Picture
    47 on at

    Can you explain what code did you insert? 

    I've seen problem in the past when casting a .NET variable to an X++ variable, the code simply exited without any error. The quickest way to identify the problematic lines is to do print / infolog or commenting the section of code you suspect it's causing issue.

  • Osama Anjum Profile Picture
    530 on at

      /// Check the permission for CLR InterOp ///

       perm = new InterOpPermission(InteropKind::ClrInterop);

       perm.assert();

       try

       {

           //Traverse to the inbound folder and search file

           directoryInfo = new System.IO.DirectoryInfo(processFilePath );

           //Get list of only text files

           fileList = directoryInfo.GetFiles("*.xml");

           //Get the file count

           fileCount = fileList.get_Length();

           //One by one get each file

           for (i = 0; i < fileCount; i++)

           {

                file = fileList.GetValue(i);

               ///.Net method returns System.String need to marshal it to AX str data type

               processFileNamePath = file.get_FullName();

               proName = file.get_Name();

               axProcessName = proName;

               ////////// Process Start Time /////////////

               processStartTime = DateTimeUtil::getSystemDateTime();

               //////// Read the XML Document //////////////////

               doc = new XMLDocument();

              doc.load(processFileNamePath); at this point

               //////// Verify Document Structure ////////////////

               xmlError  = doc.parseError();

               if(xmlError && xmlError.errorCode()!= 0)

               {

                   throw error(strFmt("Error: %1",xmlError.reason()));

               }

               ///////////////// Parsing document contents //////////////////

               rootNode = doc.documentElement();

               ///Get all Vendor information from XML document tags (and remove it) ////////////

               NodeLedgerJournal = rootNode.selectSingleNode("//LedgerJournal"); at this point

               NodeCompany= rootNode.selectSingleNode("//Company");

  • Anton Venter Profile Picture
    20,345 Super User 2025 Season 2 on at

    Hi, have you tried to debug the code? What exactly is the problem?

  • Osama Anjum Profile Picture
    530 on at

    Request for the permission of type 'FileIOPermission' failed.

    (S)\Classes\FileIOPermission\demand

    (S)\Classes\XmlDocument\load

    (S)\Classes\RBT_Sampleprocess\run - line 54

    (S)\Classes\BatchRun\runJobStatic - line 63

    Whatever we do Error remains the same and also line 54 and line 63 appear even though there is no code on both the lines which is strange.

  • Osama Anjum Profile Picture
    530 on at

    This is the new Code but same error

    public void run()

    {

       Set permissionSet;

       perm = new InterOpPermission(InteropKind::ClrInterop);

       permissionSet = new Set(Types::Class);

       permissionSet.add(perm);

       errorFlag =0;

       ////////// File path from table ////////////

       select * from CompanySetupFolderAP;

    processFilePath=CompanySetupFolderAP.ProcessingPEPS;

    successFilePath=CompanySetupFolderAP.SuccessPEPS;

    errorFilePath =CompanySetupFolderAP.FailedPEPS;

       try

       {

           //Traverse to the inbound folder and search file

           directoryInfo = new System.IO.DirectoryInfo(processFilePath );

           //Get list of only text files

           fileList = directoryInfo.GetFiles(@"*.xml");

           //Get the file count

           fileCount = fileList.get_Length();

           //One by one get each file

           for (i = 0; i < fileCount; i++)

           {

    file = fileList.GetValue(i);

               ///.Net method returns System.String need to marshal it to AX str data type

               processName = file.get_FullName();

               proName = file.get_Name();

               axProcessName = proName;

               xx=processFilePath+"\\"+axProcessName;

               ////////// Process Start Time /////////////

               processStartTime = DateTimeUtil::getSystemDateTime();

               //////// Read the XML Document //////////////////

    filePerm = new FileIoPermission(xx,'r');

               permissionSet.add(filePerm);

               CodeAccessPermission::assertMultiple(permissionSet);

               doc = new XMLDocument();

               doc.load(xx);

               //////// Verify Document Structure ////////////////

               xmlError  = doc.parseError();

               if(xmlError && xmlError.errorCode()!= 0)

               {

                   throw error(strFmt("Error: %1",xmlError.reason()));

               }

               ///////////////// Parsing document contents //////////////////

               rootNode = doc.documentElement();

               /// Get all Vendor information from XML document tags (and remove it) ///

               NodeLedgerJournal = rootNode.selectSingleNode("//LedgerJournal");

               NodeCompany= rootNode.selectSingleNode("//Company");

               info(NodeCompany.text());

  • Community Member Profile Picture
    on at

    Have you tried running AX as an Administrator?

  • Community Member Profile Picture
    on at

    Try clearing cache in AX. Tools -> Cache -> AOT Element , etc. Clear cache of every object.

  • Suggested answer
    Anton Venter Profile Picture
    20,345 Super User 2025 Season 2 on at

    You have to assert File IO Permission on the server tier. See example below from MSDN.

    server static void main(Args args) 
    { 
        FileIoPermission _perm; 
        AsciiIo a; 
         
        _perm = new FileIoPermission("c:\\File.txt",'r'); 
        _perm.assert(); 
      
        // Invoke the protected API. 
        a = new AsciiIo("c:\\File.txt",'r'); 
    }

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