31 January 2013

Exception handling in AX / Try catch throw in AX

public boolean createExcelApp()
{
boolean ret = true;
;
try
{
sysExcelApplication = SysExcelApplication::construct();
if (sysExcelApplication == null)
{
ret = false;
throw Exception::Error;
}
}
catch (Exception::Error)
{
error("@SYS59928");
}
return ret;
}

=============UPdate exception=========

System.Exception ex;
        try
        {
            tableBuffer.field = value;
            tableBuffer.update();
        }
        catch(ex)
        {
            System.String messageEx = ex.Message;
            errorMessage = messageEx;
        }

30 January 2013

Dialog in class with batch job in AX/ working with batch job in AX

1. Class have to extend the RunbaseBatch
2. implement the Run() in this method we can call our method which is created for logic(upload()).
3. Implement the pack and unpack methods
4. Implement the dialog() method
5. Construct() method
6. getFromDialog() method
7. Main method
8. Description method
9. Validate method
10.canGoBatch()
11.canGoBatchJournal

--
class INU_UpdExtProcComplete extends runbasebatch
{
DialogField dlgField;
FilenameOpen filenameOpen;

#define.CurrentVersion(1)
#define.Version(1)
#localmacro.CurrentList
filenameOpen
#endmacro
}
--
public void run()
{
if(filenameOpen)
{
if(WINAPI::fileExists(filenameOpen))
this.upload();
else throw error("@SYS26757");
}
super();
}
--
client static INU_UpdExtProcComplete construct()
{
return new INU_UpdExtProcComplete();
}
--
public container pack()
{
return [#CurrentVersion,#CurrentList];
}
--
public boolean unpack(container packedClass)
{
Version version = Runbase::getVersion(packedClass);

switch(version)
{
case #CurrentVersion:
[version, #CurrentList] = packedClass;
break;
default:
return false;
}

return true;
}
--
public boolean getFromDialog()
{
boolean ret;

ret = super();

filenameOpen = dlgField.value();

return ret;
}
--
public Object dialog()
{
DialogRunbase dialog = super();
;
dlgField = dialog.addFieldValue(typeid(FilenameOpen),"","@IST10755","@IST10755");
return dialog;
}
--
void upload()
{
SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
int row;
FileName filename;
TINS_PurchTable tINS_PurchTable;
PurchId purchId;
str dataArea;
;
row++;
application = SysExcelApplication::construct();
workbooks = application.workbooks();
filename = filenameOpen;

try
{
workbooks.open(filename);
}
catch (Exception::Error)
{
throw error("@SYS19358");
}

workbook = workbooks.item(1);
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1);
cells = worksheet.cells();


do
{
row++;
purchId = cells.item(row, 1).value().bStr();
dataArea = cells.item(row, 2).value().bStr();

if(purchId)
{
ttsbegin;
select forupdate crosscompany purchTable
where purchTable.PurchId == purchId;
if(tINS_PurchTable.PurchId)
{
changecompany(dataArea)
{
purchTable.OneTimeVendor = Noyes::Yes;
purchTable.doupdate();
info("The purchase id %1 is updated successfully",purchTable.PurchId);

}
}
else
{
info(strfmt("@IST10757",purchId));
}
ttscommit;
}
else
{
throw error(strfmt("@IST10758"));
}
type = cells.item(row+1, 1).value().variantType();
}
while (type != COMVariantType::VT_EMPTY);
application.quit();
}
--
public static void main(Args args)
{
INU_UpdExtProcComplete iNU_UpdExtProcComplete;
;
iNU_UpdExtProcComplete = INU_UpdExtProcComplete::construct();

if(iNU_UpdExtProcComplete.prompt())
iNU_UpdExtProcComplete.run();
}
--
static ClassDescription description()
{
return "@IST10759";
}
--
boolean validate()
{
boolean ret;

ret = super();

if(!winapi::fileExists(filenameOpen))
ret = checkFailed(strfmt("@SYS18678",filenameOpen));

return ret;
}
--
public boolean canGoBatch()
{
return true;
}
--
public boolean canGoBatchJournal()
{
return true;
}

CrossCompany ChangeCompany in AX how to get the record from different companies

Cross company and Change company

Job
PurchTable purchTable;

select forupdate crosscompany purchTable
where purchTable.PurchId == purchId;
if(purchTable.PurchId)
{
changecompany(dataArea)
{
purchTable.OneTimeVendor = Noyes::Yes;
purchTable.doupdate();
info("The purchase id %1 is updated successfully",purchTable.PurchId);
}
}

18 January 2013

Request for the permission of type 'InteropPermission' failed. / Error in AX InteropPermission



Request for the permission of type 'InteropPermission' failed.

The solution is for the construct() method, have run on client.





Ex:
client static INU_SalesAgreementExpDelete Construct()
{
return new INU_SalesAgreementExpDelete();
}


when ever we are implementing runbasebatch, if we are using the com and sysmailer class have to run client on clientconstruct() method.

17 January 2013

Send an Email in AX / Email and alerts

Step 1. Admin-> setup->Email parameters
Step 2. Basic email template

SysEmail Errors please click here

static void sendEmail()
{
SysMailer mailer = new SysMailer();
SysEmailParameters parameters = SysEmailParameters::find();
str emailFrom = "abc@gmailcl.com";//sysuserInfo::find(curUserId(),false).Email;
InteropPermission permission = new InteropPermission(InteropKind::ComInterop);
str _emailTo;
str _subject;
str _bodyText;
;
_emailTo = "xyz@gmail.com";
_subject = "Mail";
_bodyText = "Aboutn Report";
CodeAccessPermission::revertAssert();
permission.assert();
if (_emailTo != "")
{
if (parameters.SMTPRelayServerName)
{
mailer.SMTPRelayServer(parameters.SMTPRelayServerName,
parameters.SMTPPortNumber,
parameters.SMTPUserName,
SysEmailParameters::password(),
parameters.NTLM);
}
else
{
mailer.SMTPRelayServer(parameters.SMTPServerIPAddress,
parameters.SMTPPortNumber,
parameters.SMTPUserName,
SysEmailParameters::password(),
parameters.NTLM);
}
mailer.fromAddress(emailFrom);
mailer.tos().appendAddress(_emailTo);
mailer.subject(_subject);
mailer.htmlBody(_bodyText);
mailer.sendMail();
//SysInEtMail::sendEMail("Admin","En-us","abc@outlook.com"); // to email id
}
CodeAccessPermission::revertAssert();
}
==============================================
==============================================
static void emailThruSysMailer(Args _args)
{
SysMailer mailer = new SysMailer();
SysEmailParameters parameters = SysEmailParameters::find();
;

if (parameters.SMTPRelayServerName)
{
mailer.SMTPRelayServer(parameters.SMTPRelayServerName,
parameters.SMTPPortNumber,
parameters.SMTPUserName,
SysEmailParameters::password(),
parameters.NTLM);
}
else
{
mailer.SMTPRelayServer(parameters.SMTPServerIPAddress,
parameters.SMTPPortNumber,
parameters.SMTPUserName,
SysEmailParameters::password(),
parameters.NTLM);
}



mailer.fromAddress('xxx@123.com');
mailer.tos().appendAddress('aaa@123.com');
mailer.subject('AZZZ');
mailer.htmlBody('Comunicazione AX');


mailer.sendMail();
}

Ledger Voucher creation Framework and x++ code to create ledger voucher

 Please click her for MS reference file Below is the out of the box example reference and code. SalesInvoiceJournalPostSubBill_Extension->...