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.

One thought on “AX – D365FO – Executing external x++ code with RunBuf() function

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s