AX – D365FO – Executing external x++ code with RunBuf() function

With Runbuf() you can execute an external x++ code

To show how it works, I am going to use this function to execute code read from an external file. In this example I want to retrieve the Customer Name from a given Customer Account number.

First create an external file in c:\temp\ and call it “findCustomer.xpp” and insert this code inside :

CustTable findCustomer(CustAccount _accountNum)
{
    return CustTable::find(_accountNum);
}

Then create this job and execute it.

static void ExecuteCodeFromFile(Args _args)
{
   #File
   AsciiIo asciiIo = new AsciiIo(“c:\\temp\\findCustomer.xpp”,#io_read);
   XppCompiler xppCompiler = new XppCompiler();
   Source source;
   str line;
   CustTable custTable;

   if (asciiIo)
   {
      asciiIo.inFieldDelimiter(#delimiterEnter);
      [line] = asciiIo.read();
      while (asciiIo.status() == IO_Status::Ok)
      {
          source += #delimiterEnter;
          source += line;
          [line] = asciiIo.read();
      }
      if (!xppCompiler.compile(source))
          error (xppCompiler.errorText());

      custTable = runbuf(source,'4000');
      print CustTable.Name;
   }
   else
   {
      print “Could not open file”;
   }   
pause;
}

What’s happened?

First the file c:\temp\findCustomer.xpp is read into source.

Source is then compiled and if that goes ok it is executed.

‘4000’ is passed as a parameter simply by adding it to the runbuf() call.

I had trouble getting code compiled that I had written using notepad. As it turns out, the compiler does not accept the tab character. So if you are going to try this out, watch out for that.

2 responses to “AX – D365FO – Executing external x++ code with RunBuf() function”

  1. Andrea Avatar
    Andrea

    Great post Marco! Anyway pay attention: starting from D365FO X++ is no longer interpreted, so your code can’t work for D365FO

    Like

  2. Mags Avatar
    Mags

    Agree with Andrea, this would only work up to 2012,

    In D365FO, XppCompiler no longer exists and is part of deprecated features, runbuf is still available but seems to be doing nothing.

    Like

Leave a comment