Structuring Template for API call

Hi all, I need some help trying to figure out how to structure my dynamic search results into a template.

Basically the function is I have someone who searches for one or multiple cities in my app. Those results get put into a user specific text column. From there I need those dynamic results to basically fit into this format: City eq ‘CITY1’ City eq ‘CITY2’ City eq ‘CITY3’.

Currently my data is comma separated: Los Angeles,Anaheim,Claremont,Irvine

Reason being is this is how I need to structure the data for my api call. I’m having a hard time trying to wrap my head around how to structure this or what to use. I tried using GPT to create a javascript code that would take that text and convert it into the format I needed, disclaimer I don’t know how to code lol (Couldn’t get it to work) → Here is the code:

window.function = function (p1) {
  if (p1 && p1.value) {
    let cities = p1.value.split(',');

    if (cities.length === 1) {
      return `City eq '${cities[0]}'`;
    } else {
      return cities.map(city => `City eq '${city.trim()}'`).join(' or ');
    }
  } else {
    return undefined;
  }
}

Lastly, I did get it to work using the generate text feature but that is eating up more usage then I would like. I would prefer to get this done either using a computer column or javascript if possible. Any help is appreciated!


Screenshot 2024-01-27 100916

This is doable with 2 or 3 template columns if you want something easier to understand.

However try this javascript. I only modified yours slightly by pulling the code out of the function. You had a function, but you weren’t calling the function in your code. (disclaimer: didn’t test the code below)

 if (p1 && p1.value) {
    let cities = p1.value.split(',');

    if (cities.length === 1) {
      return `City eq '${cities[0]}'`;
    } else {
      return cities.map(city => `City eq '${city.trim()}'`).join(' or ');
    }
  } else {
    return undefined;
  }
1 Like

Hey Jeff thank you for your input. I gave it a try but got an unexpected string error.

OK, actually tested it this time and cleaned it up a little.

Try this:

if (p1) {
    let cities = p1.split(',');

    if (cities.length === 1) {
      return `City eq '${cities[0]}'`;
    } else {
      return cities.map(city => `City eq '${city.trim()}'`).join(' or ');
    }
}
4 Likes

THANK YOU! Worked like a charm! I appreciate your time and help!!
Screenshot 2024-01-27 175942

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.