Wednesday, December 3, 2008

Useful methods for sets / record2set example

I find myself working with sets and maps a lot. They are a fast and easy way to buffer information without going back to the database all the time. 
The set object has a few nice methods to compare sets. 
Union 
Pass two sets to this method. The method returns a new set that includes all items from either set.  
Difference 
Pass two sets to this method. The return set includes all items from the first set that are not found in the second set. 
Intersection 
Pass two sets to this method. The method return set includes all items found in both sets. 
The following example illustrates the three methods. record2Set creates a set out of any table record (for simplicity arrays are excluded here). The format is fieldname#value The method is applied to two customer records. Check out the results you are getting:  
Union proves not very useful in this example, it has all the fields, and two entries if values are different. 
Difference is interesting. These are the differences in the two records. Note that the values are from whichever set you supplied first. 
Intersection. All identical fields. Differences are dropped altogether.

static void JobSetExamples(Args _args) 
    custTable   custTable_1; 
    custTable   custTable_2; 
    Set         setCustTable_1; 
    Set         setCustTable_2; 
    Set         compare; 
    set record2set(common _record) 
    { 
        DictTable       dictTable; 
        DictField       dictField; 
        FieldId         fieldId; 
        Set             recordSet; 
        ; 
        dictTable = new DictTable(_record.tableId); 
        recordSet = new Set(Types::String); 
        for (fieldId = dictTable.fieldNext(0);fieldId;fieldId = dictTable.fieldNext(fieldId)) 
        { 
            dictField = dictTable.fieldObject(fieldId); 
            if (!dictField.isSystem() && dictField.arraySize() == 1) 
                recordSet.add(strfmt("%1#%2",dictField.name(),_record.(fieldId))); 
        } 
        return recordSet; 
    } 
    ; 
    custTable_1            = CustTable::find('4000'); 
    custTable_2            = CustTable::find('4000'); 
    custTable_1.AccountNum = "1234"; 
    custTable_1.Name       = "Test"; 
    custTable_2.CreditMax  = 333.33; 
    setCustTable_1 = record2set(custTable_1); 
    setCustTable_2 = record2set(custTable_2); 
    compare = Set::difference(setCustTable_1,setCustTable_2); 
    print compare.toString(); 
    compare = Set::difference(setCustTable_2,setCustTable_1); 
    print compare.toString(); 
    compare = Set::intersection(setCustTable_1,setCustTable_2); 
    print compare.toString(); 
    compare = Set::union(setCustTable_1,setCustTable_2); 
    print compare.toString(); 
    pause; 
}

No comments:

How to identify the user that was used to change an object from AOT in AX2012

Get the object name for which we need to track these (user and date&time) information's. Login to SQL Server Management Studio an...