How to search an API JSON response, and do another operation if something isn't present

Hi,

I have a server-side action and I need it to do the following:

  1. Call a third party API, retrieve a list of all projects, and identify if my project already exists.
  2. If my project exists, use its internal ID from the API response in subsequent actions.
  3. If the project doesn’t exist, create it with another API call, and use the returned internal ID in the subsequent actions instead.

If it’s easier to understand in pseudocode:
const myProject = “My Project”;
var projects = listProjectsApiCall();
var internalId;
projects.forEach((project) => {
if (project.name == myProject) {
internalId = project.id;
}
});
if (!internalId) {
internalId = createProjectApiCall(myProject);
}
// do more stuff with internalId

I can see how to achieve this with plugins but I feel there ought to be a “Bubble” way to do it that I can’t figure out… any ideas?

Thanks!

You can try using custom events:

  • put your final logic in a custom event, let’s call it “final”, add a text as input, it will be the id you will use for all the oter operations
  • create anoter custom event, let’s call it “create project”, here you call createProjectApiCall, then trigger “final” with the id you get from the previous step
  • on the main workflow call listProjectsApiCall, trigger “final” if the id is present, trigger “create project” if the id is not present
1 Like

Thanks - that does make sense, I like that more than writing a custom plugin, though I do think Bubble could find better ways to achieve things like this which surely aren’t that unusual!

1 Like