need help on importing a .csv into two related tables (scripting help?)
I have a ninox.com database with two working tables: Contacts (to track association members) and Events (to track events to which those members are invited). The goal is to import members' attendance after each event, via .csv import. Tables Contacts and Events are correctly linked via a third table called RSVP. I have imported an updated .csv file to a temporary table called StagingImport (a Nioxus video suggested that approach, with a script to update the actual records from here). The first column in the table attached here shows the fields in StagingImports and their field names. The second column shows the fields that need to be updated in the Contacts table and their field names (some are slighly different). The third column shows the fields that need to be updated in Events, and their field names. When I started looking at Ninox's scripting docs I realized I was in over my head. I tried asking ChatGPT (yeah, I know) for a script for a "Process the CSV Import" Button, and needless to say it errors out. The attached doc contains the label info for each table and (on page 2) the script suggested by ChatGPT. I'd appreciate any help you can offer either in correcting the script, or if there's a better (simpler) approach.
62 replies
-
Hi,
You might find the following post interesting.
https://forum.ninox.com/t/x2yk889/is-it-possible-to-use-function-parsecsv-directly-in-server-mode
-
Hi Dave -
You have come to the right place.
First, you can try this version of ChatGPT. It has been taught by our good friend to be better (not perfect) about writing Ninox code. You just have to tell it to respond in English or any language you prefer.
Second, I'm guessing you want the script to create a new Contacts record if the person does not already exist. Or they should already be in the table?
Third, If they do exist then you want to compare contact info and only update info that has changed. That can lead to a bit of mess if you someone mistypes their email address or phone number, you wouldn't want that data to be saved.
Fourth, you will always be creating a new Event by this script.
I would recommend that you create another table called Attendance and make it a child of Event. Then you would add a link to Contacts. That way you have a table that tracks Event info (date, location, etc). Then your Attendance table will track all those who have attended. This table is called a many to many (N:N) table. You can link many Events to many Contacts. Which is what I think you want to do.
Fifth, you would create a new Attendance record for each person that attended linking it to the Event and related Contact record.
Give me some time and I can mock up what this could look like.
-
Hi,
You can try the following code inside a button. If it works, we can optimize it further.
do as server let x := select Contacts; let y := select Events; let z := select RSVP; for i in select StagingImport do let c := if contains(x.Email, i.Email) then first(x[Email = i.Email]) else create Contacts.( Email := i.Email; Lastname := i.'Last name'; Firstname := i.'First name'; 'Mobile Phone' := i.'Mobile number'; Zip := i.Zip) end; let e := if contains(y.'Event ID', i.'Event ID') then first(y['Event ID' = i.'Event ID']) else create Events.( 'Event ID' := i.'Event ID'; 'Event Name' := i.'Event name'; 'Event Type' := i.'Event type'; 'Date / Time' := i.'Event Start Date') end; if count(z[Attended = i.Attended and Contacts = number(c.Id) and Events = number(e.Id)]) = 0 then create RSVP.( Contacts := number(c.Id); Events := number(e.Id); Attended := i.Attended); end; end; end
Content aside
- Status Answered
- 17 hrs agoLast active
- 62Replies
- 82Views
-
4
Following