vendredi 29 juillet 2011

How to convert utcDateTime to date

You have to use  the function DateTimeUtil::date

Here is an example

utcdatetime xppDttm;
DateTimeUtil::date(xppDttm);

static void JobDateTimeGlobalMarshal(Args _args)
{
    System.DateTime netDttm;
    utcdatetime xppDttm;
    str xppString;
    ;
    xppDttm = 2007-06-05T23:22:21; // ISO standard format.
   
    // Convert X++ to .NET.
    netDttm = Global::utcDateTime2SystemDateTime(xppDttm);

    // Convert .NET to X++.
    xppDttm = Global::CLRSystemDateTime2UtcDateTime(netDttm);
    print DateTimeUtil::date(xppDttm);
    pause;
    xppString = DateTimeUtil::toStr(xppDttm);
    info("xppDttm: " + xppString);
}
 Or check this one
public static void testDateTimeConversion()
{
    utcDateTime dateTime;
    date dateInUserTimeZone;
    TimeOfDay timeInUserTimeZone;

    dateTime = DateTimeUtil::utcNow();

    dateInUserTimeZone = DateTimeUtil::date(DateTimeUtil::applyTimeZoneOffset(dateTime, DateTimeUtil::getUserPreferredTimeZone()));
    timeInUserTimeZone = DateTimeUtil::time(DateTimeUtil::applyTimeZoneOffset(dateTime, DateTimeUtil::getUserPreferredTimeZone()));

    dateTime = DateTimeUtil::newDateTime(dateInUserTimeZone, timeInUserTimeZone, DateTimeUtil::getUserPreferredTimeZone());

How to delete the enum value at run time

If I need to delete the enum value at run time then what i need to do?
Example has 3 values
Enum::value1
Enum::value2
Enum::value3
You need to overview the enter event of control from design and write the following line.
combobox:enter()
{
      super();
     this.delete(enum2str(Enum::value2));
}

mardi 26 juillet 2011

How to insert Data into AssetTable using Number Sequence for AssetId

here is the job:


// Changed on 22 Jui 2011 at 17:28:00 by ibs

static void Import_Immob(Args _args)
{
    AssetTable              assetTable;
    NumberSeq               assetIdNumberSeq;
    AssetId                 assetIdAllocated;
    container               c;
    TextIo                  io;
    String50                filename;
    AssetBook               AssetBook, AssetBook_Update;
    String30                grp, name;
    integer                 qty;
    AssetId                 assetId;
    Numberseq               num;
    String30                stop;
    AssetBookId             assetBookId;
    AssetAcquisitionDate    AcquisitionDate;
    String30                stringdate;

    ;
    ttsbegin;
    delete_from assetTable where assetTable.createdBy =='ibs';
    delete_from assetBook  where assetBook.createdBy  =='ibs';

    fileName = @"C:\\IMMOB\IMMOB.csv";
    io = SysLicenseCodeReadFile::openFile(fileName,'r');
    if (!io)
        throw error(strfmt("@SYS18678",fileName));
    io.inFieldDelimiter(";");
     c = io.read();
    while (io.status() == IO_Status::Ok)
    {
        c = io.read();

        if (io.status() != IO_Status::Ok)
          break;

        grp                = conpeek(c,1);
        name               = conpeek(c,3);
        qty                = str2int(conpeek(c,4));
        stringdate         = conpeek(c,6);
        AcquisitionDate    = str2date(stringdate,123);
        assetBookId        = conpeek(c,5);

        while (qty > 0)
        {
            assetTable.assetGroup           = grp;
            assetTable.Name                 = name;
            assetTable.NameAlias            = name;
            assetId                         = assetTable.initAssetNumberSeq(grp).num();
            AssetTable.AssetId              = assetId;
            assetTable.initAssetNumberSeq(grp).used();

            assetTable.insert();


            print qty;

            qty = qty - 1;

        }

    }
    info("finished");
    info(strfmt(assetId));
    ttscommit;

}

and in AssetBook 

static void Update_AssetBook(Args _args)
{
    AssetTable              assetTable;
    NumberSeq               assetIdNumberSeq;
    AssetId                 assetIdAllocated;
    container               c;
    TextIo                  io;
    AssetId                 assetId;
    String50                filename;
    AssetBook               AssetBook, AssetBook_Update;
    String30                grp, name;
    integer                 qty;
    Numberseq               num;
    String30                stop;
    AssetBookId             assetBookId;
    AssetAcquisitionDate    AcquisitionDate;
    String30                stringdate;
    Integer                 IncrementNum;
    String30                StrSub0, StrSub;
    String30                assetIdStr, Nums;
    Integer                 size;
    AssetAcquisitionDate    assetAcquisitionDate;

    ;

    ttsbegin;
    delete_from assetBook ;


    fileName = @"C:\\IMMOB\Immob_Rectif.csv";
    io = SysLicenseCodeReadFile::openFile(fileName,'r');
    if (!io)
        throw error(strfmt("@SYS18678",fileName));
    io.inFieldDelimiter(";");
     c = io.read();
    while (io.status() == IO_Status::Ok)
    {
        c = io.read();

        if (io.status() != IO_Status::Ok)
          break;

        assetBookId                 = conpeek(c,1);
        assetId                     = conpeek(c,2);
        qty                         = str2int(conpeek(c,3));
        StrSub0                     = substr(assetId,1,4);
        StrSub                      = substr(assetId,5,4);
        IncrementNum                = str2num(StrSub);
        assetAcquisitionDate        = str2date(conpeek(c,4),123);


        while (qty > 0)
        {
            size                = strlen(int2str(IncrementNum));
            Nums              = int2str(IncrementNum);
            while (size < 4 )
            {
                Nums          =  '0' + Nums;
                size = size + 1;
            }

            assetIdStr   = StrSub0 + Nums;
            if ( assetBook::Find(assetIdStr, assetBookId).RecId == 0)
            {
                assetBook.BookId            = assetBookId;
                assetBook.PostingProfile    = "IMMOB";
                assetBook.AcquisitionDate   = assetAcquisitionDate;
                assetBook.AssetId           = assetIdStr;
                assetBook.insert();
            }
            IncrementNum =  IncrementNum + 1;
            print IncrementNum;

            qty = qty -1 ;
        }
    }

    info("finished");
    info(strfmt(assetId));
    ttscommit;

}

How to use document management in a grid in Micrsofot Dynamics AX

For example you need to add service of docuement management to the inventTable Form, just add this display method to the inventTable Table and use this field as w window control in the grid of your form. Here is the method:

// Changed on 14 Jui 2011 at 15:53:43 by iba
//BP Deviation Documented
display smmDocIconNum showDocHandIcon()
{
    #macrolib.resource
    ;
    if ((select firstonly docuRef where docuRef.RefCompanyId  == this.DataAreaId && docuRef.RefTableId == this.TableId && docuRef.RefRecId == this.RecId).RecId)
    {
        return #RES_NODE_DOC;
    }

    return #RES_AM_NEW;


then you had to override this method in to window componant methods

// Changed on 12 Jui 2011 at 10:38:00 by iba
int mouseUp(int _x, int _y, int _button, boolean _ctrl, boolean _shift)
{
    #define.leftClick(1)

    int ret;
    ;

    ret = super(_x, _y, _button, _ctrl, _shift);

    if (InventTable && _button == #leftClick)
    {
        // Open the document handling form
        smmUtility::openMenuItemForm(menuitemdisplaystr(DocuView),InventTable,element,false);
    }

    return ret;
}


vendredi 22 juillet 2011

How to select records in a grid of a temporary table and enable/disable button according to these records

Here is a job in the active method of a temporary table of a dataSource of a form that shows how to pass records to another instance of a table to be able to read records ( using while)

// Changed on 19 Jui 2011 at 16:09:07 by iba
int active()
{
    int                             ret;
    TmpInventTransWMS               InventTransWMS;
    ;
    //<iba>
    InventTransWMS.setTmpData(TmpInventTransWMS);

    select firstOnly InventDimId from InventTransWMS
            where   InventTransWMS.Thy_ValidationField == false;

    //</iba>
    ret = super();


    tmpInventTransWMS_DS.setEnabled(!tmpInventTransWMS.RecId);

    tmpInventDim_DS.active();

    //<iba>
        if (InventTransWMS.InventDimId != '')
        {
            ctrlUpdateButton.enabled(false);

        }
        else
             ctrlUpdateButton.enabled(true);
    //</iba>
    return ret;
}

Here is an other example to run through records in a dataSource Grid :

//  Created on 21 Jui 2011 at 13:57:25 by iba
ItemId findDocNumItem(BomId    bomId)
{
    InventTable         inventTable;
    Bom                 localBom;
    ;
    localBom.setTmpData(Bom);
    select firstOnly ItemId from localBom
        join inventTable
            where localBom.ItemId == inventTable.ItemId
                && inventTable.ItemGroupId == InventParameters::find().Thy_ItemGroupId
                    && localBom.BOMId   == bomId;
    BomItemId =  localBom.ItemId ;
    return BomItemId;
}

mercredi 20 juillet 2011

How to check inserting records in a table

Here is an example of a  method that sets conditions before inserting records in a table

public boolean validateField(fieldId _fieldIdToCheck)
{
    boolean ret;
    ThyVendAccreditTable    Accredit;
    Str                     _year1,_year2;
    ;

    ret = super(_fieldIdToCheck);
    if(ret)
    {
        switch(_fieldIdToCheck)
        {
            case (FieldNum(ThyVendAccreditTable,Thy_vendScore)):
                if(this.Thy_VendScore>100)
                {
                    ret =checkfailed(strfmt("@THY8"));
                }
            break;
            case(FieldNum(ThyVendAccreditTable,ThyAccreditDate)):
            _year1 = date2str(this.ThyAccreditDate,123,0,0,0,0,4);
            while select accredit where accredit.VendAccount == this.VendAccount
            {
                _year2 = date2str(accredit.ThyAccreditDate,123,0,0,0,0,4);
                if(_year1==_year2)
                {
                    ret =checkfailed(strfmt("@THY9"));
                    break;
                }
            }
            break;
        }
    }

    return ret;
}

here is an example  to abort inserting and displaying an error message

//<iba>
        if (!InventTrans::existThy_InventVendNum(thy_InventVendNum,_inventTrans.CustVendAC))
        {
            _inventTrans.Thy_InventVendNum = thy_InventVendNum;
            _inventTrans.update();
        }
        else
        {
            checkFailed(strfmt("@THY10",thy_InventVendNum,_inventTrans.CustVendAC));
            return;
        }
       //</iba>

How to pass parameters from a temporary table to another table through a class

Hi 

here is a new example to pass a value from a temporary table TmpInventTransWMS to another table InventTrans passing through a class

In the clicked method of the button in your form InventTransRegister your have this code 

InventTransWMS_Register::updateInvent(element.args().record(), tmpInventTransWMS);

your have this method declared in  InventTransWMS_Register  class
server static public boolean updateInvent(
    Common                  _movementRecord,
    TmpInventTransWMS       _tmpInventTransWMS)
{
    InventMovement          movement    = InventTransWMS_Register::inventMovement(_movementRecord);

    return InventTransWMS_Register::updateInventFromMovement(movement, _tmpInventTransWMS);
}


which calls this method defined in the same class passing always  TmpInventTransWMS as parameter

server static public boolean updateInventFromMovement(
    InventMovement              _movement,
    TmpInventTransWMS           _tmpInventTransWMS) 

while select forupdate tmp
            order by InventQty
        {
            inventDim = InventDim::find(tmp.InventDimId);
            inventDimQuarantine.data(inventDim);

            if (movement.mustBeQuarantineControlled() && inventDim.inventLocation().InventLocationType == InventLocationType::Quarantine)
            {
                inventDim = InventQuarantineOrder::inventDimArriveOrRegistration(inventDim,movement.inventdim(),movement.itemId(),movement.dimGroupId());
            }

            inventDimParm.initPhysicalUpdate(movement.dimGroupId());

            setprefix(inventDim.preFix());

            registered = InventUpd_Registered::newParameters(movement,inventDim,inventDimParm,inventDim,inventDimParm,tmp.InventQty);

            if (movement.mustBeQuarantineControlled())
            {
                registered.parmInventDimQuarantine(inventDimQuarantine);
            }

            registered.parmPreferedInventTransRecId(tmp.RefRecId);
            //<iba>  
           registered.thy_TmpInventTransWMS(tmp.Thy_InventVendNum);
      
//</iba>

            registered.updateNow();
        }
In red, I added the new records to pass to InventTrans Table from TmpInventTransWMS
I created this method in InventUpdate , InventUpd_Registered extends from InventUpdate so it can call this method 


public class InventUpd_Registered extends InventUpdate
as follows 


// Changed on 19 Jui 2011 at 14:46:13 by iba 
void thy_TmpInventTransWMS (Thy_InventVendNum tmp)
{
    ;
    thy_InventVendNum = tmp;
}


Finally after passing the records through this method, I added a little code in the inventUpdate Class as follows 
if (_inventTrans.RecId)
    {
        //<iba>
       _inventTrans.Thy_InventVendNum = thy_InventVendNum;
       //</iba>
       _inventTrans.update();
    }
To take in consideration the new records 
I had also to pass these records to aonther table InventSerial so I added this code
in writeInventTransAutoDim method  of InventUpdate class.

if (!InventSerial::exist(movement.itemId(), inventDim.inventSerialId))
{
    inventSerial.InventSerialId          = inventDim.inventSerialId;                           inventSerial.initFromInventMovement(movement, _inventTrans);
     //<iba>
     inventSerial.Thy_InventVendNum      = thy_InventVendNum;
     //</iba>
     inventSerial.insert();
}