Fetch record based on any unique attribute and update the record using patch in canvas app

 A Canvas app in Power Apps is a type of app that allows you to build custom business applications for various purposes, such as data collection, process automation, and data visualization. You can build Canvas apps using a drag-and-drop interface, with no coding required. You can also add custom logic to your app using Power Apps formulas and functions, or by integrating with other services and APIs.


In this blog we are going to discuss a specific scenario which is to fetch a particular record fulfilling our filtering criteria and modifying the record using Patch function.
We have a Contact table in Dataverse and each record in the Contact table has a unique AutoNumber. Here we are trying to fetch the record by calling the unique AutNumber value and updating another attribute named Score of the record.

Step 1:

Add a textbox and a button to a blank page of canvas app. We will set the names of textbox and button as txtBox and btn respectively.



Step 2:

In the OnSelect property of the button, provide the expression

Set(
    varName,
    First(
        Filter(
            Contacts,
            AutoNumber = Value(txtBox.Text)
        )
    )
)

Here we are filtering out the record from Contacts based on our criteria. In our case the value which we enter in the textbox is the unique AutoNumber. The function will be fetching the record with the number which we have given in through the textbox txtBoxThe fetched record will be saved in the variable varName.

Step 3:
Now add a new text label txtlbl and in the Text property, add varName.'Full Name'. Since the fetched record is stored in varName, the full name will be displayed in the label.


Now try adding an AutoNumber in the text field and click on Fetch button. If a record with the given AutoNumber is present, it will be displayed in the label output control.

Here we have given a known AutoNumber which is 999 which belongs to Clint Eastwood. On clicking the Fetch button, the record of Clint Eastwood will be stored into the variable varName. Since we are calling the Full name property (varName.'Full Name') from the variable, only the full name will be displayed in the label control.

Step 3:
Since we successfully retrieved the record, we can now try to patch some information to the record. We will try to patch a value to an attribute of the record named 'Score'. For that add a textbox and a button and rename it to txtBox2 and btn2 respectively. In the OnSelect property of btn2 add the formula as given below.

Patch(
    Contacts,
    SelectedRecord,
    {Score: Value(txtBox2.Text)}
);


Now on click of the Patch button, the value given in the textbox will get patched to the selected record. We have given a score value of 55, in the image below we can see that the value 55 got saved in the Score field of corresponding record in Dataverse.


Similarly, more attributes of the record can be added or modified using Patch function. Also the records can be fetched using any of the unique attributes.

Comments

Popular posts from this blog

How to Prevent Past/Future Dates in PowerApps Using jQuery