Skip to main content

Posts

Extracting labels from one labelfile to another

Recently I've had an assignment where we needed to "split up" a label file. The assignment was to: * read thorugh a label file. * If any labels were encountered with a number bigger then N these should be written to a new label file AND any references to these labels should be replaced in the code. So I exported all adjustments/code made to an .xpo file, and I took a copy of the label file. Then I wrote a class that did the "splitting up" of the label file and the search/replace of labels in the code (.xpo file). This made for use of an other entry in this blog, namely extracting text from a label, and for some interesting use of the TextBuffer object for search/replace. Here you can download the code. Use at own risk !!!

Making a form modal i X++

Put the following code in the GLOBAL class in a new method called setFormModal : static void setFormModal(int _thisHWND, boolean _bModal) {     DLL _winApiDLL;     DLLFunction _EnabledWindow;     DLLFunction _getTop;     DLLFunction _getNext;     DLLFunction _getParent;     void local_enableWHND(int _lHWND)     {         int lnextWnd;         lnextWnd = _getTop.call(_getParent.call(_lHWND));         while (lnextWnd)         {             if (lnextWnd != _lHWND)                 enabledWindow.call(lnextWnd, (!_bModal));       ...

Usefull debugging tip.

Useful debugging tip for tracking down the point where an exception is thrown. Some times it can be difficult to determine where an exception is thrown. Is it e.g. the validatefield method on a data source field on a form that throws the exception, or is it the validatewrite method on the data source or even the validatewrite method on the table it self. A useful trick to establish the point in the code where the exception is thrown, is to set a break point in line 11 in the method add in the class Info. You can find the Info class at the bottom at the class subtree in the AOT. This will stop execution and activate the debugger each time something is added to the infolog which is normally done when an exception is thrown.

Generating and running code RUNTIME.

How to create, compile and run code at RUNTIME. In some situations the need for making "generic" code arises. This is a little example that generates, compiles and runs code to delete the contents of table PBACustGroup. The code is made as a job, but transforming this code into a method on a class, and calling this method with parameters that allows it to identify the table, suddenly makes us able to delete several selected tables with very little code. It also allows for USER CONTROLLED actions, for example letting the user build a liste of tables he would like to delete, and then calling the code for each of these tables. static void TestGenericCode(Args _args) {     XppCompiler XppCompiler;     str code;     str tableName = "PBACustGroup"; // Here we define the name of the table ;     // A compile object is created     XppCompiler = new XppCompiler();     // Here th...

Extracting text from a label.

How to extract label text. This code shows how to get the text of a specific label for a specific module, for a specific language. This might come in handy when constructing a text to be printed on a report, that will more than one label text. // This code allows you to extract the label text of a specific label, for a specific module, // for a specific language // // Handy when constructing text fields containing many values and labels for printing // // str = labeltxt("gls",element.design().languageId(),200); str labeltxt(str _labelModule,LanguageId _languageId, LabelIdNum _labelNo) {     Label l;     l = new label(_languageId);     return l.extractString(l.name(_labelModule,_labelNo)); }

Getting field values of more than one data source when calling a menu item

When a form has called another form (the user has opened a form by clicking a button in another form), you sometimes need to get information from the calling form. The traditional way is to use element.args().record(); but this construction only allows for transferring ONE datasource at a time. Sometimes it would be nice to be able to transfer more than one datasource. This is an example of how that can be done. The code must be implemented in the init -method of the CALLED form: SysSetupFormRun s; APMObjectTable apmObjectTable; int dataSourceNo; // Determine value of fields APMProductId and APMModelId of APMObjectTable record // which might have been active at the time of pressing CTRL+F4 in the previous form // (CTRL+F4 on model field in form APMCustOverview) s = element.args().caller(); if (s) {     for (datasourceNo = 1; dataSourceNo     {         if (s.dataSource(dataSourceNo).cursor().TableId == tableN...

Tokenizing a string

How to tokenize a string. In Java you can use an object of the stringTokenizer class to break up a string in to tokens. Dynamics AX has a handy class called TextBuffer with which you can achieve a similar effect. This example shows how (the code is written as a job): static void Job7(Args _args) {     TextBuffer t;     t = new TextBuffer();     t.ignoreCase(true);     t.regularExpressions(false);     // Set the text to break in to tokens     t.setText("When I find myself in times of trouble, mother Mary comes to me, speaking words of wisdom, let it be. And in my hour of darkness she is standing right in front of me, speaking words of wisdom, let it be.");     // The delimiter to search for when finding tokens is space     while (t.nextToken(false," "))     {         info...