I have a function for generating a customer code that checks all existing codes and increments the highest by 1 to create a new one.
This is the formula:
Patch(newCust,ThisItem,{company: newCompany.Text});
Clear(codeCount);
ForAll(
Search(
'[LV1_DB].[Customer]',
Left(First(newCust).company,4),
"AccountCode"
),
Collect(
codeCount,
{
Value: Value(
Right(
AccountCode,
2
)
)
}
)
);
ForAll(
Search(
'[dbo].[tblCust]',
Left(First(newCust).company,4),
"Customer_code"
),
Collect(
codeCount,
{
Value: Value(
Right(
Customer_code,
2
)
)
}
)
);Patch(
newCust,
ThisItem,
{
code: Concatenate(
Upper(
Left(ThisItem.company,4)
),
Text(
Max(
codeCount,
Value
)+1,
"[$-en-US]00"
)
)
}
)If I have both sections of the formula run on the OnChanged event of the newCompany field it creates a new record in my newCust collection.
If I split the formula as above, put the first part on the OnChanged event of the newCompany field and the second part on a button it patches the current record as it should.
This formula is running within the context of a Gallery on a collection that would normally only have one record.
I am not using the Default() function so why would it be creating new records?