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

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

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.
Highlighted
Advocate IV
Advocate IV

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
secondImage

Demo-Extravaganza 2020

Check out these cool Power Apps & vote on your favorite!

secondImage

Robotic Process Automation

Let's talk about the solution provided by Microsoft for Robotic Process Automation (RPA)

secondImage

Community Highlights

Check out whats happening in Power Apps

secondImage

Community User Group Member Badges

FIll out a quick form to claim your community user group member badge today!

Users online (7,442)