cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
DileepGolla
Advocate V
Advocate V

Check if Patch has saved the data successfully

Hi,

Is there any way we can validate a Patch function? We are using Patch to save data to the SQL table. We need to notify the user if there has been any error saving/updating the data.

 

I tried using IsError() function, but it always gives "false" as output whether on not there has been an error with the Patch function.

 

Button.Onselect -> Set(var_errorOccured,IsError(Patch('[dbo].[TableName]',Defaults('TableName'),{
Column1: GUID(),CREATED_DATE: Now(),CREATED_BY: User().FullName})))

 

Even if the data is saved successfully without any error, IsError() returns only false.

 

Is there any alternate approach that we can validate this.

 

Thanks,

Dileep

1 ACCEPTED SOLUTION

Accepted Solutions
v-xida-msft
Community Support
Community Support

Hi @DileepGolla,

Based on the formula that you provided, I have made a test on my side, and the issue is confirmed on my side.

Whether or not there is an error with the Patch function, the IsError function would always return false.

As an alternative solution, I think Errors function could achieve your needs. The user @Roberts2727 has faced similar issue with you, please check my response within the following thread:

https://powerusers.microsoft.com/t5/General-Discussion/Patch-with-navigate-stay-on-page-if-there-is-...

I have made a test on my side, please take a try with the following workaround:7.JPG

Set the OnSelect property of the "Patch" button to following formula:

If(
    IsEmpty(
        Errors('20181017_case12',Patch('20181017_case12',Defaults('20181017_case12'),{First_x0020_Name:"Teresa3"}))
    ),
    Notify("Patch Successfully",NotificationType.Success),
    Notify("There is an error with your Patch function",NotificationType.Error)
)

Note: The '20181017_case12' represents the SP list data source within my app.

If I don't provide a value for the Title column which is a required field in my SP list, the error notification would show up as below:Test1.gif

If I provide a value for the Title column, the success notification shows up as below:Test1.gif

On your side, you should type the following formula:

 

If(
    IsEmpty(
        Errors(
           '[dbo].[TableName]',
            Patch('[dbo].[TableName]',Defaults('[dbo].[TableName]'),{Column1: GUID(),CREATED_DATE: Now(),CREATED_BY: User().FullName})
       )
    ),
    Notify("Patch Successfully",NotificationType.Success),
    Notify("There is an error with your Patch function",NotificationType.Error)
)

More details about the Errors function, please check the following article:

Errors 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

7 REPLIES 7
tommyly
Responsive Resident
Responsive Resident

You can create a validation object--local variable based off of your GUID, CREATED_DATE, and CREATED_BY columns. For example (yes, UpdateContext can contain a record or table; adding First casts it as a record):

 

UpdateContext({privateValidation:
  First(Table({
    Column1: GUID(),
    CREATED_DATE: Now(),
    CREATED_BY: User().FullName
  }))
})

Casting a table/object as a record makes your life easier by not having to continually type "First(privateValidation).xyz". Once you have created your validation object, now when you patch your data, you will patch it using the variables from your validation data:

Patch('[dbo].[TableName]', Defaults('TableName'),
  {
    Column1: privateValidation.Column1,
    CREATED_DATE: privateValidation.CREATED_DATE,
    CREATED_BY: privateValidation.FullName
  }
)

This achieves the patching command you were attempting in your example. Now to validate:

Set(var_errorOccured,
  IsEmpty(Filter('[dbo].[TableName]',
    Column1=privateValidation.Column1,
    CREATED_DATE=privateValidation.CREATED_DATE,
    CREATED_BY=privateValidation.CREATED_BY
  )
);
UpdateContext({privateValidation: Blank()})

Since the values are stored in the validation object, you can use that data to check if the data exists in the table by using an IsEmpty command.

 

I'm sure there are many ways to do this, but hopefully this helps.

 

v-xida-msft
Community Support
Community Support

Hi @DileepGolla,

Based on the formula that you provided, I have made a test on my side, and the issue is confirmed on my side.

Whether or not there is an error with the Patch function, the IsError function would always return false.

As an alternative solution, I think Errors function could achieve your needs. The user @Roberts2727 has faced similar issue with you, please check my response within the following thread:

https://powerusers.microsoft.com/t5/General-Discussion/Patch-with-navigate-stay-on-page-if-there-is-...

I have made a test on my side, please take a try with the following workaround:7.JPG

Set the OnSelect property of the "Patch" button to following formula:

If(
    IsEmpty(
        Errors('20181017_case12',Patch('20181017_case12',Defaults('20181017_case12'),{First_x0020_Name:"Teresa3"}))
    ),
    Notify("Patch Successfully",NotificationType.Success),
    Notify("There is an error with your Patch function",NotificationType.Error)
)

Note: The '20181017_case12' represents the SP list data source within my app.

If I don't provide a value for the Title column which is a required field in my SP list, the error notification would show up as below:Test1.gif

If I provide a value for the Title column, the success notification shows up as below:Test1.gif

On your side, you should type the following formula:

 

If(
    IsEmpty(
        Errors(
           '[dbo].[TableName]',
            Patch('[dbo].[TableName]',Defaults('[dbo].[TableName]'),{Column1: GUID(),CREATED_DATE: Now(),CREATED_BY: User().FullName})
       )
    ),
    Notify("Patch Successfully",NotificationType.Success),
    Notify("There is an error with your Patch function",NotificationType.Error)
)

More details about the Errors function, please check the following article:

Errors 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

Hi @v-xida-msft,

 

Thank you for the response. This is all that I needed and the logic works perfectly.

 

And thank you for explaining everything in detail with example. 🙂

Hi @tommyly,

 

Thank you for taking out time and helping me out. The response from @v-xida-msft is a more appropriate approach for the app I am building.

 

Thank you once again for the response.

Hi @v-xida-msft,

 

I was able to implement the approach you have suggested. I have another question though.

 

Can we validate if a Gateway is working fine when the form is loaded?

 

Currently, we use a gateway to fetch data from the SQL server. OnStart event of the screen, we have used Lookup() and Filter() function, to fetch the required data from the tables.If the Gateway is Offline/Not working, we would like to navigate the user to an ErrorScreen. Errors() funciton is not able to check if the gateway is Offline/Not working.

 

Any ideas to work around this? 😄

Anonymous
Not applicable

Why would or should anyone need to do this, can it not save data reliably

joaoOliveiraUlh
New Member

My Solution!!

 

//Step1 - Set the variable with data of last id of your database!
Set(
varIdBefore;
Last(yourBd).ID
);;
//Step2 - Use the patch function!
Patch();;
//Step3 - Set the variable with data of last id of your database after your patch!
Set(
varIdAfter;
Last(yourBd).ID
);;
//Step4 - use the IF / ELSE function. If bb is greater than aa your data is saved!
If(
varIdAfter > varIdBefore;
Notify("This item change");
Notify("This item not change")
)

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 (2,356)