cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
EdHansberry
Kudo Collector
Kudo Collector

Adding a new record to SQL Server where there is a uniqueidenfier with a GUID

NOTE: whomever marked my last question as SPAM I would appreciate it if you would not do that. This is a legitimate request for help, and am mystified as to why someone flagged it as spam.

 

I have a form to edit/add records in a SQL table. Edits are easy. Adding not so much.

 

The table has a field that is the primary key, field type "uniqueidentifier", and is a GUID. I cannot set it as Identity YES on the SQL server where it would automatically populate on the server as that apparently only works with integers, not uniqueidentifier fields. (In otherwords, the solution here doesn't work)

 

I cannot for the life of me get a data card that has a normal DataCardValue that will let me stick GUID() in there. It is a required field, but adding the card doesn't give the the StarVisible or ErrorMessage fields, so PowerApps knows something is weird about this field.

 

This is how the field looks on the form:

20190715 18_27_20-Customer Service Maintenance - Saved (Unpublished) - PowerApps.png

You can see it doesn't have the bold blue box around it. It is like it is an uneditable field, which I don't want the user to edit. I just have it here for troubleshooting and was trying to get the UID field to be a GUID() function if this was a NEW form, but SubmitForm() won't take it.

 

So I used Patch(). This is my "Save" button function.

 

If(
    frmBBBFacilityCodeEdit.Mode = FormMode.Edit,
    SubmitForm(frmBBBFacilityCodeEdit),
    Patch(
        '[dbo].[tblBBBPoolFacilityCodes]',
        Defaults('[dbo].[tblBBBPoolFacilityCodes]'),
        {
            FacilityCode: DataCardValue1.Text,
            BuildingID: DataCardValue2.Text,
            ProviderName: DataCardValue3.Text,
            StreetAddress: DataCardValue11.Text,
            City: DataCardValue4.Text,
            State: DataCardValue10.Text,
            ZipCode: DataCardValue13.Text,
            TransitDays: Value(DataCardValue12.Text),
            StartDate: DataCardValue9.SelectedDate,
            UID: GUID()
        }
    )
);
Navigate(
    scrBBBFacilityCodes,
    ScreenTransition.Fade
);
UpdateContext({FacilityCodeUpperCase: ""})

Now, saving does add the record, and the last part of the PATCH() function adds a GUID, so this works, but this is U.G.L.Y. If this has to be this way, I will rename all of my DataCardValueX cards to something meaningful, as well as add the rest of the fields that are not required (about 6 fields missing).

 

But is there any other way to add a GUID to a record that is required? I cannot get it to work with SubmitForm() since it doesn't seem to see the UID field properly

1 ACCEPTED SOLUTION

Accepted Solutions

Hi Yutliu,

 

It is actually supported. In my PATCH() statement, I am generating and writing to the GUID field, and SQL Server happliy accepts it in the uniqueidentifier field set to only take a GUID.

 

GUID() writes a valid GUID to SQL ServerGUID() writes a valid GUID to SQL Server

It is just a pain because as far as I can tell, I must use PATCH() to do it, which means I have:

  1. Explicitly write each required field in one PATCH() function.
  2. Write optional fields that have a constraint (like a phone number field) after confirming they meet the restraint, and are not blank.
  3. Write optoinal fields that are not blank and have no constraints. 

SUBMITFORM() does all of the above much more easily, except it cannot handle the GUID field even if I try to feed the GUID() result to it.

Thanks for the other link. Additional info always helpful.

I'm going to accept my own answer as a solution unless someone has something better for me and I'll change it to a better answer as the solution.

View solution in original post

6 REPLIES 6
v-yutliu-msft
Community Support
Community Support

Hi @EdHansberry ,

Do you want to update an uniqueidentifier field of SQL Server in PowerApps?
I'm afraid it's not supported to read and write GUID data type from CDS or SQL yet.

Here's a similar issue for your reference:

https://powerusers.microsoft.com/t5/General-Discussion/How-to-get-the-GUID-of-the-newly-created-d365...

As analternative way, I suggest you insert a char type in SQL server to record Guid.

And set this column's data to GUID().

In this way, this column will be updated successfullt with Guid data.

 

 

Best regards,

Community Support Team _ Phoebe Liu

Community Support Team _ Phoebe Liu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi Yutliu,

 

It is actually supported. In my PATCH() statement, I am generating and writing to the GUID field, and SQL Server happliy accepts it in the uniqueidentifier field set to only take a GUID.

 

GUID() writes a valid GUID to SQL ServerGUID() writes a valid GUID to SQL Server

It is just a pain because as far as I can tell, I must use PATCH() to do it, which means I have:

  1. Explicitly write each required field in one PATCH() function.
  2. Write optional fields that have a constraint (like a phone number field) after confirming they meet the restraint, and are not blank.
  3. Write optoinal fields that are not blank and have no constraints. 

SUBMITFORM() does all of the above much more easily, except it cannot handle the GUID field even if I try to feed the GUID() result to it.

Thanks for the other link. Additional info always helpful.

I'm going to accept my own answer as a solution unless someone has something better for me and I'll change it to a better answer as the solution.

View solution in original post

dnourie
Advocate III
Advocate III

Spoiler
I have the same issue. There is no "Update" property for the datacard, so there is no way to use a form to generate a GUID and save it to the SQL database. @EdHansberry  used a Patch() function, but as he said, it is not a good solution when the SubmitForm function should be enough. Anyone have an easier workaround? You should not have marked this as solved Ed!

I marked it as solved @dnourie because it did work for my use case. It may not in all use cases. The real solution is to not use a GUID at all but create a new field that is a integer, is unique, is required, is indexed, and finally, the Identity Specification "Is Identity" is set to yes. This will auto-increment for each record you add via Power Apps regardless of how and you never need to manually update it.

But that requires modifying the table, which isn't always possible, but now, whenever I create a new table, I always add this column.

I agree @EdHansberry  that your solution does work, as a workaround. Ideally, when a uniqueidentifier field is required by the datasource, the Power Apps form should be able to recognize that (which it does) and rather than eliminating the update field in the form, providing guidance to use "GUID()" (or doing that automatically and making it editable). Providing us with no options to handle this situation, other than using a workaround, does not make sense to me. Thanks for the quick reply! I have used your method as well, and it is working, but I have many forms and this seems like extra work!

MasterB
Advocate II
Advocate II

Run in to the same problem.

 

Please vote for a prober implementation at:

https://powerusers.microsoft.com/t5/Power-Apps-Ideas/Support-SQL-Server-uniqueidentifier-PK-columns-...

 

Thanks

Jan

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.

PowerPlatform 768x460.png

Microsoft Learn

Check out our new Discover Your Career Path blog post series and get all the details.

Users online (3,354)