Can we exit from a!forEach() loop?

Certified Senior Developer

Hi Everyone,

Can we exit from a!forEach() loop? 

I have a use case , where I need to iterate through the list of users and once the condition met ( checking for the user's organization and weight category), I have to stop iterating through the rest of the items. I need order to achieve this, I have to exit from the loop. 

How can I achieve this ?

Apologies for not clarifying the use case earlier.

Use case is given below.

Use Case: There is a list of Boxers. Each boxers belongs to particular organization and weight category. I want to select 2 boxers for the match randomly. 2 boxers must belong to same weight category but different organization.

To achieve this I was trying to loop through list of boxers and fetch 2 boxers belonging to same weight category but different organization. Once I get 2 different boxers, then I wanted to exit the loop, because I got 2 boxers for the match and don't want to loop further. Note: Weight category and organization are not known initially. That also needs to be picked randomly.

Please do let me know if this is still not clear.

  Discussion posts and replies are publicly visible

  • 0
    Certified Senior Developer
    in reply to Chris

    Hi ,,,

    I have updated the use case now.

  • 0
    Certified Senior Developer

    I guess you can achieve it by using queryAggregation or GROUP BY if done in SQL script, and add randomizer for index.

    Something similar to this:

    a!localVariables(
      local!boxers: a!queryEntity(
        entity: cons!MFA_BOXER_LIST_DSE,
        query: a!query(
          aggregation: a!queryAggregation(
            aggregationColumns: {
              a!queryAggregationColumn(
                field: "organization",
                isGrouping: true
              ),
              a!queryAggregationColumn(
                field: "categoryId",
                isGrouping: true
              ),
              a!queryAggregationColumn(
                field: "id",
                aggregationFunction: "MAX"
              )
            }
          ),
          pagingInfo: a!pagingInfo(
            startIndex: 1,
            batchSize: 50
          )
        ),
        fetchTotalCount: true
      ),
      
      local!count: local!boxers.totalCount - 1,
      local!index: odd(rand() * local!count),
      
      local!boxers.data[{local!index, local!index + 1}]
    )

  • 0
    Certified Senior Developer
    in reply to eunnel

    Thanks for the response. This would return boxer under groups(Organization & Weight Category) . However, I still need to go through the list of boxers to get a match isn't it?

  • +1
    Certified Senior Developer
    in reply to Beena Venugopal

    Yes, I made it like this

    a!localVariables(
      local!boxer: a!queryRecordType(
        recordType: 'recordType!{74852bd6-d06a-4435-b608-70b4779e96eb}MFA Boxers',
        fields: a!aggregationFields(
          groupings: {
            a!grouping(
              field: 'recordType!{74852bd6-d06a-4435-b608-70b4779e96eb}MFA Boxers.fields.{f4e1f3f8-bc8c-4432-a327-8add89ee8307}categoryId',
              alias: "categoryId"
            ),
            a!grouping(
              field: 'recordType!{74852bd6-d06a-4435-b608-70b4779e96eb}MFA Boxers.fields.{abb9bb86-46e6-4e5f-b949-6aec81eaa7b6}organization',
              alias: "organization"
            ),
            a!grouping(
              field: 'recordType!{74852bd6-d06a-4435-b608-70b4779e96eb}MFA Boxers.fields.{0cf8dde6-3347-44c2-b9a6-777ac23cd35e}name',
              alias: "boxerName"
            )
          }
        ),
        pagingInfo: a!pagingInfo(
          startIndex: 1,
          batchSize: 50
        ),
        fetchTotalCount: true
      ),
      
      rule!MFA_BoxerIterations(boxer: local!boxer.data, totalCount: local!boxer.totalCount)
    )

    MFA_BoxerIterations

    a!localVariables(
      local!count: ceiling(rand() * (ri!totalCount-1)),
      local!index: if(
        local!count >= ri!totalCount,
        ri!totalCount - 1,
        local!count
      ),
      local!match: ri!boxer[tointeger({ local!index, local!index + 1 })],
      /*local!match: index(ri!boxer, tointeger({ local!index, local!index + 1 }), null),*/
      
      if(
        local!match.categoryId[1] <> local!match.categoryId[2],
        rule!MFA_BoxerIterations(boxer: ri!boxer, totalCount: ri!totalCount),
        local!match
      )
    )


    Result:

    Hope this helps.

  • 0
    Certified Senior Developer
    in reply to eunnel

    Thanks much Chris. This looks good. I have somewhat executed in the same way. But this looks way more reliable than mine.