0

I have a list of string something like below

String roleId=105775004,105789004,105809004,105816004,105826004,105844004,105850004,105852004,105863004,105939004,105944004,105953004,105960004,105975004,105985004,106025004,106035004,106056004,106080004,106107004,106110004,106115004,106130004,106148004,106152004,106171004,106172004,105357004,105370004,105375004,105378004,105394004,105401004,105402004,105405004,105415004,105416004,105437004,105444004,105464004,105475004,105493004,105503004,105523004,105533004,105555004,105563004,105574004,105583004,105587004,105594004,73148004,73194004,73196004,73197004,73403004,74319004,74505004,75089004,75248004,75450004,75452004,75668004,76238004,76256004,76381004,76499004,77224004,78985004,79195004,79512004,81467004,81866004,82140004,82269004,82346004,82529004,82804004,82961004,82969004,82974004,82984004,83156004,83532004,83685004,8375700

i want to divide into every 20 string to pass as input query params for my api call ?.How would i do that ? Thanks in Advance.

            Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('http://api.json.com/rest/test.json?&method=getbyQuery&id='+roleId);
        request.setMethod('GET');
        request.setTimeout(120000);
        request.setHeader('Content-Type','application/json'); 
        HttpResponse response = http.send(request);
        
        if (response.getStatusCode() == 200)
        {
        }
1

The easiest approach would be to split the string then build sub-arrays and join the values back up, something like:

List<String> roleIds = roleIdString.split(',');

while (!roleIds.isEmpty()) {
    List<String> roleIdSubset = new List<String>();

    for (Integer i = 0; !roleIds.isEmpty() && i < 20; i++) {
        roleIdSubset.add((String) roleIds.remove(0));
    }

    if (!roleIdSubset.isEmpty()) {
        String roles = String.join(roleIdSubset, ',');

        // do your callout with the roles
    }
}

Of course, you need to be aware on limits on the number of callouts you can do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.