web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Saving new record with...
Power Apps
Unanswered

Saving new record with Patch function

(0) ShareShare
ReportReport
Posted on by 171

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?

 

 

 

 

Categories:
I have the same question (0)
  • SimonMeadows Profile Picture
    171 on at

    Ok, so im half answering my own question but it would be good to know why it makes a difference.

     

    If I change all references from 'ThisItem' to 'First(newCust)' the formula operates as expected.

     

    Why, in the gallery, if im in the context of the first record in the newCust collection would there be any difference between the reference to 'ThisItem' and and the reference to 'First(newCust)'?

    Shouldn't they be the same record reference?

  • Verified answer
    Meneghino Profile Picture
    6,949 on at

    Hi @SimonMeadows

    I have to admit I did not work through all the details of your post, however here is what I learned about the Defaults(MySource) function and its combined use with Patch().

     

    1) The Defaults function will put out a new call to the data source each time it is used, so if you are patching multiple records this impacts app performance negatively (for example within a ForAll loop).  In order to avoid this, I normally create a collection like this:

    ClearCollect(MySourceDefault, Defaults(MySource))

    I normally do this as part of the OnStart of the first screen.  Then I can use Patch(MySource, First(MySourceDefault), etc. etc.) instead of the usual Patch(MySource, Defaults(MySource), etc. etc.)

     

    2) Actually, what follows from 1 above is that you do not even need to call the Defaults function at all unless you really have some explicit default values.  The following for example works with Azure SQL DB to create a new record where the table's primary key is ID

    Patch(MySource, {ID: Blank()}, etc. etc.)

    I am not sure this helps, but if for some reason ThisItem returns a record which has Blank() as the value of the primary key field(s), then a new record will be created.

     

    Hope this helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 796 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 327 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard