๐ก Challenge
In Power Apps, maintaining separate logic branches for โCreateโ and โUpdateโ can bloat your formulas and introduce bugs. How can you streamline this into a single operation?
โ Solution
Use a single Patch()
call combined with Coalesce()
. If LookUp()
finds no record, Coalesce()
falls back to Defaults(DataSource)
, so the same call either updates an existing record or creates a new one.
๐ง How Itโs Done
Hereโs the step-by-step approach:
- Define your data source (e.g., Contacts)
๐ธ Make sure you have a collection or table connection, such asContacts
. - Retrieve any existing record:
๐ธvarRecord = LookUp(Contacts, Email = varEmail)
(This returns the record if it exists, otherwise Blank.) - Use
Patch()
withCoalesce()
:
๐ธ Patch( Contacts, Coalesce( varRecord, Defaults(Contacts) ), { Email: varEmail, FullName: txtName.Text, Phone: txtPhone.Text } )
๐ธCoalesce(varRecord, Defaults(Contacts))
ensures that ifvarRecord
is Blank, theDefaults(Contacts)
record template is usedโresulting in a new record. - Publish and test:
๐ธ When a matching record exists, itโs updated. If not, a new record is created with the field values you provided.
๐ Result
Youโve consolidated two code paths into one: your form logic is cleaner, easier to maintain, and less error-prone. Now โupsertโ (update or insert) happens seamlessly with a single Patch()
.
๐ Key Advantages
๐ธ One unified call instead of separate Create/Update branches
๐ธ No extra variables or If()
checks needed
๐ธ Cleaner maintenance and improved performance
๐ธ Consistent behavior across SharePoint, Dataverse, or any Power Apps data source
๐ ๏ธ FAQ
1. Do I still need If()
conditions to check for existing records?
Coalesce() handles the check for you. If LookUp()
returns Blank, it automatically uses Defaults()
, so If()
is unnecessary.
2. Will this approach work with Dataverse tables?
Yes. Both Patch()
and Coalesce()
behave identically in Dataverse. Just replace Contacts
with your table name (e.g., Accounts
).
3. What if LookUp()
returns more than one record?
Ensure your lookup criteria are unique (for example, use a primary key or unique field). Otherwise, LookUp()
returns the first match and may lead to unexpected updates.
Leave a comment