cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Serviced2000
Helper III
Helper III

Set Lookup Field Value from PCF

Hi Everyone,

 

I hope I have posted this in the correct place and hopefully this will be a simple thing for most. I am *very* new to PCF development (there is nobody else in our company to do it) and I'm looking to use a component to solve our addressing issues (seems to be a common theme too). I have used another component from the PCF Gallery to get the addressing details, but now I need to set the value of a couple of lookups (State and Country) to the values returned from the API. 

 

I've seen a lot of posts about creating custom lookup components, but I can't seem to find anything that relates to this slightly simpler task (or it seems simpler). Can anyone point me at any resources that might help me walk through this?

 

Thanks!

24 REPLIES 24
DianaBirkelbach
Super User
Super User

Hi @Serviced2000 , 

 

You can have a look to the Lookup PCF example: https://docs.microsoft.com/en-us/powerapps/developer/component-framework/sample-controls/lookup-cont.... There is a link to the source code.

 

If you have a more specific question, please let us know.

 

Hope this helps!

 

Kind regards,

Diana

Kind regards,
Diana
----------
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."
cchannon
Solution Sage
Solution Sage

OK, to set a Lookup Value from a PCF (assuming your PCF isn't Bound to that lookup) you will need to use the WebApi in your PCF. First, uncomment the use of WebApi from the manifest file and rebuild your control. This will give you a context.webapi object in typescript you can use to perform various actions.

 

Next, you will need to put an update in the correct place in your code. Here is a reference page on the PCF webapi update syntax, but it doesn't have any examples, so check out the the client script api reference page for examples (the syntax is basically identical).

 

What you'll see is that when updating a lookup value you need to use this odata syntax to bind the guid input you're providing to a specific table.

var data =
    {
        "primarycontactid@odata.bind": "/contacts(61a0e5b9-88df-e311-b8e5-6c3be5a8b200)"
    }

So your data object will contain a row that references:

"{fieldname of your lookup}@odata.bind" :  "/{plural form of the lookup table name}({record guid})"

DianaBirkelbach
Super User
Super User

Hi @cchannon , Hi @Serviced2000 

 

When I think to an address-autocomplete, usually I have a service which retrieves the data. Usually strings. 

An adress-autocomplete can be bound to several properties like street, city. There we can add the lookup bound properties country and state too.
After the service returns the suggestions, and one of them is chosen, I would add the webAPI request in order to get the corresponding lookups, and set it together with the other propeties through getOutputs.

This way, the data is consistent: the user saves all address-fields or nothing.

 

Kind regards,

Diana

 

 

Kind regards,
Diana
----------
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up."
a33ik
MVP

Also, you can use my toolkit that already contains autocomplete that supports lookup fields for country/state - https://github.com/AndrewButenko/ABControlsToolkit

tvd_nzi
Advocate II
Advocate II

I just need the value of a lookup.

 

How can I just get the lookup entity reference as value?

 

 

        <property name="LookupProperty" display-name-key="Lookup" description-key="Lookup" of-type="Lookup.Simple" usage="bound" required="true" />

 

 Setting the property usage to "input" or "bound" lets me choose it in the form editor for the pcf control but then the pcf is not working anymore and removing the selected lookup from the component options isn't possible. The pcf is not showing as component anymore.

Features like utility and webapi are already enabled.

Can you please provide your full manifest file?

<?xml version="1.0" encoding="utf-8" ?>
<manifest>
    <control namespace="tvd" constructor="Griddy" version="0.0.5" display-name-key="Griddy" description-key="Griddy description" control-type="standard">
        <data-set name="griddy" display-name-key="griddy subgrid" cds-data-set-options="displayCommandBar:true;displayViewSelector:true;displayQuickFindSearch:true">
        </data-set>
        <property name="somePhase" display-name-key="Some phase" description-key="Some phase" of-type="OptionSet" usage="input" required="true" />
        <property name="Lookup" display-name-key="Lookup" description-key="Lookup" of-type="Lookup.Simple" usage="bound" required="true" />
        <resources>
            <css path="css/Griddy.css" order="1" />
            <code path="index.ts" order="2"/>
            <resx path="strings/Griddy.1033.resx" version="1.0.0" />
        </resources>
    <feature-usage>
      <uses-feature name="Utility" required="true" />
      <uses-feature name="WebAPI" required="true" />
    </feature-usage>
    </control>
</manifest>
a33ik
MVP

It seems that your PCF control is of "Dataset" type. How do you plan to use that lookup and what do you plan to bind it to?

I plan to bind a custom entity lookup to it so I can read out the value in pcf for filtering the dataset. I want to avoid having a dummy field just to fill the guid of the lookup and listen to this dummy control.

Helpful resources

Announcements
PA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

Power Query PA Forum 768x460.png

Check it out!

Did you know that you can visit the Power Query Forum in Power BI and now Power Apps

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

R2 (Green) 768 x 460px.png

Microsoft Dynamics 365 & Power Platform User Professionals

DynamicsCon is a FREE, 4 half-day virtual learning experience for 11,000+ Microsoft Business Application users and professionals.

Users online (2,375)