How to create a correct sequence of requests with looping?

Dear community,

could someone explain to me how to create a correct sequence of requests when I have for example list of Items that I want to add to a cart one by one? So it will be the same request but with another body parameter (with another item code).

And in addition, I need to store some values from the response that I received when calling the endpoint in looping.

I have read about this one - Gatling - Scenario
But I can understand why it isn’t work for me

I created this one:

group("User add items to the cart")
            .on(
                    foreach( session -> prepareTestData().getRandomItemsList(2), "item")
                            .on(
                                    exec(
                                            session -> {
                                                userAddItemToCart("#{token}","analytics", item)
                                                        .check(status().is(Status.OK))
                                                        .check(jsonPath("$.var.item.Code").saveAs("childItemCode"))
                                                        .check(jsonPath("$.var.base.code").saveAs("parentItemCode"))
                                                        .check(jsonPath("$.id").saveAs("id"));

                                                return session;
                                            }
            )));

public static @NotNull HttpRequestActionBuilder userAddItemToCart(String token, String context, String itemCode) {
        var path = baseUrl + "/list";
        var contextParamName = "context";
        var stringBody = switch (context.toLowerCase(Locale.ROOT)) {
            case "some_value" -> """
                    {
                      "item_code": "%s",
                      "style_code": "%s"
                    }
                    """;
            case "analytics" -> """
                    [
                      {
                        "item_code": "%s",
                        "style_code": "%s"
                      }
                    ]
                    """;
            default -> "";
        };

        return http("POST ".concat(path))
                .post(path)
                .headers(Map.of(Headers.CONTENT_TYPE, Headers.APPLICATION_JSON, Headers.AUTHORIZATION, token))
                .queryParam(contextParamName, context)
                .body(StringBody(
                        stringBody.formatted(
                                itemCode,
                                itemCode.substring(0, parentCode.length() - 3))
                ));
}

Maybe there are other options that could be used in this case? Maybe is on a simulation level like a feeder. Or another looping?

Many thanks!

Maybe this will help you:

I found the problem. It’s my bad, the code in the “session” block cannot be executed, I mean the request itself. After moving the execution of the request from the code of the session block, removing the session itself from the foreach and from the exec() function, everything worked as it should.