In a client action I am hitting an API that returns multiple objects in one array like this:
[
{"id": 1234, "type": "calendar", "name": "My Calendar"},
{"id": 5678, "type": "calendar", "name": "Holidays"},
{"id": 9012, "type": "calendar", "name": "Emailed Events"},
{"id": 7894, "type": "calendar", "name": "Birthdays"}
]
The last step of the client action is to run some custom JavaScript and that custom script needs the IDs of the "My Calendar" and "Holidays" calendars. How do I do this in OutSystems? It will always only be those two values that I need, so adding input parameters to my JavaScript seems like the way to go. But, how do I get the "id" values for the objects based on their "name" values?
Cheers,
Daniel
Are you hitting the client api using Outsystems REST Consume API? or directly sending http requests from your client action using some javascript or another component?
Here's a standard way of doing this in Outsystems (Refer it if you're not already doing this)
https://success.outsystems.com/Documentation/11/Extensibility_and_Integration/REST/Consume_REST_APIs/Consume_one_or_more_REST_API_methods
If you're already using this method for consume the REST APIs, then it should anyway return you a list object with calendar holiday items. The list can then be used in different ways to match your requirements.
Hey Ravi,
Thanks for the reply. I am indeed following the standard method. However, I still don't quite understand how to isolate just the two objects I need. I can only see how to get values from the "current" object. In my live use case the first object returned isn't the one I want, but when I choose GetCalendars.Response.Current.Id I only get the ID value for the first object. Ideally, I'd create the array in the expression editor and include just the id values for "My Calendar" and "Holidays." So, the above is the response from my API call, and here's what I'd like to put into an input variable:
["1234","5678"]
Does that make sense?
-Daniel
Alright I "think" I got it now. If you want to extract from the response, a list of IDs only, here's how you do it.
Let me know if I didn't understand your requirement correctly.
Where do I put the new CalendarIDs variable? I tried adding it as an ouput parameter to the GetCalendars call, but I'm getting this error, "Required Output Parameter: 'GetCalendars' method requires a single output parameter with 'List' or a Structure data type and the Receive In property set to 'Body'. Set a single output parameter, or change the method's response format."
Response output structure doesn't need to change. The new variable comes in after you finish calling the GetCalendars API and received the response in List response full object.
Wrap your API call into a server action and you can do the new variable inside the wrapper server action. Then in your application, call this action, instead of directly calling the REST API Action.
I'm not sure I can run it as a server action because it depends on the user. The first step is fetching data from a User Preferences entity that stores the access token for that API, and it will only return the access token for the logged in user.
Here's what I've got so far in the client action:
and the CalendarIds variable assignment:
It's a text list variable because those id values are actually coming across as text, not just an integer.
However, when I test that in my app, it's still only getting the first ID value from the first object in the JSON response.
All actions triggering from a user's screen will still allow calling server actions, without any issues.
Pleae take it as a good advise for your nice and cool future apps. I think you'll need a bit more learning on how this platform can be used. There are very nicely curated videos on each topic. I saw from your profile you haven't finished the quick start videos to understand many basic concepts in Outsystems development.
You might not find the platform great for you, unless you now how to use it, or end up in finding the experience not so great, which can be largely impacted from what you know about the platform. First two guided paths from this link https://www.outsystems.com/training/paths/ can make you almost an Outsystems Ninja.
Thank you for the advice Ravi. I haven't finished the quick start videos because I attended a bootcamp instead. The associate reactive developer bootcamp did not go into this kind of detail about how to parse JSON response, that's why I'm asking the community.
That's alright Daniel. It was just a suggestion. You can do it later when you get more time. It will be easier for you if you take the video courses because it covers more detail and overall understanding about how your apps can be modeled. I'm sure it will be worth your time spent.
About your question in concern, don't call the REST API actions on client actions. Wrap them in a server action, then call those server action from your client actions. And do the processing of transforming your API response into different representation inside the server action. It will solve your purpose.
Whether you've done courses or not, the community will always be there to support and help. Cheers!
Thanks for the encouragement Ravi!
I created the server action and made an output parameter called CalendarIds and made it of type text list. Here's what the assignment of that variable in the server action looks like:
However, when I run the server action in the app, I'm still only getting the first ID value of the first object returned in the API response, instead of an array or multiple IDs.
In the server action, how can I get the output parameter to only show the IDs for the response objects whose "name" key value is either "My Calendar" or "Holidays" ?
Perhaps a little more context may help. I'm using a service called Nylas that allows us to create scheduling pages (think Calendly). In order to create those scheduling pages we have to tell it which calendars in our users accounts to sync with in order to show only available options to book. Each user is going to have a different calendar ID for their "My Calendar" and their "Holidays" calendar, so I have to hit Nylas's /calendars endpoint first using the current user's access token in order to get those calendar IDs. I then want to filter out the responses to only include the "My Calendar" and "Holidays" IDs because other calendars that we don't need are returned and in the following call it needs those two calendar ID values in an array.
So, I call /calendars and get back a JSON array with 4 objects. I need the "id" key value only for the objects whose "name" key value equals "My Calendar" or "Holidays". Those "id" values need to go into an array that gets put into the following outgoing call that creates a scheduling page.
Hi Daniel,
Can you confirm you're receiving all the items in the response, but only getting the first item when you're converting it to the IDs List?
From the implemention in the previous message, it seems all good when you're converting it to the IDs List.
Replying to your other question, regarding filtering the keys having "My Calendar" or "Holidays", you can use ListFilter server action, after receiving the response from API, and before converting them into IDs List. You can add ListFilter by going to dependencies, select (System) module, select ListFilter. When you place it on your code flow in server action, you will easily figure out how it works.
Apologies for the delay, I had a long holiday here in the US.
I was only receiving the first item in the response, but I think it was because I was choosing the "current" item.
I've modified my server action a little after your ListFilter suggestion and I've definitely gotten closer. However, I don't think I'm entering a proper condition for ListFilter because it's returning all the values for a particular key, and not performing the filter.
As I mentioned previously, the response from my API is:
[{"id": 1234, "type": "calendar", "name": "My Calendar"},
{"id": 7894, "type": "calendar", "name": "Birthdays"}]
And the condition I have in the ListFilter step is: Name = "My Calendar" or Name = "Holidays"
After that step I assign my CalendarIds text list output variable as the results of that ListFilter step:
To test this I'm passing that variable as the body of an API call to an endpoint on Pipedream, and it's showing that body has 4 items in an array, ["1234","5678","9012","7894"]. So, it is indeed formatting the variable as a list(array), which is good and what I need, but it didn't seem to filter the list properly. That to me means I've got my ListFilter condition wrong, but it's a pretty simple expression. Am I missing a particular function there?
The filter conditions looks perfect if it has the following set up:
Name = "My Calendar" or Name = "Holidays"
You've correctly passed the ListFilter.FilteredList to the CalendarIds.
I doubt you've the mentioned JSON response correctly coming into your Nylas_GetCalendarsCopy.Response
Can you share a screenshot while debugging to show the values when you get response and ready to send it to ListFilter.
It would be better if you perform developer testing via debugging on your app. It gives you values for each and every step as you move forward with your code execution.