Thank you for taking the time to read my question... a bit of a long one. Sorry.
I have the following code on a button. The idea of this simple app is to get a new unique code (a code generating app). The app is in TEAMS and the table is in Dataverse for TEAMS.
It works great if one person is using the app, but if two people click the button at approximately the same time, one person updates the "empty" record, the other doesn't, they both get notified that they secured the new code and two new exact same "empty" records are created in the table.
Empty: No UserID, CreatedDate, ProdDescription and Facility = Facility
My Table:

I was testing with another person and we clicked the button at about the same time. The green box shows the record that the app told us we both secured. We then created a new "empty" record. The new record is supposed to be unique based on FirstLetter, SecondLetter and CodeNumber.
I tried to add a LookUp to see if, after updating the record, the users().Email was the one written to the record based on tID, but no matter what I do, all code after it is red underlined... I also tried using the IsEmpty(Errors) but also get red underlines... I really don't know why.
The IsEmpty(Errors) at the bottom of the code also does not seem to catch any errors when the app tries to write to the table even though it's not successful.
We've been able to replicate this several times.
Any suggestions or ideas would be great!
My Code OnSelect:
//Get the open record
ClearCollect(
LastCode,
Filter(
CodeLists,
Facility = "Facility"
)
);
ClearCollect(
UseCode,
First(LastCode)
);
Set(
varFacility,
tiBrillPlant.Text
);
Set(
varDesc,
tiItemDesc.Text
);
Set(NewCode,"");
//Get the Record ID of the open record
Set(
FirstUseCode,
First(UseCode).tID
);
//Update the last known open record based on tID
//If the UserID is blank where tID = FirstUseCode then update it with the info provided by the user
If(
IsBlank(
LookUp(
CodeLists,
tID = FirstUseCode
).UserID
),
UpdateIf(
CodeLists,
tID = FirstUseCode,
{
UserID: User().Email,
CreatedDate: Now(),
ProdDescription: tiItemDesc.Text,
Facility: tiBrillPlant.Text
}
);
ClearCollect(
MakeCode,
Last(
Sort(
CodeLists,
tID,
Ascending
)
)
);
If(
First(MakeCode).Facility <> "Facility",
Set(
NewCodeNumber,
If(
Last(MakeCode).CodeNumber = 999999,
1,
Last(MakeCode).CodeNumber + 1
)
);
Set(
NewSecondLetter,
If(
And(
NewCodeNumber = 1,
Last(MakeCode).SecondLetter = 90
),
65,
If(
NewCodeNumber = 1,
Last(MakeCode).SecondLetter + 1,
Last(MakeCode).SecondLetter
)
)
);
Set(
NewFirstLetter,
If(
And(
NewCodeNumber = 1,
NewSecondLetter = 65
),
Last(MakeCode).FirstLetter + 1,
Last(MakeCode).FirstLetter
)
);
If(
NewFirstLetter > 90,
Notify("There are no new codes available. The last code has been used."),
If(
IsEmpty(
Errors(
Collect(
CodeLists,
{
Facility: "Facility",
FirstLetter: NewFirstLetter,
SecondLetter: NewSecondLetter,
CodeNumber: NewCodeNumber
}
)
)
),
Set(
NewCode,
Concatenate(
Char(Last(UseCode).FirstLetter),
Char(Last(UseCode).SecondLetter),
Concatenate(
Left(
"000000",
6 - Len(Text(Last(UseCode).CodeNumber))
),
Text(Last(UseCode).CodeNumber)
)
)
);
Notify(
Concatenate(
Text(NewCode),
" is now in use."
),
NotificationType.Success
);
If(
chkSendEmail.Value,
Office365Outlook.SendEmailV2(
User().Email,
tiBrillPlant.Text & " | " & tiItemDesc.Text & " | " & Text(NewCode),
htEmail.HtmlText,
{
From: User().Email,
Importance: "Normal",
IsHtml: true
}
)
),
Notify(
Concatenate(
"Unable to mark ",
Text(NewCode),
" as in use."
),
NotificationType.Error
)
)
)
);
//If(
//IsEmpty(Errors(CodeLists)),
//UpdateContext({resettext: !resettext})
//);
//If(
//IsEmpty(Errors(CodeLists)),
//UpdateContext({resettext: !resettext})
//);
,
Notify(
Text(NewCode) & " has been saved by " & LookUp(
CodeLists,
tID = FirstUseCode
).UserID,
NotificationType.Error
)
)