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 / Can someone explain th...
Power Apps
Unanswered

Can someone explain this peculiar behavior of creating an entity with lookup value?

(0) ShareShare
ReportReport
Posted on by 241

I got this error message when trying to create a new record that contained a lookup field. When investigating it further, I found the following.

 

My code was set up like:

 

// Create item
UpdateContext({ reg: { vdt_name: "test", vdt_date: Now() } }); // [..] some other code
// add reference to item UpdateContext({ reg: Patch( reg, { vdt_refitem: LookUp( RefItems, RefItem=selected.RefItemId ) } ) }); // save the item Patch( Registrations, Defaults( Registrations ), reg );

This gave me an error "Value must be an entity record" on the final patch to the data source.

However, when I changed it to the following:

// Create item
UpdateContext({ 
 reg: { 
 vdt_name: "test",
 vdt_date: Now(),
 // include the reference now
 vdt_refitem: LookUp( RefItems, RefItem=selected.RefItemId ) 
 }
});

// [..] some other code

// save the item
Patch( Registrations, Defaults( Registrations ), reg );

This code runs and works correctly.

 

I inspected the contents of the variable in both scenarios, and they both seem to contain all fields that I specified, including the lookup value. The only difference is that in the first instance, the lookup field reference is added later by doing a patch. What makes this not work?

 

I have the same question (0)
  • v-yutliu-msft Profile Picture
    on at

    Hi @MrNappa ,

    That's a little strage...

    Did you make other changes about variable reg?

    Could you tell me Registrations table and RefItems table's structure?

    I've made a test and found that the two ways to create variable will result the same data.

    Here's my test:

    UpdateContext({ 
     reg: { 
     vdt_name: "test",
     vdt_date: Now()
     }
    });
    UpdateContext({
     reg: Patch( reg, { vdt_refitem: {id:"aa"} } )
    });
    UpdateContext({ 
     reg1: { 
     vdt_name: "test",
     vdt_date: Now(),
     vdt_refitem: {id:"aa"}
     }
    });

    And the result:10101.PNG

     

    10102.PNG

     

     

     

     

    Best regards,

  • MrNappa Profile Picture
    241 on at

    Hi Phoebe,

    The field is a simple Lookup field to another table. 


    Also -- after some further testing, I it might have something to do with async behavior of the patch command?

    If you look at the following test:

    lookup.jpg

    You can see that only the 2nd patch is 'problematic'. If I include the lookup field in the original UpdateContext, or if I Patch it with a cached value, the final patch seem to work correctly.
    Update: Actually, when I comment out the 2nd patch line, the third patch also gives an error. So that was a false positive.

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 717 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 329 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard