How to add a value to an array

Hi all. Make newbie here, so bare with me.

I am working with the Slack APIs, and i want to update a user group. Turns out the “usergroups.users.update” API call will update the user group, with whatever is in the request, therefore removing any previous users.

So what i’m doing is, running a request to get the list of current users, which comes out as in Array format. And i then just want to append/add to this array, and then use this in the request to update the user group.

I have a request, which dumps the current UserIDs (members) for the group, that gives me an output like below

Screenshot 2024-07-11 at 7.00.01 PM

The output data is:

[
{
“body”: {
“ok”: true,
“users”: [
“U04GZM4GA80”,
“U07AJ00BEQ5”,
“U07APN6P668”
]
},
“headers”: {
“date”: “Thu, 11 Jul 2024 08:54:54 GMT”…

Then i have another request, which gets the UserID of the new member i want to add, that gives me an output like below

Screenshot 2024-07-11 at 7.02.26 PM

The output data is:

[
{
“id”: “U04GZM4GA80”,
“team_id”: “T02G758BAH3”,
“name”: “john.smith”,
“deleted”: false,
“color”: “9f69e7”,
“real_name”: “John Smith”,…

I’ve tried using various itterators, parsers and array aggregators, but…no luck. And i think i’m missing something obvious.

Is there an easy way todo this, so i end up with the array with all the UserID’s i need to send off to the Slack API to “update” the members of the group.

Thanks

Hello! Welcome to the Make community!

I don’t know the exact payload required by your update, but in any case, you could try and use the add function to create a new array that contains the new userID.

This is my initial array

I use the add function (the d would be the new User ID you map from the previous call)

This is the new Array that contains old data + the new ID

This new array can then be mapped. If it’s part of a broader payload, you may have to rebuild it. If you are stuck, let me know so that I have a look

Benjamin

4 Likes

Aha! Benjamin, thank you mate.

I wasn’t aware of the functions, and specifically the “Add” function.
I’ve now got a combined array (existing userid list + the additional userid) which i can pass into the request to update the group membership.

Thank you so much.

I WILL follow back up on this post with what i did, as i think theres more refining i can do to it, and am keen to share the working result for anyone else interested in it. But it’s late, and i need to turn my brain off from Make for the night :smiley:

Thanks Benjamin!

1 Like

Promised i’d reply back with the working solution.

So here’s the scenario

  • Gets the UserID for the user i want to add to the group.
  • Gets the existing list of UserIDs of the members of the group (I just specified the group ID in here)
  • Combines the list of existing UserIDs with the new UserID to be added.
  • Converts the format into JSON (This step might not be needed, but i had issues otherwise)
  • Updates the list of members of the group.

Slack - Get list of users in group

Tools (Combine the existing list of UserIds with the new one
image

Example output of this step is

[
    {
        "users": [
            "U07AJ00BEQ5",
            "U04GZM4GA80"
        ]
    }
]

JSON (Converts the output from the previous step into JSON
image

Example output of this step is

[
    {
        "json": "[\"U07AJ00BEQ5\",\"U04GZM4GA80\"]"
    }
]

Adds all users to the group

Example input of this step is

[
    {
        "url": "/usergroups.users.update",
        "body": "{\"usergroup\":\"S07BNTLFUUF\",\"users\":[\"U07AJ00BEQ5\",\"U04GZM4GA80\"]}",
        "domain": "https://slack.com/api/",
        "method": "POST",
        "headers": [
            {
                "key": "Content-Type",
                "value": "application/json"
            }
        ]
    }
]

This works fine. But i wonder if i’ve overcomplicated it a little, and don’t need the JSON convert step. But i had to add it, as it was the only way i could get the data into the right format.

Thanks again Benjamin (promise last time i say that…was a bit weird with my 3 “thanks” on my porevious post. LOL)

3 Likes

Hey!

Thank you SO MUCH for sharing your final solution!!

The Transform to JSON is mandatory because in the previous steps, the data is in the « Make » format so that you can map fields; you need to transform it back to a json string before you add it in the global payload of the last step.

However, you could save one operation if you don’t use the Set Variable and put the add(……) directly in the Transform to JSON.
The Add function will be evaluated and the result will be transformed to a json string.

Good luck with your automations! :smiling_face:

Benjamin

3 Likes

Ahhh, that makes sense. i assumed the output might already be in JSON, but now i know theres the “Make” format, i can remember that for next time.

I’ll play around with adding the function into the Transform to JSON.

Cheers Benjamin, have a good weekend

2 Likes

And yup, correct by Benjamin, i could ditch the Task to add the UserIDs together, and just added them into the Transform to JSON

Woot Woot

2 Likes