There are many scenarios where we need to generate unique GUID whenever user performs certain action. One of the main scenarios is to link child records with parent records when app is in offline mode. In relationship model, child is linked to parent through Lookup which is complex object and combination of ID and Text fields.
When app is online and connected, this ID is automatically generated by platform (CDS or Dynamics 365). However, in offline mode, we need to generate this ID, or we can say unique GUID for each new parent record created.
Currently within PowerApps there is no inbuilt function to generate Unique GUID. Maybe it will be included in future releases. In this blog we will see how we can use Azure function API to generate unique GUID and consume in PowerApps through custom connector.
Create Azure Function:
- In Azure portal, click on Create a Resource and select Function App under Compute
- Give it a name “RetrieveGUID”. Select subscription, resource group, location and all other mandatory fields. Click on Create.
- Once created, click on it to open Function App. Click on “+” to create new function under this Function App. Select Custom function option from right hand bottom since we will be creating Http C# trigger.
- Click on C# under Http Trigger, Give your function a name “GenerateGUID”. Select Authorization level, I have selected Anonymous for demo purpose which means anyone can access this API without authentication. Click on Create
- Remove existing code from within function and paste below code to just return unique GUID every time this function is called. Click on Save and run. You will see GUID value in Output window which means function is running fine.
return req.CreateResponse(HttpStatusCode.OK, Guid.NewGuid());
- Click on Get Function URL to copy URL to be used while creating custom connector in PowerApps
Create Custom Connector:
- In PowerApp web portal, select Custom Connectors under Data and Click on Create Custom Connector and select Create from blank
- Give a name to your custom connector say “CustomFunctionAPI” and click on Continue
- Provide value for Host as “<<functionapiname>>.azurewebsites.net”. In our case it would be “retrieveguid.azurewebsites.net”. You can get host name from function URL as well copied from above. Click on Security
- Click on Definition since we are not authenticating our API for now. In Definition tab, click on New Action to provide operation name “GenerateGUID”. Operation name provided here will be accessible in PowerApp while calling this function API.
- Scroll down and click on “Import from Sample” under request. Sample request format helps connector to understand request JSON schema for validation. Select Post as operation type, paste function URL copied from above and add sample request object in body. Request body, you can copy from your function test runs as shown below. Click on Import
- Scroll further down and click on Add default response under Response. Add sample response object in body. Response body also you can copy from your function test run output window. Click on Import
- Click on Create Connector now on top. Once created, go to Test tab, click on New Connection to establish connection with your custom connector. Click on Create
Consume API in PowerApp:
- Go to your App under Apps and click on Edit. Click on Data Sources under View tab to add new connection we created above.
- Click on + Add Data Source and select connection we created for our custom connector. Connection will be added to your list of data sources.
- Add a blank screen and insert one button and label. Set button text as “Retrieve GUID” and no text for label as shown below:
- Set below formula in OnSelect property of button to set a variable “uniqueGUID” with GUID value returned from function API.
- Set Text property of label to “uniqueGUID” variable
- Now run the app, click on button. Label will be set with unique GUID every time you click on button