cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
MrNappa
Level: Powered On

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

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?

 

2 REPLIES 2
Highlighted
Community Support Team
Community Support Team

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

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,

Community Support Team _ Phoebe Liu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
MrNappa
Level: Powered On

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

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.

Helpful resources

Announcements
firstImage

Microsoft Business Applications Virtual Launch Event

Join us for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

firstImage

Watch Sessions On Demand!

Continue your learning in our online communities.

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

FirstImage

Power Platform World Tour

Coming to a city near you

thirdimage

PowerApps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

FourthImage

Join PowerApps User Group!!

Connect, share, and learn with your peers year-round

Users Online
Currently online: 168 members 3,493 guests
Please welcome our newest community members: