cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
SteveMC
Level: Powered On

Patch a Collection to SQL

I am trying to patch a collection to a SQL data source. The SQL table contains three columns which correspond to the collection labels. a YearKey (int) column, a HolidayDate (date) column, and a HolidayName (varchar(255)) column.

 The idea is to check if there is data in the SQL table for the current year, and if not, push the collection to the table... Finally, if there is an error, show a popup error message.

 

The code below throws no error, but the data is not written to the SQL table... I can't figure out the issue.. Does anyone see anything wrong with the code?

 

ClearCollect(HolidayNames, LookUp('[dbo].[HolidayNames]',YearKey = Year(Today())));
If(IsEmpty(HolidayNames),
     Collect(HolidayNames,
        { YearKey: Year(Today()),
         HolidayDate : Date(Year(Today()), 1, 1),
         HolidayName : "New Year's Day"},
         { YearKey: Year(Today()),
         HolidayDate : Date(Year(Today()), 7, 4),
         HolidayName : "Independence Day"},
        { YearKey: Year(Today()),
         HolidayDate : Date(Year(Today()), 12, 24),
         HolidayName : "Christmas Eve"},
        { YearKey: Year(Today()),
         HolidayDate : Date(Year(Today()), 12, 25),
         HolidayName : "Christmas Day"},
        { YearKey: Year(Today()),
         HolidayDate : Date(Year(Today()), 12, 31),
         HolidayName : "New Year's Eve"}        
        );
        Patch('[dbo].[HolidayNames]', HolidayNames)
);

  If(!IsEmpty(Errors('[dbo].[HolidayNames]')),
  UpdateContext({ErrorMessage : First(Errors('[dbo].[HolidayNames]')).Message});
  UpdateContext({showErrorPop:true}))

Thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Community Support Team
Community Support Team

Re: Patch a Collection to SQL

Hi @SteveMC,

 

Could you please share a bit more about the data structure of your SQL table?

 

Please take a try to modify your formula as below:

ClearCollect(HolidayNames, LookUp('[dbo].[HolidayNames]',YearKey = Year(Today())));
If(IsEmpty(HolidayNames),
     Collect(HolidayNames,
         {
YearKey: Year(Today()), HolidayDate : Date(Year(Today()), 1, 1), HolidayName : "New Year's Day"
}, {
YearKey: Year(Today()), HolidayDate : Date(Year(Today()), 7, 4), HolidayName : "Independence Day"
}, {
YearKey: Year(Today()), HolidayDate : Date(Year(Today()), 12, 24), HolidayName : "Christmas Eve"
}, {
YearKey: Year(Today()), HolidayDate : Date(Year(Today()), 12, 25), HolidayName : "Christmas Day"
}, {
YearKey: Year(Today()), HolidayDate : Date(Year(Today()), 12, 31), HolidayName : "New Year's Eve"
} ); Patch(
'[dbo].[HolidayNames]',
Defaults('[dbo].[HolidayNames]'), /* <-- Modify here */
HolidayNames
) ); If(!IsEmpty(Errors('[dbo].[HolidayNames]')), UpdateContext({ErrorMessage : First(Errors('[dbo].[HolidayNames]')).Message}); UpdateContext({showErrorPop:true})
)

 

In addition, you could also consider take a try to modify your formula as below:

ClearCollect(HolidayNames, LookUp('[dbo].[HolidayNames]',YearKey = Year(Today())));
If(IsEmpty(HolidayNames),
     Collect(HolidayNames,
         {
YearKey: Year(Today()), HolidayDate : Date(Year(Today()), 1, 1), HolidayName : "New Year's Day"
}, {
YearKey: Year(Today()), HolidayDate : Date(Year(Today()), 7, 4), HolidayName : "Independence Day"
}, {
YearKey: Year(Today()), HolidayDate : Date(Year(Today()), 12, 24), HolidayName : "Christmas Eve"
}, {
YearKey: Year(Today()), HolidayDate : Date(Year(Today()), 12, 25), HolidayName : "Christmas Day"
}, {
YearKey: Year(Today()), HolidayDate : Date(Year(Today()), 12, 31), HolidayName : "New Year's Eve"
} ); ForAll( /* <-- Modify here start */
RenameColumns(
RenameColumns(
RenameColumns(HolidayNames,"YearKey","YearKey1"),
"HolidayDate","HolidayDate1"
),
"HolidayName","HolidayName1"
),
Patch(
'[dbo].[HolidayNames]',
Defaults('[dbo].[HolidayNames]'),
{
YearKey:YearKey1,
HolidayDate:HolidayDate1,
HolidayName:HolidayName1
}
)
) /* <-- Modify here end */ ); If(!IsEmpty(Errors('[dbo].[HolidayNames]')), UpdateContext({ErrorMessage : First(Errors('[dbo].[HolidayNames]')).Message}); UpdateContext({showErrorPop:true})
)

Note: Please make sure that you have defined a primary key within your SQL table.

 

 

More details about the Patch function and ForAll function in PowerApps, please check the following article:

Patch function, ForAll function

 

Best regards,

Kris

 

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

View solution in original post

2 REPLIES 2
Community Support Team
Community Support Team

Re: Patch a Collection to SQL

Hi @SteveMC,

 

Could you please share a bit more about the data structure of your SQL table?

 

Please take a try to modify your formula as below:

ClearCollect(HolidayNames, LookUp('[dbo].[HolidayNames]',YearKey = Year(Today())));
If(IsEmpty(HolidayNames),
     Collect(HolidayNames,
         {
YearKey: Year(Today()), HolidayDate : Date(Year(Today()), 1, 1), HolidayName : "New Year's Day"
}, {
YearKey: Year(Today()), HolidayDate : Date(Year(Today()), 7, 4), HolidayName : "Independence Day"
}, {
YearKey: Year(Today()), HolidayDate : Date(Year(Today()), 12, 24), HolidayName : "Christmas Eve"
}, {
YearKey: Year(Today()), HolidayDate : Date(Year(Today()), 12, 25), HolidayName : "Christmas Day"
}, {
YearKey: Year(Today()), HolidayDate : Date(Year(Today()), 12, 31), HolidayName : "New Year's Eve"
} ); Patch(
'[dbo].[HolidayNames]',
Defaults('[dbo].[HolidayNames]'), /* <-- Modify here */
HolidayNames
) ); If(!IsEmpty(Errors('[dbo].[HolidayNames]')), UpdateContext({ErrorMessage : First(Errors('[dbo].[HolidayNames]')).Message}); UpdateContext({showErrorPop:true})
)

 

In addition, you could also consider take a try to modify your formula as below:

ClearCollect(HolidayNames, LookUp('[dbo].[HolidayNames]',YearKey = Year(Today())));
If(IsEmpty(HolidayNames),
     Collect(HolidayNames,
         {
YearKey: Year(Today()), HolidayDate : Date(Year(Today()), 1, 1), HolidayName : "New Year's Day"
}, {
YearKey: Year(Today()), HolidayDate : Date(Year(Today()), 7, 4), HolidayName : "Independence Day"
}, {
YearKey: Year(Today()), HolidayDate : Date(Year(Today()), 12, 24), HolidayName : "Christmas Eve"
}, {
YearKey: Year(Today()), HolidayDate : Date(Year(Today()), 12, 25), HolidayName : "Christmas Day"
}, {
YearKey: Year(Today()), HolidayDate : Date(Year(Today()), 12, 31), HolidayName : "New Year's Eve"
} ); ForAll( /* <-- Modify here start */
RenameColumns(
RenameColumns(
RenameColumns(HolidayNames,"YearKey","YearKey1"),
"HolidayDate","HolidayDate1"
),
"HolidayName","HolidayName1"
),
Patch(
'[dbo].[HolidayNames]',
Defaults('[dbo].[HolidayNames]'),
{
YearKey:YearKey1,
HolidayDate:HolidayDate1,
HolidayName:HolidayName1
}
)
) /* <-- Modify here end */ ); If(!IsEmpty(Errors('[dbo].[HolidayNames]')), UpdateContext({ErrorMessage : First(Errors('[dbo].[HolidayNames]')).Message}); UpdateContext({showErrorPop:true})
)

Note: Please make sure that you have defined a primary key within your SQL table.

 

 

More details about the Patch function and ForAll function in PowerApps, please check the following article:

Patch function, ForAll function

 

Best regards,

Kris

 

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

View solution in original post

Highlighted
SteveMC
Level: Powered On

Re: Patch a Collection to SQL

Thank you for your help Kris! The issue was the SQL table did not have a primary key... Your code examples were also very helpful!

 

Thanks again,

Steve

Helpful resources

Announcements
thirdimage

Power Apps Super User Class of 2020

Check it out!

thirdimage

New Badges

Check it out!

thirdimage

Power Apps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

SecondImage

Difinity Conference

The largest Power BI, Power Platform, and Data conference in New Zealand

Top Solution Authors
Top Kudoed Authors
Users online (4,801)