Wednesday, August 22, 2018

Table Buffer2Buffer by comparing source and target table fields in Dynamics AX


Public static void buffer2buffer(Common _source, Common _target)
    {
        TableScope scope = TableScope::CurrentTableOnly;
        DictTable   sourceDictTable = new DictTable(_source.TableId);
        DictTable   targetDictTable = new DictTable(_target.TableId);

        FieldId     sourceFieldId   = sourceDictTable.fieldNext(0, scope);
        FieldId     targetFieldId   = targetDictTable.fieldNext(0, scope);

        Map targetFields = new Map(Types::String, Types::Int64);

        //Adding target table fields in to Map
        while (targetFieldId && ! isSysId(targetFieldId))
        {
            targetFields.insert(targetDictTable.fieldName(targetFieldId), targetFieldId);
            targetFieldId         = targetDictTable.fieldNext(targetFieldId, scope);
        }  
        
       //Retrieving source field map and comparing with target table fields to set field values
        while (sourceFieldId && ! isSysId(sourceFieldId))
        {
            if (targetFields.exists(sourceDictTable.fieldName(sourceFieldId)))
            {
                targetFieldId = targetFields.lookup(sourceDictTable.fieldName(sourceFieldId));
                _target.(targetFieldId)   = _source.(sourceFieldId);
            } 
            sourceFieldId         = sourceDictTable.fieldNext(sourceFieldId, scope);
        } 
    }


No comments:

Post a Comment

Copy Markup charges while posting purchase invoice using X++

 Copy Markup charges while posting purchase invoice using X++ Class: Important: Code logic is just for Reference.  New class => Duplicate...