Wednesday, 18 December 2013

Validate table record fields through X++ ax 2009


This is a code snippet I use to validate a record created in X++. I do not have to manually go and check if the group I am adding exists in the reference table. The validateField method does this, but is only called from the UI. This topic has been added by me previously here
The following code goes through each line and validates the field, and then the record itself. Any error messages are populated in the infolog (just like the UI)
It could be placed as a static method and can be used application wide with one line of code
public static boolean validateRecord(common _common)
{
    boolean ret = true;
    DictTable dictTable = new DictTable(_common.TableId);
    int fields;
    int i;
    ;
    fields = dictTable.fieldCnt();
    //iterate through each field and validate it
    for(i = 1; i    {
        ret = ret && _common.validateField(dictTable.fieldCnt2Id(i));
    }
    //validate the record itself over here
    ret = ret && _common.validateWrite();

    return ret;
}
To implement this code we can call it like this
inventTable.itemId = "NewItem0001";
inventTable.itemGroupId = "NewItemGroup001";
/*
..
Other data
*/
//Check if the item validates
if(Validations::validateRecord(inventTable))
{
    inventTable.insert();
}
For the above example, an error will be shown because the itemGroupId does not exist.


No comments:

Post a Comment