web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :

Validate table record fields through X++

Shashi s Profile Picture Shashi s 1,203

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.

Hopefully this help others :)


Filed under: Ax 2009, Ax 2012, Dynamics Ax, X++ Tagged: Ax2009, AX2012, Dynamics Ax, x++

This was originally posted here.

Comments

*This post is locked for comments