Power Apps:自动发送运行错误邮件
Power Apps应用在运行时出现错误,会在屏幕显示红色弹窗,并显示出具体的错误消息,这样可以快速的定位到问题所在。
但有的时候,其他人在使用时,如果程序可以继续进行,可能会忽略了这个错误消息,这样就无法回溯这个错误。这样就需要每次在有错误提示的情况下,及时通知到开发人员。
实现步骤:
1、应用开发视图中,选择APP的OnError属性
2、粘贴命令
/* ======================================================================NOTE: Here are links to helpful documentation if you'd like to fully understand the following code and, perhaps, add to it. ======================================================================Topic: Error, IfError, IsError, IsBlankOrError functions: https://learn.microsoft.com/en-us/power-platform/power-fx/reference/function-iferror======================================================================Topic: Errors functionhttps://learn.microsoft.com/en-us/power-platform/power-fx/reference/function-errors ======================================================================Topic: Power Fx Error handlinghttps://learn.microsoft.com/en-us/power-platform/power-fx/error-handling // ================================================================ */ /* ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️NOTE: Please follow the instructions below to finalize the "installation" of this useful snippet of code to your Power Apps application. ➡️🔴 Please be sure to add the Office365Outlook connector! 🔴⬅️Any global variables created here to make this code to work should be moved to App.Formulas. ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️*//* ===================================================================VARIABLE 1️⃣ fxErrorHandlerEmail===================================================================USE: The email address you'd like to be notified of the errors that might occur in this application. If you'd like multiple developers to be emailed, include them all in the string and simply separatethem with semicolons as shown belowExample: "jon@domain.com;lisa@domain.com"STEP #1: Change the email address below to your email address or the person who will be responsible for supporting this application!STEP #2: Copy the code to App.Formulas ⬇️ fxErrorHandlerEmail = "YOUR_EMAIL_HERE"; STEP #3: Delete or comment out the use of the Set() function=================================================================== */Set(fxErrorHandlerEmail, ";" & User().Email); // Put your email address before the semicolon (;)// ==================================================================// /* ===================================================================VARIABLE 2️⃣ fxApplicationName==================================================================USE: The name of the application to be used to let the developer know which app the Error(s) came from. STEP #1: Change the name in the string below to reflect the actual name of this application. STEP #2: Copy the code to App.Formulas ⬇️fxApplicationName = "The Power Apps Application"; STEP #3: Delete or comment out the use of the Set() function=================================================================== */Set(fxApplicationName, "出入库应用功能测试");// ==================================================================// /* ===================================================================VARIABLE 3️⃣ fxApplicationURL==================================================================USE: The URL of this application. We use this to link backto this App. OPTION #1: If you'd like to link to the app to play it as a user would, go to https://make.powerapps.com/ and find this app listed there. Click on the 3 virtical dots, and click on "Details" to copy the app link from. Replace this: "https://apps.powerapps.com/"...with that URL. The URL will look something like this: https://apps.powerapps.com/play/e/12345efb-ce00-e9be-9b13-42284f521909/a/12345dd2-33c6-43c8-9d4e-96e6bd55b914?tenantId=12345526-320f-46bb-8838-a0626db4c5e2&sourcetime=9921603599236 OPTION #2: If you'd like to link to back here to Power Apps studio to easily edit the app, simply copy the URL you have listed above in your browser and Replace this: "https://apps.powerapps.com/"...with that URL. The URL will look something like this: https://make.powerapps.com/e/12345efb-ce00-e9be-9b13-55284f521909/canvas?referrer=AppsPage&action=edit&form-factor=tablet&app-id=%2Fproviders%2FMicrosoft.PowerApps%2Fapps%2F5fe123d2-33c6-43c8-9d4e-1236bd55b914==================================================================STEP #1: Pick one of the two options outlined above and updatethe link below. STEP #2: Copy the code to App.Formulas ⬇️fxApplicationURL = "https://apps.powerapps.com/"; STEP #3: Delete or comment out the use of the Set() function=================================================================== */Set(fxApplicationURL, "https://make.powerapps.com");// ==================================================================// /* ===================================================================VARIABLE 4️⃣ fxLightGrayColor==================================================================USE: This is simply a light gray color assigned to this variable to be used in the email's formatting. If you do change it, keep in mind this will be usedin CSS style for the HTML format of the email. STEP #1: Feel free to change it to whatever color you wish.STEP #2: Copy the code to App.Formulas ⬇️fxLightGrayColor = "#e5e5e5"; STEP #3: Delete or comment out the use of the Set() function=================================================================== */Set(fxLightGrayColor, "#e5e5e5");// ==================================================================// /* ===================================================================VARIABLE 5️⃣ colAppErrors (collection variable)==================================================================USE: This is a collection variable that will hold all theerrors that have occurred in this session for the user. DECIDE: No changes are needed for this collection variable*UNLESS* you'd like to store these in a data sourcewhich might prove advantageous. If you would like to implement this, then...STEP #1: Create a database table or SharePoint list. You may call it whatever makes sense to you, buta good name might be "PowerAppsErrors".This name is used in the example below. STEP #2: Create 9 fields that are single line of text, string, or nvarchar(255) for SQL Server. {Message: "",Source: "",Observed: "",HttpResponse: "",HttpStatusCode: "",TimeStamp: "", Screen: "", UserEmail: "",UsersName: ""}STEP #3: Add this data source to this project. STEP #4: At the bottom of this whole code block, patch your data source with the contents of this collection variable in one go://Patch(PowerAppsErrors, colAppErrors);Clear(colAppErrors);//=================================================================== *//* ===================================================================👍👍 The rest of this code may remain unmodified 👍👍=================================================================== */With({SubjectLine: Concatenate( "Error(s) occurred in the ", fxApplicationName, " application for ", User().FullName),HowManyErrors: CountRows(AllErrors) + CountRows(colAppErrors),LightGrayColorHexBG: "background-color:" & fxLightGrayColor & ";",ScreenName: App.ActiveScreen.Name,MyUsersName: User().FullName,MyUsersEmail: User().Email},Collect(colAppErrors,ForAll(AllErrors As My, {Kind: My.Kind,Message: Text(My.Message),Source: Text(My.Source),Observed: Text(My.Observed),HttpResponse: Text(My.Details.HttpResponse),HttpStatusCode: Text(My.Details.HttpStatusCode),TimeStamp: Text(Now(), "mm/dd/yyyy hh:mm:ss:ffff AM/PM"),Screen: ScreenName, UserEmail: MyUsersEmail,UsersName: MyUsersName}));// // The AllErrors collection is a global collection that is only // available in error contexts like this OnError event or inside an // IfError() function.//// Clear(AllErrors);// // Now it is time to send out an email notification to the developer(s)// Office365Outlook.SendEmailV2(// =============================================================// WHO TO SEND THE EMAIL TO// =============================================================fxErrorHandlerEmail, // =============================================================// SUBJECT OF EMAIL// =============================================================SubjectLine,// =============================================================// BODY // =============================================================$"<html><body><h3>Error Report for {MyUsersName} ({MyUsersEmail})</h3><table style='width:100%;'border='1' cellpadding='10' cellspacing='0'><tr style='{LightGrayColorHexBG} width:100%;'> <th>Time Stamp</th> <th>Screen Name</th> <th>Source</th><th>Error Message</th><th>Observed</th> <th>Http Response</th><th>Http Status Code</th></tr>" & Concat(colAppErrors, $"<tr><td>{TimeStamp}</td><td>{ScreenName}</td><td>{Source}</td><td>{Message}</td><td>{Observed}</td><td>{HttpResponse}</td><td>{HttpResponse}</td><td>{HttpStatusCode}</td></tr>") & $"<tr><td colspan='7' style='{LightGrayColorHexBG}'><div style=''>From Application: <a href='{fxApplicationURL}'>{fxApplicationName}</a></div></td></tr></table></html></body>"););///* ===================================================================As mentioned at the top of all this code, *IF* you'd like to store these in a data source, 🤔then uncomment out the two lines of code below. 👍=================================================================== *///Patch(PowerAppsErrors, colAppErrors); // ⬅️ //Clear(colAppErrors); // ⬅️ // ===================================================================//// END OF App.OnError 🔚//// ===================================================================
大部分是注释,可以删除。和原文内容相比,在下面邮件内容中,我修改了显示字段,因为在使用中我发现发送 的邮件内容表格有错误。
另外需要修改的地方:
Set(fxErrorHandlerEmail, ";" & User().Email); 输入你的邮箱
Set(fxApplicationName, "The Power Apps Application"); 你的应用名
Set(
fxApplicationURL,
"https://apps.powerapps.com/"
); 你的应用开发程序 地址
3、这样前期步骤就完成了,运行程序时如果 有错误,在关闭程序后,会发送邮件给你
里面包含了时间,发生错误时的屏幕, 错误组件,错误信息等。
工具地址:PowerFxSnippets/App.OnError/fxAppOnError.yaml at main · PowerAppsDarren/PowerFxSnippets