Mm77
Mm77

Reputation: 95

How to send requests to api in batches in logic app

I have a logic app in which I create a json request using javascript inline code and send that to an api. The json request might have more than 1000 requests in it. I need to send those requests in the batches of 200 to api. How do I achieve batching in logic app?

Upvotes: 0

Views: 548

Answers (1)

Hury Shen
Hury Shen

Reputation: 15754

For this requirement, please refer to the js code below:

//First define a function to do the separate arry operation
function group(array, subGroupLength) {
    let index = 0;
    let newArray = [];
    while(index < array.length) {
        newArray.push(array.slice(index, index += subGroupLength));
    }
    return newArray;
}

//Then use the "group(array, subGroupLength)" function to separate your array to multiple 200 records array
var resultArray = group(json.records, 200);

return resultArray;

Afer the code, the resultArray should be like:

[
  [
    {
        "attributes": {
            "Id": ""
        },
        "Property": ID1,
        "Type": ""
    },
    .....
    //200 items
    .....
    {
        "attributes": {
            "Id": ""
        },
        "Property": ID1,
        "Type": ""
    }
  ],
  ....
  //five sub array
  ....
  [
    {
        "attributes": {
            "Id": ""
        },
        "Property": ID1,
        "Type": ""
    },
    .....
    //200 items
    .....
    {
        "attributes": {
            "Id": ""
        },
        "Property": ID1,
        "Type": ""
    }
  ]
]

===============================Update===========================

For your new requirement about give all those sub arrays a name(same name for all sub arrays), I modified the code of function group(array, subGroupLength) like below:

function group(array, subGroupLength) {
    let index = 0;
    let newArray = [];
    while(index < array.length) {
        let item = array.slice(index, index += subGroupLength);
        let itemObj = {"subArray": item};
        //newArray.push(array.slice(index, index += subGroupLength));
        newArray.push(itemObj);
    }
    return newArray;
}

In my code, the name for each sub array is subArray. Please check if it meet your requirement.

Upvotes: 1

Related Questions