The Easiest Way to Edit/Create a Record

Options
What's the easiest way to create an API if I need to edit a record if it exists or add a new one if it doesn't.

I thought of querying all records for a match of the searched columns (other than the id) and if found I return the id in a variable.
If the id is not null I edit the record and else I add a new record.

I tried to do that but I can't get it to work.
Xano complains that it can't see the [variable name].id field.

Comments

  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
    Hi Oliver - there is a function that does this. You will want use the Add or Edit Record function (Function Stack > Database Requests > Add or Edit Record).

    This will do a record lookup based on the field you define, if it finds the record exists then it will edit it, if it doesn't exist then it will add a new record.
  • Oliver Burrell
    Options
    Hi Michael, thanks but I can't find this function.
    After I click 'Add API Endpoint' I get the following options:
    [Screenshot 2021-08-12 at 21.32.26.png]If I then click CRUD I can't see Add or Edit Record ...

    Where do you mean?
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
    In the Function Stack (when you add a new Function). Not a new API Endpoint.

    [Screenshot_2021-08-12_20-42-22.png]
  • Oliver Burrell
    Options
     I never looked that far down ;) cheers!
  • Oliver Burrell
    Options
    Hi Michael, What do you do if you have two columns to find the record by? I need to search using the fields users_id and procategories_id instead of the id field.[Screenshot 2021-08-13 at 11.07.58.png]
  • Matt
    Matt Member
    Options
     Did you get this sorted? I'm curious on this myself.
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
    Use a query all records function to search with the multiple constraints and change your return type to single. Then you can pull the id field from the result variable
  • Oliver Burrell
    Options
    , yes, it works if you query all records and then you can select by multiple fields and the result needs to go into a variable. Please see the example below ...
    [Screenshot 2021-11-16 at 09.41.34.png][Screenshot 2021-11-16 at 09.41.49.png]