Bhubaneswar, Odisha, India
+91-8328865778
support@softchief.com

Lookup Field update in Edit Form, Display Form, Gallery using Canvas Apps

Lookup Field update in Edit Form, Display Form, Gallery using Canvas Apps

When we are working with Canvas Apps we Create, Updated, Retrieve and Delete records for Dataverse or Dynamics 365 tables using Gallery, Forms and other UI components of Canvas Apps. Working with simple Data Type fields is ok but when it is a matter of Look Up Field its not straight forward so we need to do some Power FX formula to achieve this. So follow me and see how we can do that.

HOW TO DISPLAY LOOKUP FIELD TEXT IN CANVAS APP GALLERY

This is the output of the gallery.

I have used a gallery control to display Contact records by connecting Dataverse. When I have connected the gallery the layout displays Contact Image, Full Name, Email and which is very straight forward. The below app is the output. You can see I have taken a label to display Account associated with the Contact. If you just add the field and choose the Company name from the field it will not display result rather it will display error.

So for this we have to use code to identify whether the look up value is pointing to a Contact or an Account. If Account prepare the lookup for Account and Display Account Name, If it points to Contact, Prepare contact lookup and display full name.

For this we have to change the Text property of the label that we have added to have the below formula.

If(
    IsBlank(ThisItem.'Company Name'),
    "---",
    IsType(ThisItem.'Company Name',Accounts),
    "Account:" & AsType( ThisItem.'Company Name',Accounts
    ).'Account Name', "Contact: " & AsType(
        ThisItem.'Company Name',Contacts
    ).'Full Name'
)

After you provide the formula it will display Lookup field value.

NOTE : When you want to display Look up value use the IsType and AsType methods to display text of lookup field.

HOW TO DISPLAY LOOKUP FIELD TEXT IN CANVAS APP DISPLAY FORM

From the gallery control when user clicks view more arrow icon it goes to a screen with a display form where we are displaying more fields about the contact. In this form also displaying Lookup is not straight forward. You have add a Custom card and add required Label and Dropdown then bind the Item property of the drop down as below.

In the Display screen I have added Email, Business Phone, Fax, First Name, last Name and Entity Image. But when I have added Company name its added but it shows error on the form.

So for this we have to add a Custom Card by clicking the 3 dots option form the Add fields panel and choose custom card.

Once the custom card is added drag 2 labels and rename as Account to first label. Select the second label and go to its Text Property and specify the below formula,

If( IsBlank( ThisItem.'Company Name' ), "--",
    IsType( ThisItem.'Company Name', Accounts ),
        "Account: " & AsType( ThisItem.'Company Name', Accounts ).'Account Name',
    "Contact: " & AsType( ThisItem.'Company Name', Contacts ).'Full Name'
)

Now the label will display the Account Lookup value Which was my requirement. Now let us see how we can add lookup fields in Edit Form.

HOW TO SET LOOKUP FIELD DEFAULT TEXT IN CANVAS APP EDIT FORM & UPDATE LOOKUP

Now we have taken a screen and added an Edit Form. Added all necessary fields in the edit form so that user can update the values. Simple fields no problem but if we have to add Lookup Field then we have to do the following task.

Open the Edit field option for the Edit Form and click on Add Field and choose Company Name lookup field. It will add a card to the edit form but inside the card no controls found. So we have to add a label to display as Company as label and a dropdown which will display the List of Accounts so that user can choose an account to update the record.

Here we have to do 2 things. First we have to set the default value of the dropdown to display the account which is already selected. and second we have to edit the save button to update the Lookup field value.

So let us start using some formula to achieve this.

Select the Dropdown and go to Default property and provide the formula as given below.

After this when the user will navigate to edit form from the gallery the dropdown will display the already associated Account with the contact as selected.

If(
    IsBlank(ThisItem.'Company Name'),
    "--",
    IsType(
        ThisItem.'Company Name',
        Accounts
    ),
    AsType(
        ThisItem.'Company Name',
        Accounts
    ).'Account Name',
    AsType(
        ThisItem.'Company Name',
        Contacts
    ).'Full Name'
)

Now the important thing is how we can update the lookup field. if you just simply use SubmitForm method it will not work. So we have to use Patch function with Lookup method to achieve this.

Check the code below how I called SubmitForm and Patch function for the same button. The fundamental is After the form data is submitted we are calling an update operation to update the contact with the account lookup.

Open Submit button and choose OnSelect property and provide the below code. make sure to update the code as per your use as I have taken the id of gallery as Gallery1 and Dropdown id as Dropdown4. So you have to updated according to your control ids.

SubmitForm(editform);
Patch(
    Contacts,
    LookUp(
        Contacts,
        contactid = Gallery1.Selected.contactid
    ),
    {
        'Company Name': LookUp(
            Accounts,
            accountid = Dropdown4.Selected.accountid
        )
    }
);
Notify("Record Updated")

Now if you change the lookup value and click submit button. It will save the new lookup value in the table.

Cheers. hope this helps.

 

One Response

  1. […] Type fields is ok but when it is a matter of Look Up Field its not straight forward so we need… Read more Read Complete Post and Comments SBX – Two Col […]

Comments are closed.