Outsystems Certified Discussion

[Tip] How to correctly iterate a Record List inside an Extension

thumbs_up_ico12thumbs_down_ico0
Hi guys,
 
In the last few months I've been seeing lots of Extensions with incorrect patterns used for iterating Record Lists.
These wrong patterns can produce wrong and unexpected behaviors/results, the most common being missing some entries when doing the iteration.
 
This post shows how to correctly iterate a Record List in an extension.
 
The following code snippet illustrates the correct pattern:
 
 
    try {
                                                                             
        // Call Record List "StartIteration" to ensure the iteration starts from the beginning of the list
        recordList.StartIteration();
                                                                                             
        // Record List "Eof" property indicates if we're in the end of the list
        while (!recordList.Eof) {

            // access "CurrentRec" to retrieve current record information
            string attrName = recordList.CurrentRec.ssSTMyEntity.ssName;
 
            // call "Advance" to move to the next record in the list
            recordList.Advance();
        }
    }  finally {
                                                                           
        // in the finally, call "EndIteration" to indicate that we're no longer iterating the record
        // we must call this function to allow other record list operations, e.g., list-append, list-remove, etc
        recordList.EndIteration();
    }
 
If you are using extensions developed in your company and have actions that use Record Lists, and are having unexpected behavior, you should look into the source code to check if the correct iteration pattern is being used, and fix it if that is the case.
 
If you have any question or comments, please let me know.
 
 
Regards,
João Portela
thumbs_up_ico4thumbs_down_ico0
By the way, the Java code is very similar:

    try {
                                                                             
        // Call Record List "
startIteration" to ensure the iteration starts from the beginning of the list
        recordList.
startIteration();
                                                                                             
        // Record List "Eof" property indicates if we're in the end of the list
        while (!recordList.
isEof()) {

            // access "
currentRec" to retrieve current record information
            string attrName = recordList.
currentRec.ssSTMyEntity.ssName;
 
            // call "
advance" to move to the next record in the list
            recordList.
advance();
        }
    }  finally {
                                                                           
        // in the finally, call "
endIteration" to indicate that we're no longer iterating the record
        // we must call this function to allow other record list operations, e.g., list-append, list-remove, etc
        recordList.
endIteration();
    }


Regards,
Rui Eugénio

thumbs_up_ico0thumbs_down_ico0
Great tip guys. Definitely one to go to the Guides and How Tos forum section.

Regards,

Paulo Tavares
thumbs_up_ico0thumbs_down_ico0
Hello all

And what about in the opposite direction?
Fill a Record List inside an extension and pass it to OS?
Is there an How to?

Antonio
thumbs_up_ico4thumbs_down_ico0
Hi António,

Follows a sample how to insert a record (entity/structure) in a record list, in a extension:


        public void MssExportLists(out RLMyEntityRecordList ssMyEntityList, out RLMyStructureRecordList ssMyStructureList) {
           
            // entity record list creation
            ssMyEntityList = new RLMyEntityRecordList(null);

            // entity record creation
            RCMyEntityRecord ssMyEntityRecord = new RCMyEntityRecord (null);
            ssMyEntityRecord.ssENMyEntity.ssParam1 = "value 1 of entity record";
            //ssMyEntityRecord.ssENMyEntity.ssParam1 = "value 1 of entity record";

            // append entity record to the entity record list
            ssMyEntityList.Append( ssMyEntityRecord);
           
            // structure record list creation
            ssMyStructureList = new RLMyStructureRecordList(null);

            //structure record creation
            RCMyStructureRecord ssMyStructureRecord = new RCMyStructureRecord(null);
            ssMyStructureRecord.ssSTMyStructure.ssParam1 = "value 1 of entity record";
            //ssMyStructureRecord.ssSTMyStructure.ssParam1 = "value 1 of entity record";

            // append structure record to the structure record list
            ssMyStructureList.Append(ssMyStructureRecord);

        } // MssExportLists


Relevant information:
  • when creating a Record object (RC<name>Record), always use the construcutre with null parameter, this constructior performs internal initializations (the constructor without parameter won't do)
  • use the "Append" function of the record list to append a single record
I've also attached a sample extension.

Notice: I've created this extension in Integration Studio 7.00.14 . In other versions/revisions there could be changes in the record/structure/record-lists internal structure.


Regards,
João Portela
thumbs_up_ico0thumbs_down_ico0
Hi João and Rui,
Excellent performance tips..
Share more tips of this kind..:)

Cheers,
Gonçalo Martins
thumbs_up_ico0thumbs_down_ico0
Awesome.
It is working..  :-)