Search code examples
axaptadynamics-ax-2012dynamics-ax-2012-r2dynamics-ax-2012-r3

Finding an inventory dimension


I am trying to get an inventDim record if it exists or create a new one otherwise.

InventDim inventDim;

inventDim.InventLocationId = "220";
inventDim = InventDim::findOrCreate(inventDim);
info(inventDim.inventDimId);

I am sure that InventLocationId with the value "220" is already there but anyway, a new one is added.

If i run the above lines again, i will get the last created value, so this time, it's all fine.

By checking in SQL with :

SELECT *
FROM INVENTDIM
WHERE INVENTLOCATIONID = '220'

both lines are returned.

I am able to delete the added line with the following lines:

 select forUpdate inventDim 
    where inventDim.inventDimId == 'the new id';
 inventDim.delete(true);

I can't figure out what am i doing wrong here..


Solution

  • The InventDim table contains values for inventory dimensions. Every record is unique, as in, no record has exactly the same combination of dimensions.

    This means that if you are looking for a record with InventLocationId == "220", there can be many records, one with the InventColorId == "Red" and one with InventColorId == "Blue". You'll get further combinations when you combine other dimensions, for example, you can have two more records where the Color is blue, the location is 220 and the InventSizeId is 5 cm or 3 cm.

    So with your example above, you are only taking one dimension into account and ignoring the rest. It's completely OK to have more than one record with the InventLocationId set to 220, since one or more of the other inventory dimensions will be different.

    If you meant that you want a record where all the other dimensions are empty and InventLocationId is 220, you have to specify that the other dimensions should be blank before you try to find the record.

    InventDim          inventDim;
    InventDim          inventDimNew;
    
    select inventDim
        where inventDim.InventLocationId == "220"
        && inventDim.ConfigId == ""
        && inventDim.InventSizeId == "";
        //Add more blank dimensions depending on the dimensions active in your system.
    
    buf2Buf(inventDim, inventDimNew);
    inventDimNew.inventDimId = '';
    
    info(InventDim::findOrCreate(inventDimNew).inventDimId);