PowerApps 中的 Errors 函式Errors function in PowerApps

提供先前變更資料來源的錯誤資訊。Provides error information for previous changes to a data source.

概觀Overview

資料來源的記錄變更時,就會發生 Errors。Errors can happen when a record of a data source is changed. 許多原因都有可能,包括網路中斷、權限不足,以及編輯衝突。Many causes are possible, including network outages, inadequate permissions, and edit conflicts.

Patch 函式和其他資料函式不會直接傳回錯誤。The Patch function and other data functions don't directly return errors. 相反地,它們會傳回其作業的結果。Instead they return the result of their operation. 資料函式執行之後,您可以使用 Errors 函式來取得任何錯誤的詳細資料。After a data function executes, you can use the Errors function to obtain the details of any errors. 您可以使用 IsEmpty( Errors ( ... ) ) 公式中的 [IsEmpty] 函式來檢查是否有錯誤You can check for the existence of errors with the [IsEmpty] function in the formula IsEmpty( Errors ( ... ) ).

您可以使用 ValidateDataSourceInfo 函式,在錯誤發生之前予以避免。You can avoid some errors before they happen by using the Validate and DataSourceInfo functions. 如需更多有關如何使用和避免錯誤的建議,請參閱使用資料來源See working with data sources for more suggestions on how to work with and avoid errors.

說明Description

Errors 函式會傳回錯誤的資料表,當中包含下列資料行The Errors function returns a table of errors that contains the following columns:

  • 記錄Record. 發生錯誤之資料來源中的記錄。The record in the data source that had the error. 如果在記錄建立期間發生錯誤,此資料行將為空白。If the error occurred during the creation of a record, this column will be blank.
  • 資料行Column. 造成錯誤的資料行,如果錯誤可屬於單一資料行。The column that caused the error, if the error can be attributed to a single column. 如果不是,這會是空白。If not, this will be blank.
  • 訊息Message. 錯誤的描述。A description of the error. 可以針對使用者顯示此錯誤字串。This error string can be displayed for the end user. 請注意,此訊息可能會由資料來源產生且可能會很長,並包含對使用者可能沒有任何意義的未經處理資料行名稱。Be aware that this message may be generated by the data source and could be long and contain raw column names that may not have any meaning to the user.
  • 錯誤Error. 可以在公式中使用錯誤程式碼來協助您解決錯誤︰An error code that can be used in formulas to help resolve the error:
ErrorKindErrorKind 說明Description
ErrorKind.ConflictErrorKind.Conflict 會針對同一筆記錄進行變更,因而導致變更衝突。Another change was made to the same record, resulting in a change conflict. 使用 Refresh 函式以重新載入記錄,並重新嘗試變更。Use the Refresh function to reload the record and try the change again.
ErrorKind.ConstraintViolationErrorKind.ConstraintViolation 已違反一或多個條件約束。One or more constraints have been violated.
ErrorKind.CreatePermissionErrorKind.CreatePermission 嘗試建立一筆記錄,而目前使用者沒有建立記錄的權限。An attempt was made to create a record, and the current user doesn't have permission to create records.
ErrorKind.DeletePermissionErrorKind.DeletePermission 嘗試刪除一筆記錄,而目前使用者沒有刪除記錄的權限。An attempt was made to delete a record, and the current user doesn't have permission to delete records.
ErrorKind.EditPermissionErrorKind.EditPermission 嘗試編輯一筆記錄,而目前使用者沒有編輯記錄的權限。An attempt was made to edit a record, and the current user doesn't have permission to edit records.
ErrorKind.GeneratedValueErrorKind.GeneratedValue 嘗試變更資料來源所自動產生的資料行。An attempt was made to change a column that the data source generates automatically.
ErrorKind.MissingRequiredErrorKind.MissingRequired 記錄中遺漏必要資料行的值。The value for a required column is missing from the record.
ErrorKind.NoneErrorKind.None 沒有發生錯誤。There is no error.
ErrorKind.NotFoundErrorKind.NotFound 嘗試編輯或刪除記錄,但找不到記錄。An attempt was made to edit or delete a record, but the record couldn't be found. 另一位使用者可能已變更記錄。Another user may have changed the record.
ErrorKind.ReadOnlyValueErrorKind.ReadOnlyValue 嘗試變更唯讀的資料行。An attempt was made to change a column that's read only.
ErrorKind.SyncErrorKind.Sync 資料來源報告了錯誤。An error was reported by the data source. 如需詳細資訊,請檢查訊息資料行。Check the Message column for more information.
ErrorKind.UnknownErrorKind.Unknown 發生錯誤,但為未知的種類。There was an error, but of an unknown kind.
ErrorKind.ValidationErrorKind.Validation 偵測到一般的驗證問題,其不符合其他種類其中之一。There was a general validation issue detected, that did not fit one of the other kinds.

可能會傳回整個資料來源或只有選取之資料列的錯誤,方法為提供 Record 引數給函式。Errors can be returned for the entire data source, or for only a selected row by providing the Record argument to the function.

Patch 或另一個資料函式可能會傳回空白值,例如,若無法建立記錄。Patch or another data function may return a blank value if, for example, a record couldn't be created. 您可以傳遞空白至 Errors,它在這些情況下會傳回適當的錯誤資訊。You can pass blank to Errors, and it will return appropriate error information in these cases. 相同資料來源上之資料函式的後續使用將會清除此錯誤資訊。Subsequent use of data functions on the same data source will clear this error information.

如果沒有任何錯誤,Errors 傳回的資料表將為空白,且可以使用 IsEmpty 函式進行測試。If there are no errors, the table that Errors returns will be empty and can be tested with the IsEmpty function.

語法Syntax

Errors( DataSource [, Record ] )Errors( DataSource [, Record ] )

  • DataSource – 必要項目。DataSource – Required. 您要傳回錯誤的資料來源。The data source for which you want to return errors.
  • Record – 選擇性。Record – Optional. 您要傳回錯誤的特定記錄。A specific record for which you want to return errors. 如果您未指定這個引數,函式會傳回整個資料來源的錯誤。If you don't specify this argument, the function returns errors for the entire data source.

範例Examples

逐步解說Step by Step

針對此範例中,我們將使用 IceCream 資料來源︰For this example, we'll be working with the IceCream data source:

透過應用程式,使用者會將 Chocolate 記錄載入至資料輸入表單,然後將 Quantity 的值變更為 90。Through the app, a user loads the Chocolate record into a data-entry form and then changes the value of Quantity to 90. 要使用的記錄會放在內容變數 EditRecordThe record to be worked with is placed in the context variable EditRecord:

  • UpdateContext( { EditRecord: First( Filter( IceCream, Flavor = "Chocolate" ) ) } )UpdateContext( { EditRecord: First( Filter( IceCream, Flavor = "Chocolate" ) ) } )

若要在資料來源進行這項變更,會使用 Patch 函式︰To make this change in the data source, the Patch function is used:

  • Patch( IceCream, EditRecord, Gallery.Updates )Patch( IceCream, EditRecord, Gallery.Updates )

其中 Gallery.Updates 評估為 { Quantity: 90 },因為只有 Quantity 屬性已修改。where Gallery.Updates evaluates to { Quantity: 90 }, since only the Quantity property has been modified.

很不幸,就在叫用 Patch 函式之前,有其他人將 Chocolate 的 Quantity 修改為 80。Unfortunately, just before the Patch function was invoked, somebody else modifies the Quantity for Chocolate to 80. PowerApps 會偵測這個且不允許發生衝突的變更。PowerApps will detect this and not allow the conflicting change to occur. 您可以使用下列公式來檢查這種情況︰You can check for this situation with the formula:

  • IsEmpty( Errors( IceCream, EditRecord ) )IsEmpty( Errors( IceCream, EditRecord ) )

它會傳回 false,因為 Errors 函式會傳回下列資料表︰which returns false, because the Errors function returned the following table:

記錄Record 資料行Column 訊息Message 錯誤Error
{ Flavor: "Chocolate", Quantity: 100 }{ Flavor: "Chocolate", Quantity: 100 } 空白blank 「另一位使用者已修改您嘗試修改的記錄。"Another user has modified the record that you're trying to modify. 請重新載入記錄,然後再試一次。」Please reload the record and try again." ErrorKind.ConflictErrorKind.Conflict

您可以在表單上放置一個標籤,向使用者顯示此錯誤。You can place a label on the form to show this error to the user.

  • 若要顯示錯誤,將標籤的 Text 屬性設為此公式︰To show the error, set the label's Text property to this formula:
    Label.Text = First(Errors( IceCream, EditRecord )).MessageLabel.Text = First(Errors( IceCream, EditRecord )).Message

您也可以在表單中新增 [重新載入] 按鈕,讓使用者可以有效地解決衝突。You can also add a Reload button on the form, so that the user can efficiently resolve the conflict.

  • 若只要在發生衝突時才顯示按鈕,將按鈕的 Visible 屬性設為此公式︰To show the button only when a conflict has occurred, set the button's Visible property to this formula:
    !IsEmpty( Lookup( Errors( IceCream, EditRecord ), Error = ErrorKind.Conflict ) )!IsEmpty( Lookup( Errors( IceCream, EditRecord ), Error = ErrorKind.Conflict ) )
  • 若要還原使用者選取按鈕的變更,將其 OnSelect 屬性設為此公式︰To revert the change which the user selects the button, set its OnSelect property to this formula:
    ReloadButton.OnSelect = Revert( IceCream, EditRecord )ReloadButton.OnSelect = Revert( IceCream, EditRecord )