POST conditional on existing record

Options

I am fairly new to API's so forgive me if this is a naive question.

I would like to setup an API request that POST a new record in a table for a given user so long as there is not already a record in that table for that user. Otherwise I would like to update that existing record with the incoming info.

What would be the best way to achieve this?

Best Answers

  • Guillaume Maison
    Guillaume Maison Member ✭✭
    Answer ✓
    Options

    @Peter Nooteboom
    Yep, you do a Query All Record with the criterias mentioned. if you already have a record, the result variable is not null. Else, it is null.
    You can make your conditional on result variable = null :

    and for the Conditional: if condition :

  • Guillaume Maison
    Guillaume Maison Member ✭✭
    Answer ✓
    Options

    @Peter Nooteboom i forgot to mention that in the query all records’ output, I set the query to return a single record and not a list. That’s why I’m able to set the condition == null.

    and then no need to reference the first element of the array :)

Answers

  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options

    There is a useful function called add or edit record which will supply this functionality in a function stack. Do you know how to identify whether the record is already there? Some sort of key or ID that you use to tell records apart / identify duplicates? With that in hand, you would probably use a "create record" style POST request, and then modify it to use the "add or edit" rather than "add record" with a check on whatever field you use for that purpose.

    This is the cool part! Getting out of the "standard" REST metaphors is where we start to make much more interesting endpoints in Xano! We discuss these sorts of edge use cases in our daily office hours and loom-enabled forums on State Change Pro.

  • Prepify
    Prepify Member
    edited June 2023
    Options

    @Ray Deck This is somewhat helpful. But how am I supposed to know the unique ID if at the time of the query I don't know if the record exists or not?

    Just for some additional context, the table in question is logging user (user_id) responses to questions (question_id). Each user should only be allowed to have 1 record for each question id. So I need to check if a record for a given user_id and question_id pair exists. Update it if it does, create it if it doesn't.

    The "Add or Edit", "Has Record", and "Get Record" all only allow for querying based on a single ID rather than a pair (i.e. user_id and question_id).

    I thought maybe I could "query all records" based on those two criteria (user_id and question_id) and then use the "Conditional" function to either update the single queried record or create the record if it returned nothing. But I can't figure out how to specify in the conditional whether the query was empty or not. It just provides an error if there is no input.

  • Prepify
    Prepify Member
    edited June 2023
    Options

    @Guillaume Maison That is extremely helpful.

    It turns out the "Query All Records" either returns an Array with 1 record or an array with no records. So for the conditional to work I used [] instead of null.

    One last small question. In the case the conditional results in me using "Edit Record" I need to use the ID returned in the initial "Query All Records".

    How do I reference a particular row of an array? I tried QueriedRecord.id, but it gives an error. I think I need to choose the id in the first row, but can't figure out the syntax.