How to using X++ code achieve copying records

This X++ Code Snippet post describes how you can easily copy records by running over all of the fields of the from-record. During this looping over all the fields, you can exclude some fields if needed. 

 

Create following method in a class: Jimmy_Global

static void Jimmy_CopyoingField(Common _from, Common _to)
{
    DictTable           t;
    DictField           f;
    
int                 i;
    fieldId             id;
    FieldName           fieldname;
;
    t 
= new DictTable(_to.TableId);
    
if (t)
    {
        
for (i = 1; i <= t.fieldCnt(); i++)
        {
            id 
= t.fieldCnt2Id(i);
            f  
= new DictField(_to.TableId,t.fieldCnt2Id(i));
            
// excluding system fields (or add other fields to exclude during the copy action
            if (f && !f.isSystem())
            {
                _to.(id) 
= _from.(id);
            }
        }
    }
}

To copy for example a VendTable to a new one, just call your class like this:

YourClass::CopyFields(VendTable , VendTableTo);

VendTableTo.insert();

原文地址:https://www.cnblogs.com/Fandyx/p/2126967.html