createRecordAsync creating multiple instances of the same record

I am attempting to write multiple linked field values to a sigle new record. When I invoke the createRecordAsync method on the table, more than ten records (all with the same cell values) get written to the table. When I define the function as Async and use an await, no records end up getting created.

Code snippets below.

  let operatorNamesField=tableOperators.getFieldByNameIfExists('Operator Names');
  let operatorNamesRecord=useRecords(tableOperators, operatorNamesField);
  let operatorNameOptions=operatorNamesRecord.map(operatorNamesRecord=>({
    label: operatorNamesRecord.getCellValueAsString(operatorNamesField),
    value: operatorNamesRecord.id 
  }));

//note: all of the select field options are formatted as above, but not shown for brevity
                      <Select
                      value={machineId}
                      options={machineNameOptions}
                      onChange={setMachineId}
                      />
                      <Select
                      value={systemId}
                      options={systemNameOptions}
                      onChange={setSystemId}
                      />


                      <Select
                      value={procedureId}
                      options={maintenanceProcedureOptions}
                      onChange={setProcedureId}
                      />
<Select
                   value={operatorId}
                   options={operatorNameOptions}
                   onChange={setOperatorId}
/>
// The code then attempts to create a record using these values as part of an event triggered by a context dialog component

<React.Fragment>
                       <Button disabled={!checkBoxState || !operatorId ||!dateValue}
                        onClick={() => setIsDialogOpen(true)}>
                        Create a Maintenance Record
                      </Button>
                      {isDialogOpen && (
                        <ConfirmationDialog
                          isConfirmActionDangerous={true}
                          title="Are you sure?"
                          body="NOTE: A Record Cannot be Created Until All Fields Are Completed."
                          onConfirm={tableMaintenanceTickets.createRecordAsync({'Machine Number / 
                                                                                 Station':[{id: machineId}],
                                                                                'Task ID': [{id:procedureId}],
                                                                                'Task Completed by': [{id: operatorId}]

                        }),
                          
                            ()=>setIsDialogOpen(false)
                            
                          }
                          onCancel={() => setIsDialogOpen(false)}
                        />
                      )}
    </React.Fragment>

Try putting your confirmation into an actual function. createRecordAsync has to be done asynchronously anyways, as the name suggests. It doesn’t appear you’re doing that.

onConfirm={async () => {
   tableMaintenanceTickets.createRecordAsync({})
}}
3 Likes

Thanks for the reply @Kamille_Parks !
I was able to get this to work without declaring async in front of the inline function… Not sure why it works for me like this. I wonder if the behavior is different in scripts and blocks/app (I’m using the latter) .

The issue in this case seems to be the way I had grouped my functions inside the onConfirm prop.

Given how you’re:

  • writing your own app
  • using a JavaScript stack
  • with an experimental/beta interface on top
  • while exhibiting no clear signs of frustration
  • including frustration with concern for things like the event queue
    .

You must have been doing something right so far, and assuming all those statements are still true right now, stop reading this bogus comment and start spilling the secrets to your stoicism. :joy:

With that said, this particular case, disregarding the obvious syntactic differences, the limitation should be exactly same - global scope and top-level modules are where you need your async functions to be.

I wonder if the behavior is different in scripts and blocks/app (I’m using the latter)

I would hope not. Anything else should throw, if no custom behaviors were defined and you’re in strict-mode, making callbacks a requirement

All of this is purely anecdotal obviously. Regardless, if the going gets tough right about now, consider taking a short break from your code and start catching up on the theory behind it That seems to be what you’re missing more than any x is y answer. A few things that come to mind and are always worth recapping, regardless:

function declarations vs function expressions = what’s the difference between those, and does it have anything to do with scope?, and how does it connect to the overall scoping logic from the language spec ? Can you do code reviews? Do code reviews. If you have any patience to spare, help complete newcomers and you’ll likely help yourself identify any glaring omissions from your own memory banks.

@Dominik_Bosnjak
Could be stoicism, could just be ignorance of what I’m getting myself into :man_shrugging:

You’re right about reviewing my fundamentals/ mid level techniques. This has been an evolving project and so far I’ve just kind of scrambled to get a minimum viable product up and running. After a certain point though I need to step back and put a bit more thought into how I want this all to work.

Thankfully everyone in the community here has been pretty helpful so far!

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