cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Advocate V
Advocate V

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
Community Support
Community Support

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.

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
New Badges

New Solution Badges!

Check out our new profile badges recognizing authored solutions!

New Power Super Users

Congratulations!

We are excited to announce the Power Apps Super Users!

Power Apps Community Call

Power Apps Community Call: February

Did you miss the call? Check out the Power Apps Community Call here.

Microsoft Ignite

Microsoft Ignite

Join digitally, March 2–4, 2021 to explore new tech that's ready to implement. Experience the keynote in mixed reality through AltspaceVR!

Users online (27,215)