2012年8月15日星期三

How to validate Email in AX Code

static boolean THK_validateEmail(args   _arg)
{
    str _email = "jimmy.xie@tectura.com;jimmyxie.yf@hotmail.com";
    str emailRegex = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
    str email;
    List emailAddresses;
    ListEnumerator enum;
    System.Text.RegularExpressions.Regex regEx;
    System.Text.RegularExpressions.Match regMatch;
    InteropPermission permission = new InteropPermission(InteropKind::ClrInterop);
    boolean retVal;
    permission.assert();
    //BP Deviation documented
    regEx = new System.Text.RegularExpressions.Regex(emailRegex);
    // Split the email address string and validate each email address
    emailAddresses = SysEmailDistributor::splitEmail(_email);
    enum = emailAddresses.getEnumerator();
    while(enum.moveNext())
    {
        email = enum.current();
        if (email)
        {
            regMatch = regEx.Match(email);
            retVal = regMatch.get_Success();
        }
        else
            retVal = false;
        // If a single email address is invalid, the string is invalid
        if(!retVal)
            break;
    }
    info(queryValue(retVal));
    return retVal;
}

2012年8月9日星期四

How to check active dimension of item


static void THK_isConfigIdActive(Args _args)
{
    boolean ret;
    ItemId  ItemId = "1000";
//Code to find active dimension of item in axapta
boolean isConfigIdActive(ItemId _item)
{
    InventDimSearch FindDim = new InventDimSearch();
   InventTable      inventTable;
;
    inventTable = InventTable::find(_item);
    if(FindDim.find(inventTable.DimGroupId, fieldnum(InventDim,ConfigId)))
        return FindDim.dimActive();
    return false;
}
;
    info(queryValue(isConfigIdActive(ItemId)));
}

How to String to Validate only Numbers

1) using String to Validate only Numbers Regular Expression
static void THK_isNumericForRegularExpression(Args _args)
{
    TextBuffer  txt = new TextBuffer();
    str         msg = "9879q7897";
;
    txt.setText(msg);
    txt.regularExpressions(true);   // activate regular expr in search
    setprefix(msg);
    // Regular expression to validate only digits
    if (txt.find("^[0-9]+$"))
    {
        info("Yes,string contains only numbers");
    }
    else
        checkFailed("No");
}
2) build - in method
static void THK_isNumeric(Args _args)
{
    str     st = "1234we7878899";
   
boolean isNumeric(str StrNumVar)
{
    boolean ret = true;
    int     m,n;
    str     s = strkeep(StrNumVar,"1234567890");
;
    m   = strlen(StrNumVar);
    n   = strlen(strkeep(StrNumVar,"1234567890"));
    ret = m == n;
 
    return ret;
}

boolean  THK_validNumbers(str _text)
{
    int     counter, textLen = strlen(_text);
    Phone   validNumbers = '0123456789';
    ;

    for(counter = 1; counter <= textLen; counter ++)
    {
        if(!strfind(substr(_text,counter,1), validNumbers, 1, 10))
        {
            return false;
        }
    }
    return true;
}

;
    info(queryValue(isNumeric(st)));
    info(queryValue( THK_validNumbers (st)));
}