Wednesday, August 15, 2012

Using containers vs. other collection classes


I’ve seen containers used on some forms, so I thought this would be useful information.
This is based on my experience and some sites I’ve read (which I’ve given links to below).

If you’re going to store recIds as your datatype in any of these collection classes and you're not on AX 2012 yet, you should define the type like  this (because AX 2012 handles recIds differently, it will be easier for upgrading when you do this):

Declare a dictype variable like this:
DictType    dt = new DictType(extendedTypeNum(recid));
Then use it like this, here are examples for a map and a set:
msgMap = new Map(dt.baseType(),Types::String);
setRecIds = new Set(dt.baseType());

Containers:
Containers are dynamic and have no limits. They can contain elements of almost all data types: boolean, integer, real, date, string, container, arrays, tables, and extended data types. However, objects may not be stored in containers.

Containers in AX are used very often. It’s easy to work with them, but
data in containers are stored sequentially, and retrieved sequentially. This means that containers provide slower data access if you are working with a large numbers of records. You cannot modify a container in-place, instead each  addition or deletion has to iterate over the entire structure to copy all values into a newly allocated one. So every container manipulation has a run time of O(n). This is why they are not recommended to be used on forms. This site has some good information about how to use containers more efficiently when you do need to use them http://www.axaptapedia.com/index.php?title=Container If you are storing unique data of one type (like storing recids for a checkbox on a form),you can use a Set (see below). If you are storing non-unique data of the same type, use a List (see below).

Maps are most useful if you need a key for your data. I used a map recently to store the recId as a key to save/retrieve the inventTransId of some records. This is a good explanation of how to use Maps (http://www.axaptapedia.com/index.php?title=Map_Class)

Sets:
Sets are an unordered list of items. If you try to add something to a set that is already in the set, it will ignore it. Sets have an in() and remove() method which is useful. This is a good explanation of how to use Sets http://www.axaptapedia.com/index.php?title=Set_Class

Lists:
Lists contain elements that are accessed sequentially. Lists provide getEnumerator() and getIterator() methods (like sets do) which allow you to insert and delete items from the list. Here’s some information about Lists http://msdn.microsoft.com/en-us/library/aa848905%28v=ax.10%29.aspx.

Monday, April 30, 2012

Using the Fill Utility on the table and/or fields you specify

We have a customized field in AX that we want the user to be able to select multiple records and change them all to the same value at once. Dynamics AX 2009 has a fill utility that can do this. However, the fill utility can only be used on tables that are "main" tables (property TableGroup = main). I wanted it to be able to be used on my custom field (a date field) on the SalesTable form. Here's what I had to do:
On the Form: SysRecordInfo, Method: init
Add code to allow the fill utility to be used on my table.
void init()
{
 ....
     // don't do the field level testing if the table doesn't meet the base requirement
    if (dictTable.tableGroup() == TableGroup::Main ||
        common.TableId == tablenum(LedgerJournalTrans)
        //N - GW_Admin_FillUtility - Arains 03/02/2012
        // Allow the fill utility to be used on the SalesTable
     || common.TableId == tablenum(SalesTable))
    {
        element.fillUtilityInit();
    }
    else
    {
        fillGrp.visible(false);
    }
 ....
}

Also, on the Method: fillUtilityInit
Add code to allow the fillUtility to be used for my custom field on the salestable and only my custom field on the SalesTable.

void fillUtilityInit()
{
....
 if (fieldIdLocal == dictTableLocal.primaryKeyField() ||
                        fieldIdLocal == dictTableLocal.fieldName2Id('RECID') ||
                        fieldIdLocal == dictTableLocal.fieldName2Id('DATAAREAID'))
                    {
                        fillGrp.visible(false);
                        return;
                    }

                    // New code for fill utility modifications
                    // If this is the salesTable and this is the custom date field
                    // allow the fill utility to be used, disallows for all other fields
                    if(tblId == TableNum(SalesTable) &&
                       fieldIDLocal != dictTableLocal.fieldName2Id('YourCustomField'))
                    {
                        fillGrp.visible(false);
                        return;
                    }
....
}

Friday, March 30, 2012

Unpicking the entire order all at once

For orders in AX (I am addressing sales orders specifically here), if you want to unpick an order, you have to go to the sales line and, one-by-one, unpick the order. You click on the Inventory button and select the Pick option. In the form, check the autocreate box, then click Post All in the lower area of the form. If you regularly have to pick and unpick orders, especially if you have a lot of lines on your orders, this can become very tedious.

I wrote a job that will unpick the entire order. Now I'm going to put an Unpick button on the sales order form at the order level and allow certain users (security will be used) to do this. I will most likely allow multiple orders to be selected so you can unpick multiple orders with one click of a button. The code for the job I wrote is below:

    // For testing, I set the salesid here.
    // In the final code, I will have to pass in the salesTable record
    // from the salesTable_ds of the form
    SalesId salesid = 'RSO948671';
    TmpInventTransWMS   tmpinventTransWMS;
    InventMovement  movement;
    InventTrans inventTrans;
    salesline   salesline;
    inventtransWMS_pick inventTransPick;
    ;
   
    while select salesline
    where salesline.SalesId == salesId
    {
        select inventTrans
            where inventTrans.TransRefId == salesline.SalesId &&
                  inventTrans.ItemId == salesline.ItemId &&
                  inventTrans.StatusIssue == StatusIssue::Picked;
        if(inventTrans.RecId)
        {
            movement = null;
            movement = InventMovement::construct(salesLine);
            inventTranspick = new InventTransWMS_Pick(movement,tmpInventTransWMS);
            tmpInventTransWMS = null;
            tmpInventTransWMS.initFromInventTrans(inventTrans);
            tmpInventTransWMS.InventQty = inventTrans.StatusIssue == StatusIssue::Picked ? inventTrans.Qty : -inventTrans.Qty;
            tmpInventTransWMS.insert();
            inventTransWMS_pick::updateInvent(inventTransPick, tmpInventTransWMS);
        }
    }

Wednesday, January 11, 2012

Go to main table form dynamic use

You can set the FormRef property on a table to a display menu item that references a class. This will allow you to open different forms depending on some field value in the record that is passed. In the class main() you can retrieve the record and then open the appropriate form from code.

Tuesday, October 25, 2011

How to unhide grid views

We all know how you can right-click on the grid of a form and use MorphX to add fields, remove fields, etc. You can also save and load views of a grid. Have you ever accidentally right-clicked on a form and clicked "hide" and not been able to figure out how to unhide?

In the example below, I have clicked "Load" to see all the views I could load for this grid. I then right-clicked on my list and said "Hide."


Now that I've hidden it, I cannot right-click and "unhide." I've lost my list of views. The only way to fix it is to remove a record from your usage data.

Go to Tools > Options to modify your own usage data or go to Admin > Users > select the user that is having this issue, click on the "User options" button. From there, click the "Usage data" button. Go to the last tab that says "All usage data." Highlight the row that has Record Type = UserSetup and Name = SysPick (see below) and delete that record (hit the big red X at the top of the form or press Alt-F9).

Friday, October 21, 2011

Calling AIF Web Service - not sending all the fields into AX

We had the occasion to call the Dynamics AX SalesOrder create web service. However, we were trying to pass in the SalesOrder.SalesType field and it wouldn't come through in the XML that AIF read. The element and the attribute were not there in the XML file. We spoke with someone at Microsoft and they told us that you had to use the "specified" property on the field. For example (this is C#):

salesOrder.SalesType = AxdEnum_SalesType.Sales;
salesOrder.SalesTypeSpecified = true;

This seemed to do the trick!

Limiting user sessions

Our company has a limited number of AX user licenses, as most companies will. Some people like to open multiple sessions (and eat up licenses) while they're working. We have asked people to stop this and we close their sessions when we notice that they have multiples open, but we wanted some way for the system to prevent this from happening.

I read about this idea on another blog, but I didn't save the link. It's the same general idea as they proposed, but I did it a little differently, in the end.

I created a table called UserSessionLimits. It has a field called UserID (extended data type is userId) and this is a mandatory field. It has another field called MaxSessions (this is an integer) and it also is mandatory. The index is UserIDx, it contains userid and it is unique.

The code that accesses this table is written so that if a user is not in this table, then they are limited to one session, so add users to this table if they are exceptions and you want them to be able to have more than one session open.

Add code to the "Info" class, in the startup() method. The Info class can only be accessed by going to the System Documentation area of the AOT.

In the startup method, at the top, I placed this code:

select count(recid) from sysClientSessions
where sysClientSessions.userId == curuserid() &&
sysClientSessions.Status == 1 &&
sysClientSessions.clientType == 0;

if(sysClientSessions.RecId > UserSessionLimits::getMaxSessions(curUserid()))
{
box::stop(strfmt("You have exceeded your session limit of %1, the application will now close",
UserSessionLimits::GetMaxSessions(curUserid())));
appl.globalcache().set(classstr(info),identifierStr(AutoLogoff), true);
this.shutDown(true);
}

By searching only for clientType = 0, you will only be looking for "User" sessions. Web sessions or worker sessions (batches that are running) will not be affected.

There have been times that people have gotten around this. They quickly opened two sessions immediately one after the other. If they are simultaneously opened, it's hard to catch. Also, sometimes this locks people out. If they were doing something and AX shut down on them or their system froze, sometimes it takes some time for the session to end for them to get back in again. Your network administrator can control when inactive sessions time out.

We also set automatic shutdown (in user options) to 60 minutes. So if their session is inactive for 60 minutes, it will close.

Update:
So we implemented this and found that sometimes people would see this box and never click OK. It is the user interaction that causes the session to close. So if they just leave the session up with the box open, then they can still open up a bunch of sessions and eat up our licenses. So we changed the code to show a progress box for 5 seconds instead of a box that requires user interaction. I made this a utility in case we ever needed a progress box that would show a message for a period time and then close automatically, we could call this code again. Here is the code:

static void timedProgressBox(int timetoWait, str caption, str text)
{
    #AviFiles
    AppTimer timer = new AppTimer();
    SysOperationProgress  progress = new SysOperationProgress();
    int i ;
    ;

    timer.Start();

    progress.setAnimation(#AviStopWatch);
    progress.setText(text);
    progress.setCaption(caption);
    progress.setTotal(timetoWait);

    for (i = 0; timer.time() < timetoWait; i++)
    {
        timer.ticks();
        progress.setCount(i,1);
        i = real2int(timer.time());
    }

    progress.kill();

    timer.Stop();
}


FTP in and out of Dynamics AX

Axaptapedia has a good article on this:
http://axaptapedia.com/index.php?title=FTP_from_Axapta.

I pretty much followed their instructions and then tweaked the code to work for our needs.

Just a few notes on it:
1. I added the InteropPermission assert before setting the handle.
2. I had to use the IP address of our FTP server instead of the ftp.company.com address.
3. My set current directory looked something like "folder//subfolder" don't forget the double slashes.
4. My put file is on another server so I had to do something like (handle,"\\\\server\\folder\\folder2\\folder3\\xmlfile.xml","XMLFile.xml"); Lots of slashes involved.
5. The get file requires the handle, then the file you are going to get, then the file location and name that you want it to create.

The upload worked great. I'm having issues with the download, but only because I think the user that I'm logging into on the FTP server does not have permission to access the file location that I'm trying to put it to. I'll follow up on this post when we have it working. I could download it to my C: drive fine though. All in all, a success!

Wednesday, August 17, 2011

Dynamics AX 2009 - removing new line character from string (note/memo/text) fields

So this is my first post on this dynamics AX blog of mine. I'm going to use this to keep track of the little programming solutions I develop. Maybe it will help someone else too. Who knows?

My customer was having problems with a report showing the delivery address in excel. When a new line character is reached, it moves to another line. This made the excel format untenable for our 3rd party vendor that needed the data. So I wrote this code on the report to strip the new line character out and replace it with a space.

display str DeliveryStreet()
{
TextBuffer txt = new TextBuffer();
;

txt.setText(dataSource.DeliveryStreet);
txt.replace('\n',' ');
dataSource.DeliveryStreet = txt.getText();

return(dataSource.DeliveryStreet);
}

The DeliveryStreet field on the report calls this method instead of calling the field on the datasource.