๐Ÿ’ก 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:

  1. Define your data source (e.g., Contacts)
    ๐Ÿ”ธ Make sure you have a collection or table connection, such as Contacts.
  2. Retrieve any existing record:
    ๐Ÿ”ธ varRecord = LookUp(Contacts, Email = varEmail)
    (This returns the record if it exists, otherwise Blank.)
  3. Use Patch() with Coalesce():
    ๐Ÿ”ธ Patch( Contacts, Coalesce( varRecord, Defaults(Contacts) ), { Email: varEmail, FullName: txtName.Text, Phone: txtPhone.Text } )
    ๐Ÿ”ธ Coalesce(varRecord, Defaults(Contacts)) ensures that if varRecord is Blank, the Defaults(Contacts) record template is usedโ€”resulting in a new record.
  4. 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