Copy Data From One Table To Another Table By Using buf2buf- Ax 2012

Hello Guys...!

Some times we get requirement like , we have to copy data from one table to other table .

For that we use buf2buf()  Global method. This method we can use if our second table fields Id's are same as First table field Id's.

If not then we need to do some changes in buf2buf() method

Example :

-> Here i duplicated "InventTable" and named "CopyInventTable"
->I'm copying data from InventTable to CopyInventTable by using Buf2Buf method

Step 1:

Go to Class-> Global-> buf2buf() method and paste below code

static void buf2buf(Common  _from, Common  _to)
{
    DictTable   dictTableFrom   = new DictTable(_from.TableId);
    DictTable   dictTableTo     = new DictTable(_to.TableId);
    DictField   dictFieldFrom;
    FieldId     fieldIdFrom     = dictTableFrom.fieldNext(0);
    FieldId     fieldIdTo
    ;


    while (fieldIdFrom && ! isSysId(fieldIdFrom))
    {
        dictFieldFrom   = new DictField(_from.TableId, fieldIdFrom);


        if(dictFieldFrom)
        {
            fieldIdTo = dictTableTo.fieldName2Id(dictFieldFrom.name());


            if(fieldIdTo)
                _to.(fieldIdTo) = _from.(fieldIdFrom);
        }


        fieldIdFrom = dictTableFrom.fieldNext(fieldIdFrom);
    }
}

Step 2 :

Paste the below code in AOT -> Jobs and new Job


static void CopyData(Args _args)
{
    InventTable inventTable;
    CopyInventTable copyInventTable ;
    ;
 
    while select inventTable
    {
         buf2Buf(inventTable ,copyInventTable );
         copyInventTable .insert();
    }
    info("Done");
}




That's It.


@Rahul Talasila

Comments

Post a Comment

Popular posts from this blog

Filter Records on Item Lookup Through X++ - Ax 2012

How to Install Management Reporter in Ax 2012 R3

Settle Vendor Invoice/Payment through X++ code - AX 2012