There is an external system, from which I need to pull (via REST request) information about, let's say, brands and their categories. After the request, I need to collect the data in such structure:

public class Wrapper {
    String brandId;
    List<String> categories;

Then I need to insert records in Salesforce, but due to the huge process, there is also a limitation of, let's say, 5 records during the transaction. That's why in the case when there are more than 5 categories for one brand, I need to make 2 Wrappers.

Example: We have 2 brands:

  1. "Abibas" with 6 categories ("Shoes", "Hats", "Shorts", "Sweaters", "Socks", "Balls");
  2. "Nite" with 2 categories ("Clothes", "Sports")

I need the logic to distribute the data above to this structure:

[ {
  "categories" : [ "Shoes", "Hats", "Shorts", "Sweaters", "Socks" ],
  "brandId" : "Abibas"
}, {
  "categories" : [ "Balls" ],
  "brandId" : "Abibas"
}, {
  "categories" : [ "Clothes", "Sports" ],
  "brandId" : "Nite"
} ]

Please advise not heavy solution to distribute records in described structure.

  • @identigral no, the question is not only how to create a wrapper from JSON. Commented Jun 23, 2022 at 18:16

1 Answer 1


Let's say a brand (xyz) has more than 5 categories. Do the serialization of your JSON response into the wrapper. This would mean the element for the brand xyz would have more than 5 categories in the "categories" array. Next step would be iterate over your list of wrappers and create new array element for such brands (and remove the extra categories from the og element).

There is no inbuilt array split method in apex, you may want to look at some utilities built by the community or write your own split method. Ref : Apex Array Slice

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .