Wednesday, June 4, 2008

Choose a Printer Tray in Dynamics AX

The PrintJobSettings Object allows you to choose from which tray to print. First ask for the Number of trays on the printer. Notice that the returned value may not correspond to reality. It may be much bigger than the number of trays on the printer itself. Once it returned 20 for a printer with 3 trays.

int trayNums = settings.getNumberOfTrays();

Next step is try and error. Open each tray on the printer and write the number on some sheet of papers. Than you have to try what Tray ID corresponds to which tray.

Args args;
ReportRun reportRun;
PrintJobSettings settings;
int trayId;
;

args = new Args();
args.name(reportStr(MyDummyReport));

reportRun = new ReportRun(args);
settings = reportRun.printJobSettings();
settings.clearTrayPageCopy();
trayId = settings.getTray(1); // maybe 261, etc.
settings.addTrayPageCopy(trayId,1); // try from 1 .. getNumberOfTrays()
reportRun.run();

Check out what trayID value leads to what printing behaviour. Now store the (trayID | real tray number) pair in a paramter table or further use. If the printer is exchanged make sure that your ID |Tray pairs still fit.

Calling a stored procedure

Here is a piece of code to execute a stored procedure thru Axapta

static void storedProcedure(Args _args)
{
LogInProperty Lp = new LogInProperty();
CCADOConnection myConnection;
Statement myStatement;
ResultSet myResult;
;

LP.setServer("SeverName");
LP.setDatabase("DataBaseName");
Lp.setUsername("sa");
Lp.setPassword("sa");

try
{
myConnection = new OdbcConnection(LP);
}
catch
{
info("Check username/password.");
return;
}

myStatement = myConnection.createStatement();
myResult = myStatement.executeQuery('EXEC [inventtablesp]');//stored procedure name
while (myResult.next())
{
print myResult.getString(1);
}
pause;
}

Accessing Parent Datasource

If you need access to the datasource() of the parent form. Parent form and child form being attached with dyna links you can use the following code snippet

element.args().record() to get the Datasource selected on the parent form.

For e.g. If you create a form named CustAdditions which is a child form to CustTable, and these two datasources are attached using dyna links and you want the selected CustTable record on child form you can write in the init method of CustAdditions

Code Snippet:
public void init()
{
CustTable custTable = element.args().record();
}

Getting number of recrods present in a query run

I want to get the total number of records from a query run object. I will use them to generate a progress bar (progress indication framework).

Use SysQuery::countloop(qr). this will return the total number of records in a query object.

using brackets in macro

Try using escape sequences

#define.ActiveDateCriteria("(\%1 == \%2)")

"Loss of precision" warning message

A question came up at today's webinar where a developer had a (presumably legitimate) reason to cast a real value into an integer value. The X++ language does not allow explicit casting (there's no support for it in the language), but the compiler will do its best to satisfy the user and do the conversion on its own. In this case, however, it issues a warning message, lest this is not what the user wanted.

One solution is to use the anytype type to hold the vaue for conversion and then using the any2int function, as shown below:static void Job47(Args _args)
{

real r = 3.13;
int i = r; // Warning is issued here
anytype a;

a = r; // Assign to an anytype variable...
i = any2int(a); // ... and back into an int

print r;
print i;
pause;
}

This should be packaged into a function, maybe called int RealToInt(real arg).

Another way would be doing the conversion in managed code (through the System.Convert::ToInt32(object) method), but the performance will not be as good because of the marshalling that needs to take place.

I hope this helps.

Hide an element from the combo box

Let say you you two forms where this enum is reflected, namely Form1 and Form2. In order to hide an element from Form1 and continue showing it on Form2 go to form design and then access that combo box control. Override the enter() method of that combo box and write this line to delete the element from it.

combobox:enter()
{
super();
this.delete(enum2str(BaseEnum::Element4));
}

Find out the string length of an extendedDataType

static void TestJob(Args _args)

{

Dictionary dict;

DictType dictType;

;

dict = new Dictionary();

dictType = dict.typeObject(dict.typeName2Id(extendedtypestr(AccountName)));

info(strfmt("Name : %1 \nId: %2 \nStringLength: %3 \nAdjustment: %4 \nLabel: %5 \nHelp: %6 \nBasetype: %7", dictType.name(), dictType.id(), dictType.stringLen(), dictType.stringRight() ? "Right" : "Left", dictType.label(), dictType.help(), int2str(dictType.baseType()) + " - " + enum2Value(dictType.baseType())));
}

eBook "Inside Dynamics AX 4.0" available for download

The great book "Inside Microsoft Dynamics AX 4.0" is available as a free download now.
You can go to: http://download.microsoft.com/download/2/5/8/258C8894-B94A-4A87-81EA-4DBB9776F8F2/622579eBook.pdf to download your copy.

When is my Label file updated in AX

When is my Label file updated in AX

If we look at our label files in the AOT we see 4 different extensions for our labels

· ALI Axapta Label Index

· ALC Axapta Label Comments

· ALT Axapta Label Temp, Store

· ALD Axapta Label Dictionary



The ALD file is readable/editable with a text editor. General speaking you only need the ALD file. When the AOS is restarted the ALI and ALC will be generated on the fly. (or update when the time stamp of the ALD file is bigger than the timestamp of the ALC or ALI file)

Next, a developer creates new labels. These labels will be stored in the ALT file. Not yet in the ALD file. When the final AOS will stop. AX will update the ALD file this way. It will copy the ALD file to an ALB file. Next the changes in the ALT file will be stored in the ALB file. Finally this ALB file is placed back in the ALD file and the ALB file will be deleted. (HINT: make the ALD file read only and you will see it your selfJ)

When your AOS has creased the changes are not stored in the ALD file. Even when you start and stop the AOS again the file is not updated. To solve this issue start an AX client search for the label in the Label editor. Next stop your client and the AOS. Now the label file is updated.