Tuesday, September 24, 2019

Maintain Fast SQL Operations avoid set based operation to fall back to row by row operation using skip methods in Dynamics ax

Sometimes when importing large number of records (Eg. millions of lines), the quickest way is to use:

  • insert_recordset
  • update_recordset
  • delete_from

But these set base operation will roll back to row-by-row operation when either of the following condition is true:
  • it is not an SQL table (Eg. temporary table)
  • Database log is enabled for the table
  • Alert is setup for this table
  • Record level security is enabled for the table
  • AOSValidation method is overwritten
  • when using insert_recordset
    • the .insert() method is overwritten
  • when using update_recordset
    • the .update() method is overwritten
  • when using delete_from
    • the .delete() method is overwritten
    • DeleteAction is defined

To counter this, you can call a number of skip* methods:

  • common.skipDataMethods(true) will skip the insert/update/delete methods
  • common.skipDeleteMethod(true) will skip the delete method
  • common.skipDeleteActions(true) will not execute the delete actions
  • common.skipEvents(true) if alert is setup

image

see: https://docs.microsoft.com/en-us/dynamicsax-2012/developer/maintain-fast-sql-operations

I saw an example in this post: https://dynamicsuser.net/ax/f/developers/76285/update_recordset

public server static void MyCustomMethod(SalesLine _salesLine)
{
    salesLine           salesLineUpd;
    InventDim           inventDim;
    
  if ( _salesLine.MyField )
  {
    inventDim = _salesLine.inventDim();

    salesLineUpd.skipDataMethods( true );

    ttsBegin;

    update_recordSet salesLineUpd
    setting PER_ChairSerialNo = inventDim.inventSerialId
    where salesLineUpd.SalesId == _salesLine.SalesId
    && salesLineUpd.RecId != _salesLine.RecId; // update every other line with serial id

    ttsCommit;
  }


I also saw in this other post some suggestions regarding database log
http://www.artofcreation.be/2014/08/11/what-you-should-know-about-database-logging-functional-impact/

If you really want to activate database logging but you have code that need to do a set-based operation, you can get around this issue by using the skipDatabaseLog method in combination with the other skip* methods.

However, in my opinion it is better not to use database log in the first place. So these are my recommendations about database logging:

  • Do not use it.
  • If you do use it, make sure it is for a good reason and document why.
  • Do not use database logging because you do not trust your employees or as a form of “security”.
  • When activating the database log for a table, pay close attention to the TableGroup property of the table. It is fairly safe to activate the database log on tables with table group Main, Group and Parameter. Activating it for other groups such as Transaction, TransactionHeader, TransactionLine, WorksheetHeader and WorksheetLine is usually bad.
  • If you are a consultant, capture the need for database logging in the analysis phaseand set this up in your DEV/TST/ACC/… environments as early as possible.
  • Do not simply activate database logging in a production environment and expect everything to go well, test it first in an other environment as if it were a code change

Monday, September 9, 2019

Simulated Multiple Inheritance fox x++ in Dynamics Ax

We all know that in X++, a class can only extend one class; multiple inheritance is not supported in X++ and to overcome that we can use Interfaces. What I never saw is an example of multiple inheritance in X++. Instead I saw someone saying it’s a bug! I don’t know if was serious or a was making a joke…

Let’s start making things clear:

In Multiple inheritance, one class can have more than one superclass and inherit features from all its parent classes. As shown in the below diagram, class C inherits the features of class A and B.

Most recent languages like C# support only simple inheritance. They don't have multiple inheritance because their designers had to choose between have it in and have all the problems it comes with, or get it out of the language putting away all those problems, and introduce a versatile and less problematic substitute like interfaces and interface inheritance.
Multiple inheritance has a pathological problem:

Imagine, we want to have a class Child which inherits from Parent classes ParentA and ParentB. Both classes have the same methods MethodA and MethodB.
Now, when we instantiate the Class Child then calling MethodA will confuse the compiler that does not know from which class MethodA should be called.

The traditional way to emulate multiple inheritance with interface inheritance.

Let us see how to simulate multiple Inheritance in X++

image

first we create the interfaces

interface EF_IMultipleInheritaceBaseClass1
{
}
public void x()
{
}
public void y()
{
}

interface EF_IMultipleInheritaceBaseClass2
{
}
public void z()
{
}

next we can create two classes that implement those interfaces

class EF_MultipleInheritaceBaseClass1 implements EF_IMultipleInheritaceBaseClass1
{
}
public void x()
{
    info(funcName());//it shows the class and method name
}
public void y()
{
    info(funcName());
}

class EF_MultipleInheritaceBaseClass2 implements EF_IMultipleInheritaceBaseClass2
{
}
public void z()
{
    info(funcName());
}

now we are ready to create our abstract class that implements the two interfaces

abstract class EF_MultipleInheritaceBaseClass1And2 implements EF_IMultipleInheritaceBaseClass1, EF_IMultipleInheritaceBaseClass2
{
    EF_IMultipleInheritaceBaseClass1 class1;
    EF_IMultipleInheritaceBaseClass2 class2;
}
protected void new()
{
    class1 = classFactory.createClass(classNum(EF_MultipleInheritaceBaseClass1));
    class2 = classFactory.createClass(classNum(EF_MultipleInheritaceBaseClass2));
}
public void x()
{
    class1.x();
}
public void y()
{
    class1.y();
}
public void z()
{
    class2.z();
}

finally we can extend our abstract class and bingo, we simulated multiple inheritance:

class EF_MultipleInheritaceClass1And2 extends EF_MultipleInheritaceBaseClass1And2
{
}
public static EF_IMultipleInheritaceClass1And2 construct()
{
    return new EF_IMultipleInheritaceClass1And2();
}
public static void main(Args _args)
{
    EF_IMultipleInheritaceClass1And2 class1And2;
    class1And2 = EF_IMultipleInheritaceClass1And2::construct();
    class1And2.x();
    class1And2.y();
    class1And2.z();
}

Convert a Dynamics Enum to a C# generic collection

When you need to consume a Dynamics Ax Enum type in a .Net project you should make your project independent from the internal structure of Dynamics and certainly you do not want to create a static copy of your Dynamics Enum in your .Net project.

In order to achieve that I taught to expose the Dynamics Enum as a generic List.

To do that and adhere to the DRY principle (do not repeat yourself) I made an extension method in C# that converts any Enum in a generic list.

first you need to create a POCO class, a data transfer object

namespace MydDynamicsIntegration.Models
{
    public class AxEnumDefinition
    {
        public int Value { get; set; }
        public string Name { get; set; }
        public string Label { get; set; }
    }
}

now here the extension method

using System;
using System.Collections.Generic;
using MydDynamicsIntegration.Models;

namespace MydDynamicsIntegration.DynamicsCommon
{
    public static class AxGetValuesExtensions
    {
        public static List<AxEnumDefinition> FromAxToColl<T>(this T eEnum) where T : struct, IConvertible
        {
            var enumDefinitions = new List<AxEnumDefinition>();

            Type t = typeof(T);

            if (!t.IsEnum)
                throw new ArgumentException("T must be an enumerated type");

            try
            {
                SysDictEnum sysDictEnum = SysDictEnum.newName(t.Name);

                for (int i = 0; i < sysDictEnum.values(); i++)
                {
                    enumDefinitions.Add(new AxEnumDefinition()
                    {
                        Value = sysDictEnum.index2Value(i),
                        Name = sysDictEnum.index2Name(i),
                        Label = sysDictEnum.index2Label(i)
                    });
                }
            }
            catch
            {
                //
            }

            return enumDefinitions;
        }
    }
}

now you can use it like this

use example: 
return new HcmDiscussionStatus().FromAxToColl();
OR to a specific value
Status = new AxEnumDefinition()
   {
      Value = Global.enum2int(discussion.status),
      Name = Global.enum2Value(discussion.status),
      Label = discussion.status.FromAxToColl().Where(x => x.Value == Global.enum2int(discussion.status)).Select(x => x.Label).FirstOrDefault()
   }
OR 
Status = discussion.status.FromAxToColl().FirstOrDefault(x => x.Value == Global.enum2int(discussion.status))

Thursday, September 5, 2019

Attach documents in Dynamics Ax programmatically in x++ code. different options

I had to do some custom development to upload/Attach document in Ax programmatically from a webApi. So I did some research on what is the best way to it.

here I share the different options I found to do it.

OPTION 1
USE \Classes\DocuActionArchive\add (it extends DocuActionFile)

NOTE, is the preferred way, but doesn't let you customize anything. the description of the file is the file name

static void JobEF_DocumentInsert(Args _args)
{
    DocuType docuType;
    DocuTypeId docuTypeId;
    DocuRef docuRef;
    DocuValue docuValue;
    DocuActionArchive archive;

    Filename filename;

    HcmDiscussion hcmDiscussion;

    docuTypeId = 'AppraisalEnrico';
    hcmDiscussion = HcmDiscussion::findByDiscussionWorker('xx', HcmWorker::findByPersonnelNumber('xx').RecId);

    filename = @'\\...\Myfile.PNG';

    if ( ! hcmDiscussion ) return;

    docuType = DocuType::find(docuTypeId);

    ttsBegin;

    docuRef.RefCompanyId = hcmDiscussion.DataAreaId;
    docuRef.RefTableId   = hcmDiscussion.TableId;
    docuRef.RefRecId     = hcmDiscussion.RecId;
    docuRef.TypeId       = docuType.TypeId;
    docuRef.insert();

    archive = new DocuActionArchive();

    archive.add(docuRef, filename);

    ttsCommit;
}

OPTION 2
USE \Classes\DocuActionFile\insertDocuValue

Note: seems quite good option. you can't use this method directly as it is an abstract class, use \Classes\DocuActionArchive\

static void JobEF_DocumentInsert2(Args _args)
{
    DocuType docuType;
    DocuTypeId docuTypeId;
    DocuRef docuRef;
    DocuValue docuValue;
    DocuActionArchive archive;

    Filename filename;

    HcmDiscussion hcmDiscussion;

    docuTypeId = 'AppraisalEnrico';
    hcmDiscussion = HcmDiscussion::findByDiscussionWorker('xx', HcmWorker::findByPersonnelNumber('xx').RecId);

    filename = @'\\...\Myfile.PNG';

    if ( ! hcmDiscussion ) return;

    docuType = DocuType::find(docuTypeId);

    ttsBegin;

    docuRef.RefCompanyId = hcmDiscussion.DataAreaId;
    docuRef.RefTableId   = hcmDiscussion.TableId;
    docuRef.RefRecId     = hcmDiscussion.RecId;
    docuRef.TypeId       = docuType.TypeId;
    docuRef.Name         = strFmt('prova - %1', Docu::getFileName(filename));
    docuRef.insert();

    archive = new DocuActionArchive();

    archive.insertDocuValue(docuRef, filename);

    ttsCommit;
}

OPTION 3
NOTE: is using Docu::insertFile. more complex.

static void JobEF_DocumentInsert1(Args _args)
{
    DocuType docuType;
    DocuTypeId docuTypeId;
    DocuRef docuRef;
    DocuValue docuValue;
    DocuActionArchive archive;

    Filename filename;
    DocuValueFile file;
    BinData binData;
    boolean fileLocked;

    HcmDiscussion hcmDiscussion;
    #File
    
    //done like: \Classes\TrvImportReceiptsBatch\processFolder
    DocuValueFile getDocuValueFile(FileName _fullPathOfFile)
    {
        binData = new BinData();

        new FileIOPermission(_fullPathOfFile,'r').assert();
        // BP Deviation documented
        binData.loadFile(_fullPathOfFile);
        CodeAccessPermission::revertAssert();
        return binData.getData();
    }
    
    //done like: \Data Dictionary\Tables\DocuValue\Methods\writeDocuValue
    DocuValueFile getDocuValueFile1(FileName _fullPathOfFile)
    {
        binData = new BinData();
        if (isRunningOnServer())
        {
            // Assert permission and get the temp filename
            new FileIOPermission(_fullPathOfFile,#io_read).assert();
            // BP deviation documented
            fileLocked = WinApiServer::fileLocked(_fullPathOfFile);
            CodeAccessPermission::revertAssert();
        }
        else
        {
            // BP deviation documented
            fileLocked = WinApi::fileLocked(_fullPathOfFile);
        }

        // Insert to database
         if (fileLocked)
         {
            info("@SYS72783");
         }
        else
        {
            // LoadFile demands read permission on the file
            new FileIOPermission(_fullPathOfFile, #io_read).assert();
            // BP deviation documented
            if (binData.loadFile(_fullPathOfFile)) //only works if file not locked
            {
                file = binData.getData();
            }
            CodeAccessPermission::revertAssert();
        }
        return file;
    }
    
    docuTypeId = 'AppraisalEnrico';
    hcmDiscussion = HcmDiscussion::findByDiscussionWorker('123', HcmWorker::findByPersonnelNumber('123').RecId);

    filename = @'\\...\Myfile.PNG';

    file = getDocuValueFile(filename);

    if ( ! hcmDiscussion ) return;

    docuType = DocuType::find(docuTypeId);

    ttsBegin;

    docuRef.RefCompanyId = hcmDiscussion.DataAreaId;
    docuRef.RefTableId   = hcmDiscussion.TableId;
    docuRef.RefRecId     = hcmDiscussion.RecId;
    docuRef.TypeId       = docuType.TypeId;

    docuValue = Docu::insertFile(docuRef, filename, file, true);
    docuRef.ValueRecId = docuValue.RecId;
    docuRef.Name = 'mia prova';

    docuRef.insert();

    ttsCommit;
}

OPTION 4
USE \Classes\DocumentFileHelper\attachDocumentAsUser

SEE EXAMPLE: \Data Dictionary\Tables\RetailDiscountCode\Methods\createBarCodeImage

private void createBarCodeImage()
{
    #define.AttachmentName('BarcodeImage.jpg')

    RetailSharedParameters sharedParams;
    BarcodeSetup barcodeSetup;
    str base64ImageString;
    container attachDocumentParams;
    DocuRef DocuRefTable;
    RecId docuRefRecId;

    if (this.BarCode)
    {
        // Get selected barcode
        select firstOnly BarcodeSetupId from sharedParams
        join RetailBarcodeMask, fontName, fontSize from barcodeSetup
        where sharedParams.barcodeSetupId == barcodeSetup.barcodeSetupId;

        // Get base64 string of barcode image.
        base64ImageString = RetailBarcodeManagement::getBarcodeJpegImageAsBase64String(this.BarCode, barcodeSetup.fontName, barcodeSetup.fontSize);

        // Attach barcode image to the discount code.
        attachDocumentParams = [0, base64ImageString, #AttachmentName, '', DateTimeUtil::utcNow(), this.TableId, this.RecId, curext(), DocuType::typeFile()];
        [docuRefRecId] = DocumentFileHelper::attachDocumentAsUser(attachDocumentParams);

        // Make attached image external.
        select forUpdate DocuRefTable where DocuRefTable.RecId == docuRefRecId;
        // set document description to 'Bar code'
        DocuRefTable.Name = "@RET3053";
        DocuRefTable.Restriction = DocuRestriction::External;
        DocuRefTable.update();
    }
}

OR see \Classes\TrvUnreconciledExpenseService\createAttachments

OR \Classes\TrvReceiptService\createReceiptHelper

private DocuRef createReceiptHelper(RefTableId _tableId, RefRecId _recId, DataAreaId _dataAreaId, str _name, str _documentName, str _documentContents)
{
    Filename documentName;
    FilePath documentUrl;
    RefRecId contentType;
    str documentFile;
    DocumentFileReceiveDate receiveDate;
    DocuTypeId docuTypeId;
    TrvReceiptsHelper trvReceiptsHelper = new TrvReceiptsHelper();
    container args;
    RecId createdRecId;
    DocuRef docuRef;

    [documentName, documentUrl, contentType, receiveDate, documentFile] = DocumentFileHelper::getValidateUnpackedDocumentFileData(
                                                                _documentName, "", "",
                                                                DateTimeUtil::utcNow(), _documentContents);

    docuTypeId = trvReceiptsHelper.getDocuTypeId();

    args = [contentType, documentFile, documentName, documentUrl, receiveDate, _tableId, _recId, _dataAreaId, docuTypeId];

    [createdRecId] = DocumentFileHelper::attachDocumentAsUser(args);

    if (createdRecId != 0)
    {
        docuRef = DocuRef::findRecId(createdRecId, true);

        if (_name != '')
        {
            docuRef.Name = _name;
        }
        else
        {
            docuRef.Name = "@SYS138348";
        }
        docuRef.update();
    }

    return docuRef;
}

NOTE to get the documentContents see: \Classes\TrvReceiptService\getDocumentContents

.... 
    // Get the receipt contents and serialize them.
    binData = DOCommonDocuUtils::GetDocuContent(docuRef);
    if (binData && conLen(binData.getData()) > 0)
    {
        documentContents = binData.base64Encode();
    }

    return documentContents;


------------------------------------------------

Finally I didn’t use none of them but I ended extending an existing class which gives me the advantage to code less (always use existing methods!) and also to upload all the files in a directory in one go. So from the WebApi I create a shared folder where I upload all the documents I want to be attached to an Ax record and my extended class is responsible to Attach the document.
When the process ends Ax deletes the shared folder.

class MYD_HcmDiscussionAttachmentHelper extends TrvImportReceiptsBatch
{
    HcmDiscussion hcmDiscussion;
    DocuTypeId docuTypeId;
}

//for trials only
public static void main(Args _args)
{
    FilePath dirPath;
    HcmDiscussion hcmDiscussion;

    MYD_HcmDiscussionAttachmentHelper cl = new MYD_HcmDiscussionAttachmentHelper();

    dirPath = @'\\...\temp\000198';
    hcmDiscussion = HcmDiscussion::findByDiscussionWorker('000198', HcmWorker::findByPersonnelNumber('123').RecId);

    cl.parmDirPath(dirPath);
    cl.parmHcmDiscussion(hcmDiscussion);
    cl.parmDocuTypeId('AppraisalEnrico');
    cl.run();
}

boolean importFile(Filename _filename, DocuValueFile _file)
{
    DocuRef docuRef;
    DocuValue docuValue;
    boolean ret = false;

    if (hcmDiscussion)
    {
        ttsbegin;
            docuRef.RefCompanyId = hcmDiscussion.DataAreaId;
            docuRef.RefTableId   = hcmDiscussion.TableId;
            docuRef.RefRecId     = hcmDiscussion.RecId;
            docuRef.TypeId       = docuTypeId;

            docuValue = Docu::insertFile(docuRef, _filename, _file, true);
            docuRef.ValueRecId = docuValue.RecId;
            docuRef.Name = System.IO.Path::GetFileNameWithoutExtension(_filename);

            docuRef.insert();
        ttscommit;
        ret = true;
    }

    return ret;
}

public DocuTypeId parmDocuTypeId(DocuTypeId _docuTypeId = docuTypeId)
{
    docuTypeId = _docuTypeId;

    return docuTypeId;
}

public HcmDiscussion parmHcmDiscussion(HcmDiscussion _hcmDiscussion = hcmDiscussion)
{
    hcmDiscussion = _hcmDiscussion;

    return hcmDiscussion;
}

public void run()
{
    boolean result = true;
    try
    {
        super();
    }
    catch
    {
        result = false;
    }

    if (result)
    {
        new FileIOPermission(dirPath,'W').assert();
        System.IO.Directory::Delete(dirPath, true);
        CodeAccessPermission::revertAssert();
    }
}

Wednesday, September 4, 2019

utility method to get all table fields their Extended data type or Enum type and add them as parm methods in a class programmatically

I had to get the list of fields from a table with quite many fields to be used as properties in a class. quite and error prone task.

for example for CustTable you should have to do:

classDelaration
{
   CustAccount accountNum;
    CustInvoiceAccount invoiceAccount;
    CustGroupId custGroup;
   ….
}

I made this job that using a bit reflection does the work for you:

static void JobEF_TableGetFieldsListForProperties(Args _args)
{
    TableID tableID;
    DictTable dictTable;
    TableName tableName;
    FieldId fieldId;
    DictField dictField, dictFieldFrom;
    Common buffer;
    TableScope scope = TableScope::CurrentTableOnly;
    ExtendedDataTypeName fieldType;
    EnumName fieldEnum;
    FieldName fieldName;
    LabelString fieldTypeLabel, fieldEnumLabel;
    IdentifierName fieldDataType;
    TextBuffer txtb = new TextBuffer();
    int i;

    tableName = tableStr(CustTable);

    tableID = tablename2id(tableName);
    dictTable = new DictTable(tableID);

    if (!dictTable)
        throw error('invalid table');

    buffer = dictTable.makeRecord();

    fieldId   = dictTable.fieldNext(0, scope);

    while (fieldId && ! isSysId(fieldId))
    {
        dictField   = new DictField(tableID, fieldId);

        fieldType = Global::extendedTypeId2name(dictField.typeId());
        fieldEnum = Global::enumId2Name(dictField.enumId());
        fieldName = dictField.name();
        fieldTypeLabel = Global::extendedTypeId2pname(dictField.typeId());
        fieldEnumLabel = Global::enumId2pname(dictField.enumId());

        fieldDataType = fieldType ? fieldType : fieldEnum;

        if (i)
            txtb.appendText('\n');

        txtb.appendText(strFmt('%1 %2;', fieldDataType, MYD_Functions::stringLowerCaseFirst(fieldName)));

        i++;

        fieldId         = dictTable.fieldNext(fieldId, scope);
    }

    info(txtb.getText());
}

I have a helper class where I have all my utility methods, one is the one used in this code:

public static str stringLowerCaseFirst(str string)
{
    System.String myString, firstLetter;
    str firstLetterLwr, stringEnd, stringNew;

    if (strLen(string) < 2)
        return strLwr(string);

    myString = string;

    firstLetter = myString.Substring(0, 1);
    stringEnd = myString.Substring(1);
    firstLetterLwr = firstLetter.ToLowerInvariant();
    stringNew = strFmt('%1%2', firstLetterLwr, stringEnd);

    return stringNew;
    
    /*
    NOTE:
    you can do the same like \Classes\xppSource\parmMethod
    stringNew = strLwr(subStr(string,1,1))+subStr(string,2,strLen(string));
    I don't know which one is best. I come form C#
    */
}

I took it a step further and this other Job also auto generate the parm methods in a class for each table field.

To complete this task I used two Ax Foundation classes:

  • ClassBuild
  • XppSource

ClassBuild contains methods to programmatically create a class and methods

XppSource is the same class which you use behind the scene when you use shorcuts in the code editor or when you select a template under the script button in the editor toolbar.

static void JobEF_ClassMakeDataContractPerTblFields(Args _args)
{
    TableID tableID;
    DictTable dictTable;
    TableName tableName;
    FieldId fieldId;
    DictField dictField, dictFieldFrom;
    Common buffer;
    TableScope scope = TableScope::CurrentTableOnly;
    ExtendedDataTypeName fieldType;
    EnumName fieldEnum;
    FieldName fieldName;
    LabelString fieldTypeLabel, fieldEnumLabel;
    IdentifierName fieldDataType;
    TextBuffer txtb = new TextBuffer();
    int i;
    ClassName className;
    Source source;
    ClassBuild classBuild;
    XppSource xppSource;
    MethodName paramMethodName;
    str parmPrefixToFieldName;
    boolean addDataContractAttribute;

    //SET the table you want to get all fields from
    tableName = tableStr(CustTable);
    //SET the class you want the parm methods to be created
    className = classStr(MYDataContractClass);
    //SET optionally a prefix to all Table field names for the parm
    parmPrefixToFieldName = '';
    //SET if you want to add the [DataContractAttribute] attribute
    addDataContractAttribute = true;
    
    /*
    the hob will run and create a parm for each field of the table in the class.
    From the info log copy the parmeters and paste in the class declaration.
    */

    tableID = tablename2id(tableName);
    dictTable = new DictTable(tableID);


    if (!dictTable)
        throw error('invalid table');

    buffer = dictTable.makeRecord();

    fieldId   = dictTable.fieldNext(0, scope);

    while (fieldId && !isSysId(fieldId))
    {
        dictField   = new DictField(tableID, fieldId);

        fieldType = Global::extendedTypeId2name(dictField.typeId());
        fieldEnum = Global::enumId2Name(dictField.enumId());
        fieldName = strFmt('%1%2', parmPrefixToFieldName, dictField.name());
        fieldTypeLabel = Global::extendedTypeId2pname(dictField.typeId());
        fieldEnumLabel = Global::enumId2pname(dictField.enumId());

        fieldDataType = fieldType ? fieldType : fieldEnum;

        if (i)
            txtb.appendText('\n');

        txtb.appendText(strFmt('%1 %2;', fieldDataType, MYD_Functions::stringLowerCaseFirst(fieldName)));

        i++;

        classBuild = new ClassBuild(className);
        xppSource = new XppSource();
        source = xppSource.parmMethod(fieldDataType, fieldName);

        paramMethodName = strFmt('parm%1%2', parmPrefixToFieldName, strUpr(subStr(fieldName,1,1))+subStr(fieldName,2,strLen(fieldName)));
        
        if (addDataContractAttribute)
            source = strFmt('[DataContractAttribute]\n%1', source);
        
        classBuild.addMethod(paramMethodName, source);

        fieldId = dictTable.fieldNext(fieldId, scope);
    }

    info(txtb.getText());
}

I hope it helps!

DateTime conversion: utcdatetime to string and System.DateTime to utcdatetime

convert utcdatetime to string

static void JobEF_DateTimeToString(Args _args)
{
    utcDateTime myDate = DateTimeUtil::getSystemDateTime();
    
    //OPTION1:
    info(DateTimeUtil::toStr(myDate));
    //OPTION2:
    info(DateEffectivenessCheck::queryDate(myDate));
}

convert System.DateTime to utcdatetime

static void JobEF_DateTimeToUtcDatetime(Args _args)
{
    System.DateTime date1 = System.DateTime::get_Now();

    //OPTION1:
    utcdatetime date2 = CLRInterop::getAnyTypeForObject(date1);
    //OPTION2 (behind the scene, does the same as the above). better use this
    utcdatetime date3 = Global::clrSystemDateTime2UtcDateTime(date1);

    info(strFmt('%1', date2));
    info(strFmt('%1', date3));
}

Example of Date Filter in a form DataSource using SysQuery and DateEffectivenessCheck

The following code is an example of how to build a query range based on a Form Data Source, which displays only "daily" records.

In the example the form-dataSource is named DataSourceName is a date effective table. Depending on a check box only records should be displayed, which that are valid for todays date.

While it is quite a simple task, I want to remind how much easier and more reliable the code is if we use AX built in methods: in particular those two classes:

  • SysQuery
  • DateEffectivenessCheck
public void applyFilter()
{
    queryBuildRange qbr;

    qbr = sysQuery::findOrCreateRange(DataSourceName_ds.queryBuildDataSource(), fieldNum(DataSourceName, recId));

    if( !ShowExpiredCheckBox.checked())
    {
        qbr.value(
               strfmt(
                    DateEffectivenessCheck::queryRange(true,false,false),
                    DataSourceName_ds.queryBuildDataSource().name(), // queryBuildDataSource name
                    fieldstr(DataSourceName, ValidFrom), // table field from date
                    fieldstr(DataSourceName, ValidTo), // table field to   date
                    DateEffectivenessHelp::queryDate() // test date (MUST use the queryDate method for correct formatting), alternatively can use also: DateTimeUtil::toStr(DirUtility::getCurrentDateTime())
                 )                                              
                     );
    }
    else
    {
        qbr.value(SysQuery::valueUnlimited());
    }
}

how much more code would you need without using those two classes!!

Monday, September 2, 2019

c# extension method to retrieve documents from Dynamics Ax and expose them to a WebApi via a repository class

As an introduction let me show you a piece of X++ to get a document.

You can find much more if you google it.

static void JobEF_DocumentSearch(Args _args)
{
    Notes notes = "";
    DocuRef docuRefTmp;
    HcmDiscussion hcmDiscussion;
    DocuRefSearch docuRefSearchTmp;
    DocuTypeId docuTypeId;
    FilePath filePath, filePathGeneric, path;

    hcmDiscussion = HcmDiscussion::findByDiscussionWorker('000024', HcmWorker::findByPersonnelNumber('2002450').RecId);
    if ( ! hcmDiscussion ) return;


    //docuRefSearchTmp = DocuRefSearch::newCommon( hcmDiscussion );
    //filter per document type
    docuTypeId = 'Appraisal';
    docuRefSearchTmp = DocuRefSearch::newDocuTypeId(hcmDiscussion, docuTypeId);

    filePathGeneric = Docu::archivePath(curExt());
    filePath = DocuType::find(docuTypeId).ArchivePath;

    info(filePathGeneric);
    info(filePath);

    while ( docuRefSearchTmp.next() )
    {
        docuRefTmp = docuRefSearchTmp.docuRef();

        notes = docuRefTmp.docuValue().FileName;
        path = docuRefTmp.path();
        info(strFmt('%1 --- %2', path, notes));
    }
}

now let me show you how we can do the same from c# using proxy objects

to open a session i will use the code described in this post, read it first:
https://enricoariel.blogspot.com/2019/08/proxy-classes-for-net-interop-to-x.html

step1: create an extension class

using System;
using System.Collections.Generic;
using Microsoft.Dynamics.AX.Framework.Linq.Data;
using U23 = Microsoft.Dynamics.AX.ManagedInterop;
using U22 = Microsoft.Dynamics.AX.Framework.Linq.Data;

namespace MydDynamicsIntegration.DynamicsCommon
{
    public static class GenericRecord
    {
        public static List<DocuRefProperties> GetAttachments<T>(this T axTable, string docuTypeId = null) where T : Common
        {
            var ret = new List<DocuRefProperties>();

            var docuRefSearchTmp = docuTypeId == null ? DocuRefSearch.newCommon(axTable) : DocuRefSearch.newDocuTypeId(axTable, docuTypeId);

            while (docuRefSearchTmp.next())
            {
                DocuRef docuRefTmp = docuRefSearchTmp.docuRef();
                var docuref = new DocuRefProperties
                {
                    RecId = docuRefTmp.RecId,
                    ValueRecId = docuRefTmp.ValueRecId,
                    Path = docuRefTmp.path(),
                    FileName = docuRefTmp.docuValue().fileName(),
                    Description = docuRefTmp.Name,
                    TypeId = docuRefTmp.TypeId,
                    CreatedDateTime = (DateTime) docuRefTmp.CreatedDateTime
                };
                ret.Add(docuref);
            }

            return ret;
        }

        public class DocuRefProperties
        {
            public long RecId { get; set; }
            public long ValueRecId { get; set; }
            public string Path { get; set; }
            public string FileName { get; set; }
            public string Description { get; set; }
            public string TypeId { get; set; }
            public DateTime CreatedDateTime { get; set; }
        }
    }
}

Note: import the relevant proxy objects in your .Net project

step2: create the repository

in this example I made a repository for documents attached to HcmDiscussion

DiscussionRepository.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using MydDynamicsIntegration.DynamicsCommon;
using MydDynamicsIntegration.Models;
using MydDynamicsIntegration.Repositories.Interfaces;

namespace MydDynamicsIntegration.Repositories.Implementation
{
    public class DiscussionRepository : IAmDiscussionRepository
    {
        private const string AppraisalDocuTypeId = "Appraisal";
        public List<Attachment> GetAttachments(string discussionId, string personnelNumber)
        {
            using (var axSession = new AxSessionManager())
            {
                axSession.OpenConnection();

                var workerRecId = HcmWorker.findByPersonnelNumber(personnelNumber).RecId;
                var discussion = HcmDiscussion.findByDiscussionWorker(discussionId, workerRecId);

                return discussion.GetAttachments(AppraisalDocuTypeId).Select(attachment => new Attachment
                {
                    RecId = attachment.RecId,
                    ValueRecId = attachment.ValueRecId,
                    Path = attachment.Path,
                    FileName = attachment.FileName,
                    Description = attachment.Description,
                    TypeId = attachment.TypeId,
                    CreatedDateTime = attachment.CreatedDateTime
                }).ToList();
            }
        }       
    }
}

Attachment.cs:

using System;

namespace MydDynamicsIntegration.Models
{
    public class Attachment
    {
        public long RecId { get; set; }
        public long ValueRecId { get; set; }
        public string Path { get; set; }
        public string FileName { get; set; }
        public string Description { get; set; }
        public string TypeId { get; set; }
        public DateTime CreatedDateTime { get; set; }
    }
}

I omitted to copy the IAmDiscussionRepository, but is quite intuitive.

step3: use the repository in the WebApi controller class:

        [Authorize]
        [GET("api/worker/{personnelNumber}/discussions/{discussionId}/attachments/{attachmentRecId}")]
        [HttpGet]
        public HttpResponseMessage Attachment(string personnelNumber, string discussionId, long attachmentRecId)
        {
            var attachment = _discussionRepository.GetAttachment(discussionId, personnelNumber, attachmentRecId);

            return attachment.IsNullOrEmptyObject() ?
                Request.CreateErrorResponse(HttpStatusCode.NotFound, HttpStatusCode.NotFound.ToStringWithSpaces()) : 
                Request.CreateResponse(HttpStatusCode.OK, _attachmentMapping.Map(attachment));
        }

Note: in the webApi project I used Ninject and Attribute routing.

finally here the mapping class:

using System.Collections.Generic;
using System.IO;
using System.Linq;
using IDHGroup.DynamicsApi.CIT.Services.Interfaces;
using IDHGroup.DynamicsDataTransfer.CIT.Models.Dtos.RequestView;
using MydDynamicsIntegration.Models;

namespace IDHGroup.DynamicsApi.CIT.Services.Implementations
{
    public class AttachmentMappingService : IAmAttachmentMapping
    {
        public List<AttachmentViewDto> Map(List<Attachment> attachments)
        {
            return attachments.Select(MapAttachmentViewDto).ToList();
        }

        public AttachmentViewDto Map(Attachment attachment)
        {
            return MapAttachmentViewDto(attachment);
        }

        private static AttachmentViewDto MapAttachmentViewDto(Attachment attachment)
        {
            return new AttachmentViewDto
            {
                RecId = attachment.RecId,
                FilePath = Path.Combine(attachment.Path, attachment.FileName),
                Description = attachment.Description,
                TypeId = attachment.TypeId,
                CreatedDateTime = attachment.CreatedDateTime
            };
        }
    }
}

Search Projects where an Element Is Used in Dynamics Ax using x++ Reflection

today I need to search in which project an object was customized and ask for the relevant FDD. It is always a good practice to add some notes when you customize code, but not everybody does… So I found in the book Microsoft Dynamics AX 2012 R3 Development Cookbook on pag 333 a good recipe. But unfortunately the object I was looking for was in a deeper node and this recipe did not work.

Let me first bring the original recipe for Searching for an object in a development project from the book

In Dynamics AX, any development changes to the application normally have to be organized
in development projects. The same object could belong to one or more projects, but Dynamics
AX does not provide an easy way to determine which development projects a specific object
belongs to.
In this recipe, we will create a class to search for an object in the development projects.
The class is only for demonstration purposes, but it can be easily converted to a standalone
tool and integrated into the right-click menu.

In the AOT, create a new class with the following code snippet:

class EF_SearchProjectsElementIsUsed
{
}

public static void main(Args _args)
{
    EF_SearchProjectsElementIsUsed::TestDevProjectSearch();
}

static server void TestDevProjectSearch()
{
    EF_SearchProjectsElementIsUsed search;
    search = new EF_SearchProjectsElementIsUsed();
    search.find(UtilElementType::Table, tableStr(CustTable));
}

void find(UtilElementType _type, IdentifierName _name)
{
    TreeNode projects;
    ProjectNode project;

    projects = SysTreeNode::getSharedProject();
    if (!projects)
    {
        return;
    }

    project = projects.AOTfirstChild();

    while (project)
    {
        if (this.findChildren(
                project.loadForInspection(),
                _type,
                _name))
        {
            info(project.AOTname());
        }
        project = project.AOTnextSibling();
    }
}

private boolean findChildren(
            TreeNode _parent,
            UtilElementType _type,
            IdentifierName _name)
{
    TreeNode child;
    TreeNodeIterator iterator;
    #TreeNodeSysNodeType
    iterator = _parent.AOTiterator();
    child = iterator.next();

    while (child)
    {
        if (child.treeNodeType().id() == #NT_PROJECT_GROUP)
        {
            return this.findChildren(child, _type, _name);
        }
        else if (child.AOTname() == _name &&
                        child.treeNodePath() &&
                        child.utilElement().recordType == _type)
        {
            return true;
        }
        child.treeNodeRelease();
        child = iterator.next();
    }
    return false;
}
How it works...

In this recipe, we create a new class with several methods. The first method is
findChildren() and is used for a recursive search operation within the AOT node. It
accepts three parameters: a TreeNode object, an element type, and an element name. In
this method, we go through all the children of the TreeNode object and check whether any
of them match the provided element type and name. If any of the child nodes contain more
nodes within, we use the same findChildren() method to determine whether any of its
children match the element type and name.
The second method is named find() and is used for the actual search, for the given element
type and name. The method goes through all of the shared development projects and calls the
findChildren() method to determine whether the given element is in one of its nodes.
The class can be called from anywhere in the system, but in this recipe, to demonstrate how it
works, we create a new job, define and instantiate the class, and use the find() method to
search for the CustTable table in all the shared projects.

Mine enhanced recipe using a dialog and TreeNodeTraverser to get also child nodes containing the object

I also added a filter to search only in projects belonging to a specific layer. So I can just search to which project in cus layer the object belong. This speeds the search quite significantly.

class EF_SearchProjectsElementIsUsed
{
}
public static void main(Args _args)
{
    Dialog dialog;
    DialogField elementType, layer, elementName;
    UtilElementType type;
    IdentifierName name;
    UtilEntryLevel projectLayer;

    dialog = new Dialog('Search element in projects');

    elementName = dialog.addField(extendedTypeStr(IdentifierName));
    layer = dialog.addField(enumStr(UtilEntryLevel), 'Project Layer');
    elementType = dialog.addField(enumStr(UtilElementType), 'Type');
    dialog.addText("NOTE: setting the type will traverse the node and search in child nodes");

    elementName.control().mandatory(true);

    if (dialog.run() && elementName.value() != '')
    {
        type = elementType.value();
        name = elementName.value();
        projectLayer = layer.value();

        EF_SearchProjectsElementIsUsed::TestDevProjectSearch(type, name, projectLayer);
    }


}
static server void TestDevProjectSearch(UtilElementType _type, IdentifierName _name, UtilEntryLevel _projectLayer)
{
    EF_SearchProjectsElementIsUsed search;
    search = new EF_SearchProjectsElementIsUsed();
    search.find(_type, _name, _projectLayer);
}
void find(UtilElementType _type, IdentifierName _name, UtilEntryLevel _projectLayer)
{
    TreeNode projects;
    ProjectNode project;
    Set objectsInProject;
    SetEnumerator objectsInProjectEnumerator;
    str objectInProjectPath;

    projects = SysTreeNode::getSharedProject();
    if (!projects)
    {
        return;
    }

    project = projects.AOTfirstChild();

    setprefix(strFmt('These projects contain %1', _name));

    while (project)
    {
        /* I just want to search in cus layer, so is faster.
        to search all remove the condition */
        if (project.AOTLayer() != _projectLayer)
        {
            project = project.AOTnextSibling();
            continue;
        }

        objectsInProject = this.findChildren(
                                    project.loadForInspection(),
                                    _type,
                                    _name);

        if (objectsInProject.elements())
        {
            setprefix(project.AOTname());
            objectsInProjectEnumerator = objectsInProject.getEnumerator();

            while (objectsInProjectEnumerator.moveNext())
            {
                objectInProjectPath = objectsInProjectEnumerator.current();
                info(objectInProjectPath);
            }

        }
        project = project.AOTnextSibling();
    }
}
private Set findChildren(
            TreeNode _parent,
            UtilElementType _type,
            IdentifierName _name)
{
    TreeNodeTraverser tnt;
    TreeNode treeNode;
    UtilElements utilElements;
    TreeNodeType nodeType;
    boolean isRoot;
    str treePath;
    boolean nodeTypeIsUtilElement;
    boolean isTreePath;
    boolean showObject;
    Set objectsInProject;

    tnt = new TreeNodeTraverser(_parent);
    objectsInProject = new Set(Types::String);

    while (tnt.next())
    {
        treeNode = tnt.currentNode();
        nodeType = treeNode.treeNodeType();
        showObject = true;

        if (_type)
        {
            showObject = false;
            nodeTypeIsUtilElement = nodeType.isUtilElement();
            isTreePath = treeNode.treeNodePath() ? true : false;
            isRoot = nodeType.isRootElement();

            if (nodeTypeIsUtilElement && isTreePath && isRoot)
            {
                utilElements = treeNode.utilElement();//need the above conditions otherwise treeNode.utilElement() might fail
                if (utilElements.recordType == _type)
                    showObject = true;
            }
            //Debug::printDebug(strFmt('x: %1 - %2', showObject, treeNode.AOTToString()));
        }


        if (treeNode.AOTname() == _name && showObject)
        {
            objectsInProject.add(strFmt('%1 - (is root: %2)', treeNode.AOTToString(), nodeType.isRootElement()));
        }

        treeNode.treeNodeRelease();
    }
    return objectsInProject;
}

this how it would look like without setting the type. (note that it finds every object, tables forms, menu items… and also child objects like form datasources)

image

image

while setting the type it will filter per object type.

image

image