Custom code snippets in AX 7

I already explained how to use code snippets and a problem you might run into, but what if you want some additional snippets?

The good news is that you can easily build new snippets by yourself. Let’s use a concrete example.

Let’s say that you often write queries with the query framework and execute them with QueryRun and you’re tired of writing the boilerplate code. A snippet would help.

First of all, create a text file and name it queryRun.snippet. Then fill it with the following content:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>queryRun</Title>
      <Description>Creates a query with a single table, executes in and fetches records.</Description>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>table</ID>
          <ToolTip>Table name</ToolTip>
          <Default>MyTable</Default>
        </Literal>
        <Literal Editable="true">
          <ID>buffer</ID>
          <ToolTip>Table variable name</ToolTip>
          <Default>myTable</Default>
        </Literal>
      </Declarations>
      <Code Language="X++"><![CDATA[Query query = new Query();
QueryBuildDataSource qbds = query.addDataSource(tableNum($table$));
QueryRun queryRun;
$table$ $buffer$;
 
queryRun = new QueryRun(query);
while (queryRun.next())
{
    $buffer$ = queryRun.get(tableNum($table$));$end$
}]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

In short, the header describes the snippet and can provide additional information. Literals define places that can be modified after putting the snippet into code editor, such as the name of the table. The code structure will be the same in all cases, but you’ll likely want to use various tables, therefore the table name is a good candidate for a literal.

The last section, Code, contains the actual source code. Literals are represented by their IDs surrounded by dollar signs (by default).

You can learn more about the syntax in Code Snippets Schema Reference.

When you have your snippet ready, open Visual Studio, go to Tools > Code Snippet Manager, change Language to X++ and click Import.

Import snippet

Select the snippet file and put it into Snippets folder.

Import snippet location

Confirm dialogs and look at snippets in code editor – you should see your new queryRun there:

queryRun selection

Use the snippet and you should get the following code, where you’ll set the table type and the name of the variable.

queryRunResult

Note that the problem with replacements applies here, therefore you have to be careful where you use the snippet. It seems that the while loop inside the snippet triggers code indentation (although I haven’t investigated it in detail), therefore you should use the snippet in some invalid context, such as directly in class declaration or in a method with a compilation error. This workaround won’t be needed in future, after the bug gets fixed.

After typing in just two words (names of the table and the variable), you have a simple query ready to run and you can focus on your actual business logic:

query worker

Another note: If you follow this particular example with worker, you’ll notice that when you type worker and press Tab, code completion kicks in and replaces worker with WorkerSessionType. To get the intended result, you have to press Esc instead of Tab.

When you write code, think about what you do often and consider turning it into a snippet, so you don’t have to write it by hand again. You also don’t have to build snippets just for yourself – you can share them with your team or even the whole world.

Editor scripts in MorphX allow any arbitrary code, which clearly isn’t the goal of code snippets in Visual Studio. But it doesn’t mean that you can’t do that – if you need something more complex, such as generating a whole class based on input from a dialog, you can create a Visual Studio extension.

5 Comments

  1. Martin,

    Thanks for this post!

    Do you have an example of how a VS extension would create the code artifact? I cannot figure out how to add a method to a class beyond putting the generated X++ in the clipboard.
    I was looking for something like Microsoft.Dynamics.Framework.Tools.MetaModel.Automation.Classes.IClassItem.Add(Method);
    but am not seeing/understanding how the automation classes ‘create’ elements.

    Best Regards,

    • I might write a blog post about VS extensions, but I’m not working on any in this moment. If you have a particular question, I suggest you open it in a discussion forum (I contribute to community.dynamics.com and dynamicsuser.net), because it doesn’t really belongs to this blog post about code snippets.

  2. Hi Martin,

    Excellent post. I am trying to create a code snippet for putting code comment around my code modifications. Do you have any idea how can I insert current userId and datetime within the snippet ? We used to do this with EditorScripts class in AX 2012 and AX 2009. Is there any alternative available in VS ?

    Thanks,
    Baber.

    • I don’t think it’s possible with X++ code snippets; you would have to create a VS extension. Nevertheless in my opinion such things don’t belong to source code, because they’re already automatically maintained by version control systems and anything cluttering source code just makes it worse (less readable), not better.

Comments are closed.