Issue while batching list of text string

Certified Associate Developer

Hi Team,

Im trying a batch list of text items and prepare a query based on that. Below is my code snippet. With this code Im getting the last item of each list for the full batch of 50 items.

local!getAgreementIds: if(
    rule!APN_isBlank(
      index(
        index(local!PartnersResult, "data", null),
        "agreement__cr.id",
        null
      )
    ),
    index(
      local!AgreementResult.data,
      "agreement__cr.id",
      null
    ),
    index(
      local!PartnersResult.data,
      "agreement__cr.id",
      null
    )
  ),
  local!agreementDocIds: touniformstring(rule!APN_distinct(local!getAgreementIds)),
  
  local!batchedDocIds: a!forEach(
    items: enumerate(ceiling(length(local!agreementDocIds) / 50)),
    expression: local!agreementDocIds[
      (fv!item * 50) + 1 :
      min((fv!item + 1) * 50, length(local!agreementDocIds))
    ]
  ),
   

For ex: I have a list of 59 items, when I try to batch I should get 50 items in 1 batch and remaining 9 items in another batch. But Im getting last item of each batch that is 50th item in first batch and 59th item in 2nd batch. Can someone please help 

  Discussion posts and replies are publicly visible

  • Use this logic:

    a!localVariables(
    /* Input: list of items to process */
    local!data: ri!items,
    /* or pv!allItems */
    /* Batch size */
    local!batchSize: 50,
    /* Compute number of batches */
    local!numBatches: tointeger(
    if(
    mod(length(local!data), local!batchSize) = 0,
    length(local!data) / local!batchSize,
    (length(local!data) / local!batchSize) + 1
    )
    ),
    /* Split manually into batches */
    a!forEach(
    items: enumerate(local!numBatches),
    expression: with(
    local!startIndex: fv!item * local!batchSize + 1,
    local!endIndex: min(
    {
    local!startIndex + local!batchSize - 1,
    length(local!data)
    }
    ),
    /* build the batch */
    a!forEach(
    items: enumerate(local!endIndex - local!startIndex + 1),
    expression: index(local!data, local!startIndex + fv!item, {})
    )
    )
    )
    )

  • 0
    Certified Lead Developer

    The following code turns a list of things into a list of lists of things.

    if(
      or(
        a!isNullOrEmpty(ri!items),
        a!isNullOrEmpty(ri!segmentSize)
      ),
      ri!items,
      a!localVariables(
        local!numSegments: ceiling(count(ri!items) / ri!segmentSize),
        if(
          ri!rotate,
          /* WITH ROTATION: segmentSize means the number of segments */
          if(
            ri!enablePadding,
            /* WITH PADDING: Adds type casted NULL values to the last segment to fill up to the size of the segment */
            a!forEach(
              items: enumerate(ri!segmentSize),
              expression: index(
                ri!items,
                1 /* First item in list is at index 1 and enumerate creates numbers starting at 0 */
                + fv!item /* Start number for current segment */
                /* Fix segment size */
                + enumerate(local!numSegments)
                * ri!segmentSize,
                cast(runtimetypeof(ri!items[1]), null)
              )
            ),
            /* WITHOUT PADDING: Only add left over items into the last segment. The last segment might contain less items */
            a!forEach(
              items: enumerate(ri!segmentSize),
              expression: reject(
                a!isNullOrEmpty(_),
                index(
                  ri!items,
                  1 /* First item in list is at index 1 and enumerate creates numbers starting at 0 */
                  + fv!item /* Start number for current segment */
                  /* Adjust the size of the segment to either the required size or for the last segment the left over items */
                  + enumerate(local!numSegments)
                  * ri!segmentSize,
                  null
                )
              )
            )
          ),
          /* WITHOUT ROTATION: segmentSize means the number of items per segment */
          if(
            ri!enablePadding,
            /* WITH PADDING: Adds type casted NULL values to the last segment to fill up to the size of the segment */
            a!forEach(
              items: enumerate(local!numSegments),
              expression: index(
                ri!items,
                1 /* First item in list is at index 1 and enumerate creates numbers starting at 0 */
                + (fv!item * ri!segmentSize) /* Start number for current segment */
                /* Fixe segment size */
                + enumerate(ri!segmentSize),
                cast(runtimetypeof(ri!items[1]), null)
              )
            ),
            /* WITHOUT PADDING: Only add left over items into the last segment. The last segment might contain less items */
            a!forEach(
              items: enumerate(local!numSegments),
              expression: index(
                ri!items,
                1 /* First item in list is at index 1 and enumerate creates numbers starting at 0 */
                + (fv!item * ri!segmentSize) /* Start number for current segment */
                /* Adjust the size of the segment to either the required size or for the last segment the left over items */
                + enumerate(min(ri!segmentSize, count(ri!items) - (fv!item * ri!segmentSize))),
                null
              )
            )
          )
        )
      )
    )

  • 0
    Certified Lead Developer

    You can batch a List of Text String (or any list) using a reliable slicing pattern with index().
    Range slicing inside a!forEach() often returns only the last item, so this approach avoids that issue.

    you can refer this code

    a!localVariables(
    local!items: local!agreementDocIds,
    local!batchSize: 50,

    /* total number of batches */
    local!numBatches: ceiling(count(local!items) / local!batchSize),

    /* list of lists */
    a!forEach(
    items: enumerate(local!numBatches),
    expression: index(
    local!items,
    1 + (fv!item * local!batchSize),
    min((fv!item + 1) * local!batchSize, count(local!items))
    )
    )
    )