Autocomplete text in MorphX Editor

One useful tool in Dynamics AX MorphX Editor is the Autocomplete function. For example, if you type if and click the Tab Button the if statement autocompletes.

ifScript

Nifty! But what if you want to add your own Autocomplete function: let’s say if you type info and click the Tab Button you want info(StrFmt(“%1”, )); to be inserted.

 

Just add following method to class xppSource

Source info(Source _variable = '')
{
 
    source += @'info(StrFmt("%1", ));';
 
    source += '\n';
    return source;
}

 

Add the following method to class EditorScripts

 

public void template_flow_if(Editor editor)
{
    xppSource xppSource = new xppSource(editor.columnNo());
 
    int currentLine = editor.currentLineNo();
 
    int currentCol = editor.columnNo();
 
    Source template = xppSource.if();
 
 
 
    editor.insertLines(template);
 
    editor.gotoLine(currentLine+1);
 
    editor.gotoCol(currentCol+5);
 
}

 

 

The result will look like this:

infoScript

Hope this helped.

Debugging Batch Jobs

Show me the developer who doesn’t love breakpoints? They are the best thing since sliced bread and double monitors.

So how can one set a breakpoint in managed code? It’s actually not that hard.

The problem here is that the standard AX debugger can only debug interpreted code. But since we have CLR code we can use our trusty Visual Studio.

Here are the steps:

1.  Be sure breakpoints are enabled on AOS configuration
2.  Start Microsoft Visual Studio with admin rights
3.  Go to Debug and choose Attach to process

Attach to process

Attach to process

4.  Select the AX32Serv.exe process (make sure Show processes for all users and Show processes in all sessions are checked) and click Attach.

AX32Serv.exe

AX32Serv.exe

5.  A warning will pop up about a “suspicious” process. This is our process so just select Attach again.

6.  Now just go to the batch job you want to run and set a breakpoint.

Breakpoint

Breakpoint

7.  From Dynamics AX run the Job in batch

In case you get a breakpoint that is not red and when the cursor is on top of it it displays a message

“The breakpoint will not currently be hit. No symbols have been loaded for this document.”

don’t worry. If the xpps are loaded correctly and the xppil lib exists you will hit the breakpoint.

If not, just generate a fll CIL and/or restart the AX service.

Empty Breakpoint

Empty Breakpoint

 

Hope you enjoyed my post and in case you have a question don’t hesitate to leave a comment!

Create a new Job using EditorScripts Class

One thing that I like to do is copy a segment of code in a Job and debug it there. It’s like a safe place, where you can test/debug the code, without worrying that some unknown variables might influence the process. And since I use it quite often, I wrote a nifty function, to automate it a bit by adding this small method to the EditorScripts Class. In case you don’t know what the EditorSpripts does and how it works here is a previous post explaining the Class

 

Here is what it does: Let’s say you found a select statement that you would like to run, without the rest of the code in the method. You just select the code you are interested in, click on the createNewJob under Scripts -> WorkFlow, add a new for the new Job and voila! your new Job is ready.

newJobTool

 

Here is the code

 

public void WorkFlow_createNewJob(Editor editor)
{
    //dialog variables
    Dialog dialog;
    DialogField jobName;
    Str20 strJobName;
    //editor variables needed for selection
    Editor newJobEditor;
    str selectedText;
    //variables needed to create the new Job
    TreeNode treeNode;
    xInfo xInfo = new xInfo();
    //creating the dialog for the name of the job
    dialog = new Dialog("WorkFlow - Create Job");
    jobName = dialog.addField("str20");
 
    if (dialog.run())
    {
        strJobName = jobName.Value();
    }
 
    //getting the selected text from the editor
    selectedText += EditorScripts::getSelectedText(editor);
 
    //creating the job
    treeNode = xInfo.rootNode();
    treeNode = treeNode.AOTfindChild("Jobs");
    treeNode.AOTadd(strJobName);
 
    //treeNode holds our freshly made Job
    treeNode = treeNode.AOTfindChild(strJobName);
    //opens the job
    treeNode.AOTedit(3,1);
 
    //newJobEditor is the editor of the created Job
    newJobEditor = Editor::open('\Jobs\\' + strJobName);
 
    //inserting the selected text in the new Job
    newJobEditor.insertString(selectedText);
}

Hope you enjoyed my post and in case you have a question don’t hesitate to leave a comment.

Reducing Repetitive Typing with the EditorScripts Class

One thing I really hate is repetitive typing, especially when I am trying to test something via a Job or debugging. One easy way to debug your code is by displaying the information you want on the screen via an infolog.

So imagine this scenario: you’ve just finished writing that nasty SQL statement, with 2-3 joints thrown in there (you know, for good measure), and all you want to do is look at the values it returns from the database.

If you are anything like me, you will write something like this

 info(StrFmt(‘%1, ‘%2, %3’, variable1, variable2, variable3));

It’s not that bad, writing that statement out every time, but if you use this method EVERY DAY (like I do), you will get sick of it.

So I wrote this small method to add to the EditorScripts Class. In case you don’t know what the EditorSpripts does and how it works here is a previous post explaining the Class.

Here is what it does: just write the variables one after another, highlight the text, right click, select the function and voila! ready to debug!

infoMethodExample

Here is the code

void WorkFlow_infoStrFmt(Editor e)
{
    str             beginingText, selectedText, endingText, finalText;
    int             i, j;
    container       cont;
    ;
 
    //the beginning and ending of each info statement
    beginingText = 'info(strFmt(\'';
    endingText ='));';
    j = 1;
 
    //getting the highlighted text (as a string)
    //and saving it in a container
    selectedText = EditorScripts::getSelectedText(e);
    cont = str2con(selectedText, ' ');
 
    //for each value in the container
    for(i = 1; i <= conLen(cont); i++)
    {
        if(conPeek(cont,i) !='')
        {
            //we add a %1 or %2 or %3 etc.
            beginingText +=  strFmt('%%1 ', j);
            //variable j keeps track of how many variables we have in the container
            j++;
        }
    }
 
    //here we add the comma between the string and the values in the StrFmt method
    beginingText += '\', ';
    j = 1;
 
    //for each value in the container
    for(i = 1; i <= conLen(cont); i++)
    {
        if(conPeek(cont,i) !='')
        {
            //we add all the variables in the StrFmt method
            beginingText +=  strFmt('%1, ', conPeek(cont, i));
        }
    }
 
    //deleting the last 3 characters from the string (a comma, a space and a return)
    beginingText = strDel(beginingText, strLen(beginingText)-3, strLen(beginingText));
 
    //putting the 2 strings together
    finalText = beginingText + endingText;
 
    //positioning the cursor in the editor
    //adding the final string in the editor
    //and deselecting the text
    e.gotoCol(strlen(e.currentLine()) + 2);
    e.insertString(strFmt('%1', finalText));
    e.unmark();
}

Hope you enjoyed my post and in case you have a question don’t hesitate to leave a comment.

Customizing MorphX using EditorScripts Class

Most IDEs have some cool build-in tools to help you write faster and more efficient code, and MorphX is no different. In case you hate repetitive typing as much as I do, here is a quick tweak that can help.

There are a couple of ways to add functionality to the MorphX Editor, and the easiest and fastest way is by adding a new method to the Class EditorScripts in the AOT.

Every new Method in the class can be called from the Editor by Right Click > Scripts.

MSDYNAX EditorScripts Comment

So let’s write a very basic Method, just to get an idea of what we can do with this Class. This method will comment the line in which the cursor is, or the highlighted lines in the code.

In the AOT go to Classes-EditorScripts and create a new Method which will look something like this:

private void method1()
{
}

Please note the name convention: pcoNewNode is the new node under the Scripts menu and everything after the underscore is the node under pcoNewNode. The parameter _editor is just our Editor instance.

First we want to get where the line number where the code highlighting starts and where it ends. Next, we deselect the highlighted text and for each line we write in the first position the string “//”.

public void pcoNewNode_CommentScript(Editor _editor)
{
    //getting the line number of the start and end of the highlithing
    int startLine = _editor.selectionStartLine();
    int endLine   = _editor.selectionEndLine();
    int i;
 
    //unmark the highlighted text
    _editor.unmark();
 
    //for every line
    for (i = startLine; i <= endLine; i++)
    {
        //put the cursor on that line number
        _editor.gotoLine(i);
        //put it in the first position (column) of that line
        _editor.gotoCol(1);
        //insert the string
        _editor.insertString('//');
    }
}

This is a very easy and fun way of customizing MorphX, because all you have to do is write good old  X++ code.