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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Patch with Defaults to...
Power Apps
Answered

Patch with Defaults to create record - code not working (?)

(0) ShareShare
ReportReport
Posted on by 171

I'm wondering if someone can point me to the error in my code

 

I am trying to add a record to an intermediary entity 'Member Agency Contact Lists'. Being an intermediary entity (creating a N:N relationship), it references the primary fields of two other entities: 'Member Agencies' and 'Member Contacts.' These primary fields are both labelled 'Autonumber' in those two entities. Within the intermediary entity, those (referenced) Autonumbers are labelled 'Agency #' and 'Contact #" respectively, and it has it's own primary field, also labelled 'Autonumber.'

 

I had hoped to use the selected fields within MemberAgencyGallery (which displays the entity 'Member Agencies) and ExistingContactsGallery (which displays the entity 'Member Contacts'), so that OnSelect of an icon (within ExistingContactsGallery), those selected fields would be entered in a new record in the intermediary entity 'Member Agency Contact Lists'

 

I am trying the following code, and when I press the icon, PowerApps seems to be doing something, but the record doesn't *seem* to be added to the intermediary entity.

 

Select(Parent);
Patch(
 'Member Agency Contact Lists',
 Defaults('Member Agency Contact Lists'),
 {
 'Agency #': LookUp(
 'Member Agencies',
 Autonumber = MemberAgencyGallery.Selected.Autonumber
 ),
 'Contact #': LookUp(
 'Member Contacts',
 Autonumber = ExistingContactsGallery.Selected.Autonumber
 )
 }
)

 

 

Categories:
  • PaulD1 Profile Picture
    2,914 on at

    Hi

    Some thoughts...

    What datasource are you using? If SQL, make sure you have a primay key (can be the combination of the two fields) - you can't patch into a SQL table without a primary key.

    Are those fields the only fields required? 

    Are you attempting to patch a combination that already exists (violates a key or constraint)?

    Have you tried hard coding values as a test?

    Note that if you are referencing the Selected property of a gallery on a different screen, PowerApps has an intermittent bug where it does not always return the actual item selected (sometimes it just returns the first item in the gallery) - instead of referencing the gallery directly, set a gloabal variable (or pass a context variable to the screen that performs the patch) and set the value of the variable when a gallery item is selected.

  • Madumi Profile Picture
    171 on at

    I'm using Common Data Services & yes, those Autonumbers are the only fields required to make the N:N relationship.

    I tried hard coding the values as a test, but couldn't get the formula right (data type mismatch), so gave up.

    and yes, the formula references items on the same screen.

  • PaulD1 Profile Picture
    2,914 on at

    If you are using CDS, you may need to use the Relate function for an N:N relationship.

  • v-siky-msft Profile Picture
    Microsoft Employee on at

    Hi @Madumi ,

     

    Do you miss any required fields?

    'Agency #' and 'Contact #' are both lookup fields, but i cann't find any primary field.

    Sik

  • Madumi Profile Picture
    171 on at

    Thx @v-siky-msft 

     

    The primary field of the entity is an autonumber on the CDS level (the datatype is set to autonumber in the field properties of the entity)... if that helps...

     

    The code works if I shift it to a button external to the gallery (pre-selecting the record I want on the gallery) with code as follows:

     

    Patch(
     'Member Agency Contact Lists',
     Defaults('Member Agency Contact Lists'),
     {
     'Agency #': LookUp(
     'Member Agencies',
     Autonumber = MemberAgencyGallery.Selected.Autonumber
     ),
     'Contact #': LookUp(
     'Member Contacts',
     Autonumber = ExistingContactsGallery.Selected.Autonumber
     )
     }
    )

     

    But when I use the same code on an icon (OnSelect) within the gallery, when I press the icon, it does nothing (no data has been entered when I check the 'Member Agency Contact Lists' entity records)

     

    I tried cutting the (working) button from outside the gallery & pasting it within the gallery, but again, it refuses to work once it's in the gallery.

     

    Hmmm, now that I think about it, the code is doing a lookup on ExistingContactsGallery within the ExistingContactsGallery gallery. Is that possibly causing a problem? If so, any suggestions how I would work around it?

     

    Or is there some other issue causing the failure?

  • Verified answer
    v-siky-msft Profile Picture
    Microsoft Employee on at

    @Madumi  Do you mean the code is applied to the button inside of the 'Member Contacts' gallery? 

    Please try this:

     

    Patch(
     [@'Member Agency Contact Lists'],
     Defaults([@'Member Agency Contact Lists']),
     {
     'Agency #': LookUp(
     'Member Agencies',
     Autonumber = MemberAgencyGallery.Selected.Autonumber
     ),
     'Contact #': ThisItem
     )
     }
    )

     

    Sik

  • Madumi Profile Picture
    171 on at

    Brilliant! That works... (without the close bracket after ThisItem).

    I had tried using ThisItem (instead of Lookup) & hadn't gotten it to work (without the initial square brackets).

    Where can I learn the difference in the formula structure?

    Thanks again!

  • v-siky-msft Profile Picture
    Microsoft Employee on at

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 402 Super User 2026 Season 2

#2
Mohsin Ali Profile Picture

Mohsin Ali 328

#3
WarrenBelz Profile Picture

WarrenBelz 296 Most Valuable Professional

Last 30 days Overall leaderboard