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 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
-
Can you make a last effort to try the following script?
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 let w := create Events.( 'Event ID' := i.'Event ID'; 'Event Name' := i.'Event name'; 'Event Type' := i.'Event type'; 'Date / Time' := i.'Event Start Date'); y := array(y,w); w 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; delete select StagingImport; end
-
On a side note, if you are on a Mac you can use the MacOS app to download copies of your DB regardless of the level of your subscription.
-
I'm going to have to log off for awhile, will check back later for updates. Thanks for the support so far - fingers crossed we're close.
-
Hi,
I forgot to put the "w" variable in brackets []. Here's the updated script, let's hope it works!
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 let w := create Events.( 'Event ID' := i.'Event ID'; 'Event Name' := i.'Event name'; 'Event Type' := i.'Event type'; 'Date / Time' := i.'Event Start Date'); y := array(y,[w]); w 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; delete select StagingImport; end
-
I think we're getting there. The issue came up because the 'select' statement returns 'nid', but the 'create' statement returns 'rid'. Here's a script you can try out.
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 let w := (create Events); w.( 'Event ID' := i.'Event ID'; 'Event Name' := i.'Event name'; 'Event Type' := i.'Event type'; 'Date / Time' := i.'Event Start Date' ); y := array(y, [w.Id]); w.Id 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; delete (select StagingImport) end
-
We should use the same logic in the Contacts table. If a new contact attends multiple events in the .csv file, only one new record should be created in the Contacts Table. I updated the script and made the names a bit more descriptive.
do as server let allContacts := (select Contacts); let allEvents := (select Events); let allRSVP := (select RSVP); for i in select StagingImport do let contact := if contains(allContacts.Email, i.Email) then first(allContacts[Email = i.Email]) else let newContact := (create Contacts); newContact.( Email := i.Email; Lastname := i.'Last name'; Firstname := i.'First name'; 'Mobile Phone' := i.'Mobile number'; Zip := i.Zip ); allContacts := array(allContacts, [newContact.Id]); newContact.Id end; let event := if contains(allEvents.'Event ID', i.'Event ID') then first(allEvents['Event ID' = i.'Event ID']) else let newEvent := (create Events); newEvent.( 'Event ID' := i.'Event ID'; 'Event Name' := i.'Event name'; 'Event Type' := i.'Event type'; 'Date / Time' := i.'Event Start Date' ); allEvents := array(allEvents, [newEvent.Id]); newEvent.Id end; if count(allRSVP[Attended = i.Attended and Contacts = number(contact.Id) and Events = number(event.Id)]) = 0 then (create RSVP).( Contacts := number(contact.Id); Events := number(event.Id); Attended := i.Attended ) end end; delete (select StagingImport) end
I've attached my demo database where I did all the testing. I'm eager to hear the results from your database. Are you having any other problems?
-
We made it!
The issue in your database came up because the 'Event ID' field in the StagingImport table was set to number data type. So, I changed it to a text field type.
Everything seems to be working as expected now. Are you happy with the final result?
Today, I learned how to add a newly created record to an array of selected records using code. I have to thank for his helpful contribution.
I've attached the updated version of your database.
Best wishes
Content aside
- Status Answered
- 2 days agoLast active
- 62Replies
- 88Views
-
4
Following