microsoft / Power-Fx

Power Fx low-code programming language
MIT License
3.21k stars 327 forks source link

ThisRecord seems to switch context. #2037

Open readyxrm opened 1 year ago

readyxrm commented 1 year ago

I am trying the new Dataverse low-code plug-in and running into maybe an issue or maybe a misunderstanding of PowerFX.

This is an instant plug-in that runs on the creation of a record in a new custom table table.

The table will have an email address field (text).

The use case is an application form, and determining if the applicant already exists as a contact in Dataverse.

I want to query the Dataverse contact table to see how many times that email exists.

For an instant plug-in, I have the code CountIf([@Contacts], Email = ThisRecord.Email), however the ThisRecord object seems to now want to reference the Contact table (the dot notation shows contact table fields), and not the table that is used to trigger the plug-in.

If I hard-code the ThisRecord (e.g. ThisRecord.abc_emailaddress) I get an error.

Just curious if this is an issue, or just how the ThisRecord object behaves?

Here is the full code - the CountIf always returns the number of contacts (email = email).

Switch (
    CountIf([@Contacts], Email = ThisRecord.Email),
    1, Patch('Volunteer Applications',ThisRecord,{'Existing Contact ': 'Existing Contact'.'Single match found'}),
    0, Patch('Volunteer Applications',ThisRecord,{'Existing Contact ': 'Existing Contact'.'No match found'}),
    Patch('Volunteer Applications',ThisRecord,{'Existing Contact ': 'Existing Contact'.'Multiple matches found'})  
)
jas-valgotar commented 1 year ago

Hi Nick, That is an expected behavior, You could do something like the below to make it work, CountIf([@Contacts] As c, c.Email = ThisRecord.Email)

I see this is not very obvious and intuitive, and we are actively discussing internally how we can best mitigate this.

readyxrm commented 1 year ago

Awesome, your suggestion worked. Thank you for the response and I am looking forward to learning more and trying out more use cases.