Saturday, 25 April 2015

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

2 comments:

  1. Many thanks! The code works very well!

    ReplyDelete
  2. Many thanks too for the article. But please how would perform this action in D365 FO?

    ReplyDelete

Adding a newline into a string in C# and X++

Below is the sample code we can use for  adding a newline after every occurrence of "@" symbol in the string in C#   using System...