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 / Adding Records to SQL ...
Power Apps
Answered

Adding Records to SQL Server Table with Auto-Increment Identity Column

(0) ShareShare
ReportReport
Posted on by 763 Super User 2024 Season 1

I have a SQL Table defined like this:

CREATE TABLE [powrapps].[tbl_Amenity](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Title] [nvarchar](255) NOT NULL,
	[PropertyId] [int] NOT NULL,
	[Active] [bit] NOT NULL,
	[Created] [datetime] NULL,
	[CreatedBy] [nvarchar](255) NULL,
	[Modified] [datetime] NULL,
	[ModifiedBy] [nvarchar](255) NULL,
 CONSTRAINT [PK_tbl_Amenity] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

I am trying to insert a new record into that table with the following code:

Patch(
 'powrapps.tbl_Amenity',
 Defaults( 'powrapps.tbl_Amenity' ),
 {
 PropertyId: 39,
 Title: "Testing 123",
 Active: 0,
 Created: Now(),
 CreatedBy: gblAppData.User.Email,
 Modified: Now(),
 ModifiedBy: gblAppData.User.Email
 }
)

That is throwing the following error:

sperry1625_0-1692297933541.png

The error makes no sense. I have also tried specifying the Id column as Blank(). Same result. It looks like the Patch() statement is trying to perform an update, based on the error. I also tried specifying an actual Id number but get the error:

sperry1625_1-1692298106767.png

This makes sense for what I tried to do.

 

I have seen older posts that this is not supported. Please say it isn't so! I really do NOT want to use stored procedures, they are a PITA to use. This is possible with EditForms, but don't like them either. Should Patch() work?

 

Thank you.

 

 

Categories:
I have the same question (0)
  • Devvj Profile Picture
    1,132 Super User 2024 Season 1 on at

    Hi,
    read about a guy having a similar problem with the patch function some time ago, for him it eventually worked when he restarted the SQL Server after the changes were made, and reconnected and refreshed the datasource in PowerApps.

    Maybe that could help.

    -------------------------------------------------------------------------
    If this is the answer for your question, please mark the post as Solved.
    If this answer helps you in any way, please give it a like.

  • sperry1625 Profile Picture
    763 Super User 2024 Season 1 on at

    It's an Azure SQL Server, cannot really stop and start it. Too many other items on the server. I can try refreshing the connection and see if that helps.

     

     

  • Verified answer
    sperry1625 Profile Picture
    763 Super User 2024 Season 1 on at

    I found the solution to this.  It was stupid simple, but it works. My original Patch() statement was as follows:

    Patch(
     'powrapps.tbl_Amenity',
     Defaults( 'powrapps.tbl_Amenity' ),
     {
     PropertyId: 39,
     Title: "Testing 123",
     Active: 0,
     Created: Now(),
     CreatedBy: gblAppData.User.Email,
     Modified: Now(),
     ModifiedBy: gblAppData.User.Email
     }
    )

    The fix to get it to work is to drop the Defaults() parameter. The new Patch() statement is as follows and works to insert the new record:

    Patch(
     'powrapps.tbl_Amenity',
     {
     PropertyId: 39,
     Title: "Testing 123",
     Active: 0,
     Created: Now(),
     CreatedBy: gblAppData.User.Email,
     Modified: Now(),
     ModifiedBy: gblAppData.User.Email
     }
    )

     

    That will insert a new record successfully.

     

    The last hurdle for this is to grab the Id that was created for the record. I achieve this by doing the following:

    UpdateContext(
     {
     ctxLastId: Last( 'powrapps.tbl_Amenity' ).Id
     }
    );

     

    All comments welcome.

     

    Thank you.

     

     

  • EddieE Profile Picture
    4,641 Moderator on at

    @sperry1625 

    It's generally a better solution to wrap your Patch inside your variable statement to ensure you do get the record just created by the patch. You can do this to achieve that 

     

    UpdateContext(
     {
     ctxLastId: Patch(
     'powrapps.tbl_Amenity',
     {
     PropertyId: 39,
     Title: "Testing 123",
     Active: 0,
     Created: Now(),
     CreatedBy: gblAppData.User.Email,
     Modified: Now(),
     ModifiedBy: gblAppData.User.Email
     }
     ).Id
     }
    );

     

    If you use Last() you may / may not always get the corresponding record due to multiple users using your app at the same time.

     

    This method works in a similar way to the OnSuccess property of a Form.

  • sperry1625 Profile Picture
    763 Super User 2024 Season 1 on at

    I agree with your statement in general, but it doesn't work without an update to the Patch() statement.

    The Patch() statement as it stands will only return the columns passed to it, not the full record created. So, my Patch() will not return the Id column, which is the Auto-increment column, or any other column not passed (I have two datetime columns that have default values). To solve this, I adjusted my Patch() as follows:

    With(
     Patch(
     'powrapps.tbl_Amenity',
     {
     Id: Blank(),
     PropertyId: cmbHSMCAFProperty.Selected.Id,
     Title: txtHSMCAFName.Text,
     Active: If( radHSMCAFActive.Selected.Value = "Yes", 1, 0 ),
     Created: Blank(),
     CreatedBy: gblAppData.User.Email,
     Modified: Blank(),
     ModifiedBy: gblAppData.User.Email
     }
     ) As newAmenity,
     Collect(
     colAmenityGridItems,
     newAmenity
     )
    );
    

    Notice the additional columns, Id (auto-increment), Created (Default GETDATE()), and Modified (Default GETDATE()). With these changes Patch() will return the full record that I can use to add to my collection.

     

    Thank you for the suggestion.

     

     

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 505

#2
WarrenBelz Profile Picture

WarrenBelz 502 Most Valuable Professional

#3
Haque Profile Picture

Haque 324

Last 30 days Overall leaderboard