Help

Re: createRecordAsync creating multiple instances of the same record

1500 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Justin_Yorick
4 - Data Explorer
4 - Data Explorer

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>

4 Replies 4

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({})
}}

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!