web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Add or Update record i...
Power Apps
Unanswered

Add or Update record in SQL DB from PowerApp

(0) ShareShare
ReportReport
Posted on by 2

Hi, 

 

I have a PowerApp where all data is entered in collections. When you exit the app you are promted to save the data in a SQL DB on a button click. 

 

I've succesfully added new records in the SQL based on my collection "colPrøver". But if i try to update a row, based on GUID, I get an error that basically says "Violation of PRIMARY KEY constraint ...  Cannot insert duplicate key value in object 'dbo.Samples'. The dublicate key value is (xxxxxx-xxxx-xxxx-xxxxxxxxxx)"

 

dbo.Samples or Samples is my SQL table and SampleId is the primary key, which i compare my colPrøver GUID against (those two are the same). 

 

Here is my code OnSelect button: 

 

 

 

 

ForAll(
 colPrøver,
 If(
 IsBlank(
 LookUp(
 Samples,
 SampleId = GUID(prøverGUID)
 ).PointId
 ),
 Patch(
 Samples,
 Defaults(Samples),
 {
 SampleId: GUID(prøverGUID),
 PointId: GUID(varBoringGUID),
 SampleNo: prøverID,
 SampleType: prøverTypeGeoGIS,
 Depth1: prøverStartDybde,
 Depth2: prøverSlutDybde,
 Length: If(
 IsBlank(prøverSlutDybde),
 0,
 Abs(prøverSlutDybde - prøverStartDybde)
 ),
 CoreLostFromTop: 0,
 Description2: "Test af PAH - Oprettet"
 }
 ),
 Patch(
 Samples,
 LookUp(
 Samples,
 SampleId = GUID(prøverGUID),
 {
 SampleNo: prøverID,
 SampleType: prøverTypeGeoGIS,
 Depth1: prøverStartDybde,
 Depth2: prøverSlutDybde,
 Length: If(
 IsBlank(prøverSlutDybde),
 0,
 Abs(prøverSlutDybde - prøverStartDybde)
 ),
 Description2: "PAH - Redigeret"
 }
 )
 )
 )
);

 

 

 

 

What am I missing, since it won't update the record if it already exist? 

I've checked for dublicates on the specific GUID but i don't see any. I think is gives me that specific GUID as error, because it's the first record in colPrøver. 

Categories:
  • StalinPonnusamy Profile Picture
    Super User 2024 Season 1 on at

    Hi @PeterAndreas99 

     

    For SQL, we don't require LookUp instead just pass the UniqueId as another row.

     

    Please try this

    ForAll(
     colPrøver,
     If(
     IsBlank(
     LookUp(
     Samples,
     SampleId = GUID(prøverGUID)
     ).PointId
     ),
     Patch(
     Samples,
     Defaults(Samples),
     {
     SampleId: GUID(prøverGUID),
     PointId: GUID(varBoringGUID),
     SampleNo: prøverID,
     SampleType: prøverTypeGeoGIS,
     Depth1: prøverStartDybde,
     Depth2: prøverSlutDybde,
     Length: If(
     IsBlank(prøverSlutDybde),
     0,
     Abs(prøverSlutDybde - prøverStartDybde)
     ),
     CoreLostFromTop: 0,
     Description2: "Test af PAH - Oprettet"
     }
     ),
     Patch(
     Samples,
     {
     SampleId = GUID(prøverGUID),
     SampleNo: prøverID,
     SampleType: prøverTypeGeoGIS,
     Depth1: prøverStartDybde,
     Depth2: prøverSlutDybde,
     Length: If(
     IsBlank(prøverSlutDybde),
     0,
     Abs(prøverSlutDybde - prøverStartDybde)
     ),
     Description2: "PAH - Redigeret"
     }
     )
     )
     )
    );

     


    Thanks,
    Stalin - Learn To Illuminate

  • PeterAndreas99 Profile Picture
    2 on at

    Hi @StalinPonnusamy ,

     

    I've tried your proposal but got "unexpected character" here: 

     

    Patch(
     Samples,
     {
     SampleId = GUID(prøverGUID),
     SampleNo: prøverID,

     

    So i changed the "SampleId = GUID(prøverGUID)" to "SampleId: GUID(prøverGUID)". And then i get the same Error as before 😞 

  • StalinPonnusamy Profile Picture
    Super User 2024 Season 1 on at

    Hi @PeterAndreas99 

     

    I'm reading again the main issue and looks we are sending existing GUID when we create New Record. I tested from my end. For New record, just passing GUID() and works

     

    ForAll(
     colPrøver,
     If(
     IsBlank(
     LookUp(
     Samples,
     SampleId = GUID(prøverGUID)
     ).PointId
     ),
     Patch(
     Samples,
     Defaults(Samples),
     {
     SampleId: GUID(),
     PointId: GUID(varBoringGUID),
     SampleNo: prøverID,
     SampleType: prøverTypeGeoGIS,
     Depth1: prøverStartDybde,
     Depth2: prøverSlutDybde,
     Length: If(
     IsBlank(prøverSlutDybde),
     0,
     Abs(prøverSlutDybde - prøverStartDybde)
     ),
     CoreLostFromTop: 0,
     Description2: "Test af PAH - Oprettet"
     }
     ),
     Patch(
     Samples,
     LookUp(
     Samples,
     SampleId = GUID(prøverGUID),
     {
     SampleNo: prøverID,
     SampleType: prøverTypeGeoGIS,
     Depth1: prøverStartDybde,
     Depth2: prøverSlutDybde,
     Length: If(
     IsBlank(prøverSlutDybde),
     0,
     Abs(prøverSlutDybde - prøverStartDybde)
     ),
     Description2: "PAH - Redigeret"
     }
     )
     )
     )
    );

     

    Changes

    Defaults(Samples),
    {
    SampleId: GUID(),

     


    Thanks,
    Stalin - Learn To Illuminate

     

     

  • StalinPonnusamy Profile Picture
    Super User 2024 Season 1 on at

    Hi @PeterAndreas99 

     

    For Reference, Here is the code works for me new and Update

    With(
     {
     item: LookUp(
     '[dbo].[StudentAutoGUID]',
     Id = GUID(DataCardValue7.Text)
     )
     },
     If(
     IsBlank(item) || IsEmpty(item),
     Patch(
     '[dbo].[StudentAutoGUID]',
     Defaults('[dbo].[StudentAutoGUID]'),
     {
     Id: GUID(),
     StudentName: StudentNameDataCardValue.Text,
     Address1: DataCardValue8.Text
     }
     ),
     Patch(
     '[dbo].[StudentAutoGUID]',
     item,
     {StudentName: StudentNameDataCardValue.Text}
     )
     )
    );
  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Hi @StalinPonnusamy 

     

    I'm writing from my new account (company account). 

     

    I've followed your example and it seems that my IF formula never returns false (that the item contains something). Because it make a new record each time i run it. 

     

    Maybe it's because the GUID(prøver GUID) is in some kind of wrong format. I have to be honest and say that some of the GUID i've made from outside the database, bacause i've run on som test before i connected to the SQL DB. So maybe that could be the issue? 

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    I also get a delegation error  on the comparison. 

     

    PeterABH_0-1637742345785.png

     

     

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Hi @StalinPonnusamy 

     

    I've cracked the code 😄 

     

    ForAll(
     colPrøver,
     With(
     {varPrøverGUID: GUID(prøverGUID)},
     If(
     IsBlank(
     LookUp(
     Samples,
     SampleId = varPrøverGUID
     )
     ),
     Patch(
     Samples,
     Defaults(Samples),
     {
     SampleId: GUID(prøverGUID),
     PointId: GUID(varBoringGUID),
     SampleNo: prøverID,
     SampleType: prøverTypeGeoGIS,
     Depth1: prøverStartDybde,
     Depth2: prøverSlutDybde,
     Length: If(
     IsBlank(prøverSlutDybde),
     0,
     Abs(prøverSlutDybde - prøverStartDybde)
     ),
     CoreLostFromTop: 0,
     Description2: "Oprettet"
     }
     ),
     Patch(
     Samples,
     LookUp(
     Samples,
     SampleId = varPrøverGUID
     ),
     {
     SampleNo: prøverID,
     SampleType: prøverTypeGeoGIS,
     Depth1: prøverStartDybde,
     Depth2: prøverSlutDybde,
     Length: If(
     IsBlank(prøverSlutDybde),
     0,
     Abs(prøverSlutDybde - prøverStartDybde)
     ),
     Description2: "Redigeret"
     }
     )
     )
     )
    );

     

    So instead of having the GUID(prøverGUID) in the LookUp i saved it in a variable with the WITH formula. 

    So your introduction to WITH has helped me alot - Thank you for your help! 

  • StalinPonnusamy Profile Picture
    Super User 2024 Season 1 on at

    HI @Anonymous 

     

    With the Time zone differences looks you solved the issue. Good to hear that the problem is solved. Two things on this

    1. Converting a string to GUID is a function which not delegable. So using With a function that is converted before using it.
    2. When using the Lookup, we can verify the blank record itself instead of verifying any column 
      • IsBlank(LookUp(Samples,SampleId = varPrøverGUID)) 



    Thanks,
    Stalin - Learn To Illuminate

  • StalinPonnusamy Profile Picture
    Super User 2024 Season 1 on at

    Hi @Anonymous 


    Please let us know if anything needs on your post. We can help with this.

    Please mark the post as Solved If I have answered your question.
    Please give it a Thumbs Up if you find the suggestion helpful

    Thanks,
    Stalin - Learn To Illuminate

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 395 Super User 2026 Season 2

#2
Mohsin Ali Profile Picture

Mohsin Ali 328

#3
WarrenBelz Profile Picture

WarrenBelz 260 Most Valuable Professional

Last 30 days Overall leaderboard