Friday 10 June 2016

Request for the permission of type 'FileIOPermission' failed.


Problem: Generally while working with files we might get the below errors

Request for the permission of type 'FileIOPermission' failed.
(S)\Classes\FileIOPermission\demand
(S)\Classes\WinAPIServer\fileExists- line 11 

or 

Request for the permission of type 'FileIOPermission' failed.
(S)\Classes\FileIOPermission\demand
(S)\Classes\WinAPIServer\deleteFile- line 11 


Solution: 

Set permissionSet;

FileName fileName = ''Test.Xml"; 

permissionSet = new Set(Types::Class);

permissionSet.add(new InteropPermission(InteropKind::ClrInterop));
permissionSet.add(new FileIOPermission(fileName , 'rw'));
CodeAccessPermission::assertMultiple(permissionSet);

 if (WINAPIServer::fileExists(fileName))
    {
        if (Box::yesNo(strfmt("Do you want to re-write the file", filename), DialogButton::No))
        {
            WINAPIServer::deleteFile(filename);
         }
        else
        {
            return;
        }
    }
CodeAccessPermission::revertAssert();

A copy will also require FileIOPermission of 'r' on the source file.  A Move will require a permission of 'w' on the source.

Thursday 9 June 2016

Microsoft.Dynamics.Ax.Xpp.InvalidRemoteCallException’ was thrown


Problem: While working on Files using WinAPI got the below error.

Microsoft.Dynamics.Ax.Xpp.InvalidRemoteCallException’ was thrown


Solution:

Change WinAPI to WinAPIServer

Example:

WINAPI::fileExists(fileName) to WINAPIServer::fileExists(fileName)

WINAPI::deleteFile(filename) to WINAPIServer::deleteFile(filename)



Saturday 30 April 2016

Phone number validation in Microsoft dynamics AX 2012

The below code is used to validate the Phone number.

public boolean validateField(FieldId _fieldIdToCheck)
{
    boolean ret;
    Str         phonePattern = @"[0-9]";
 
    System.Text.RegularExpressions.Match  myMatch;

    ret = super(_fieldIdToCheck);

    if (_fieldIdToCheck == fieldNum(PracticeDemo, PhoneMobile))
    {
        myMatch = System.Text.RegularExpressions.Regex::Match(this.PhoneMobile, phonePattern);
         
        if (strLen(this.PhoneMobile)<10 || strLen(this.PhoneMobile)>10)
        {
            ret = checkFailed(strFmt("%1 please enter 10 digit mobile number", this.PhoneMobile));
        }
         
        if (!myMatch.get_Success())
        {
            ret = checkFailed(strFmt("%1 is not an valid phone number", this.PhoneMobile));
}
    }

    return ret;
}

Thursday 14 April 2016

Email validation in Microsoft Dynamics AX 2012


The below code is used to validate the Email Id.

public boolean validateField(FieldId _fieldIdToCheck)
{
    boolean   ret;
    Str           emailPattern = @"\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b";
   
    System.Text.RegularExpressions.Match  myMatch;

    ret = super(_fieldIdToCheck);

    if (_fieldIdToCheck == fieldNum(PracticeDemo, EmailID))
    {
        myMatch = System.Text.RegularExpressions.Regex::Match(this.EmailID, emailPattern);

    if (!myMatch.get_Success())
{
            ret = checkFailed(strFmt("%1 is not an valid email id ", this.EmailID));
}
    }

    return ret;
}