發表文章

目前顯示的是有「apex trigger」標籤的文章

這個欄位明明就存在啊,為什麼一直得到 error message: "Variable does not exist: ContactId"

圖片
情境 我在 Opportunity Trigger 裡寫了很簡單的 code,使用到 ContactId 這個 field。 for ( Opportunity opp : Trigger . new ){ if ( opp . ContactId != null ){           // Some actions } } 但是要從 VSC deploy 上 sandbox 的時候,被這個 error 給卡住: Variable does not exist: ContactId   原因 我這支 trigger 的 API version 太舊了,沒有支援到這個欄位。 解法 把 API version 升級到 46.0 以上即可。

如何在使用者儲存紀錄時,跳出客製化的訊息? How to pop up a customized message when user saves the record?

有的時候你會需要在使用者儲存時,跳出一些提示訊息。 通常是透過 validation 的 error message 或是 Apex Trigger 的 exception ( call addError() ) 來實現。 但上述兩種方法都會把使用者儲存的動作直接擋下來。 有的時候我們希望讓使用者在看到訊息的同時,仍然能夠成功地儲存 record。 目前我想到了兩種情境會遇上這個需求: 在使用者 update an opportunity 時,若是這個客戶是個特殊地區的客戶,需要提醒 Sales 留意我們的技術支援的允許範圍。 在使用者 update an opportunity 時,若是原定 auto close 的邏輯造成 triegger fail 了。則仍然讓使用者 update this record,並於畫面上跳出一個訊息顯示「已寄發通知給相關人士處理」 這個功能技術上完全可以實現,但我找不到現成簡便的實現方式。 直到我看到了這個: https://github.com/rahulmalhotra/Platform-Event-Toast-LWC 這個 LWC 小工具完全開放原始碼,你可以自己拿回去改。如果看不懂程式碼的,也完全可以隨裝即用,非常簡單。 他的概念就是透過 Process Builder (或是 Flow) 在 record 符合條件時,就觸發 insert an event record to an event object(BTW, event object的 api name 結尾是 __e 而不是 __c ) 然後再用一個 LWC 去訂閱這個 event,就可以透過簡單的 JavaScript 觸發跳出訊息框。 這邊也有圖文並茂的說明: https://www.sfdcstop.com/2020/05/how-to-display-warnings-in-salesforce.html 招數是死的,需求是活的。 不過有了這個基本範例打底,就可以變幻出千變萬幻的招數了!

Messaging.sendEmail not sending emails for non-admin profile in sandbox / 測試寄信,一直寄不出去,而且還沒有 error messsage!

總在自以為很嫺熟相關設定的時候,就會不小心又踩到一個坑。 這次是Messaging.sendEmail,怎麼也 send 不出去。 ## 症狀 我寫了一個 Apex Trigger on Knowledge__kav,讓 user 在 create new knowledge 的時候,會自動觸發通知信給相關人士。 我剛在 sandbox 做好的時候,自己測起來相當很正常、都有收到信,於是就聲稱說我把這個功能完成了。 結果別人怎麼都測不出來。 後來發現,好像只有 System Administrator 才能觸發寄送 Email 的動作。 我設了 Debug Log 也沒看到任何 error。 而non-admin 與 admin 所產生的 log,幾乎可說是一模一樣。 看不出到底寄信的動作在哪裡卡住了。 ## 原因 Root Cause 在 Sandbox,如果某個 user 的 Email 是 Invalid 的,那麼這個 user 就沒辦法觸發寄信動作。 包含 Workflow, Process, Flows or Triggers,通通都無法觸發。 這是 Summer Release 19' 上路的新制。望周知。 ## 參考資料 Messaging.sendEmail not sending emails for non-admin profile in sandbox Email addresses in sandbox appended with '.invalid' after refresh Please note as of Summer Release 19', Users must have a verified email address and .invalid removed from the email address in order to send email alerts via Workflow, Process, Flows or Triggers.

Syntax Reference 常用語法備忘: 如何使用SFDX Deploy / How to use SFDX to deploy Apex Code and Run Specific Test Class

Basic 基本語法 sfdx force:source:deploy -m ApexClass:MyClass -l RunSpecifiedTests  --runtests=MyClass_Test -w 3 -u PRD 同時 Deploy Apex Class and Apex Trigger sfdx force:source:deploy -m "ApexTrigger:MyTrigger,ApexClass:MyClass" -l RunSpecifiedTests  --runtests=MyClass_Test -w 3 -u PRD 同時 Deploy Apex Trigger 和多個 Apex Class sfdx force:source:deploy -m "ApexTrigger:MyTrigger,ApexClass:MyClass, ApexClass:MyClass_Test" -l RunSpecifiedTests  --runtests=MyClass_Test -w 3 -u PRD Note 這篇 有說到同一個 type 如果要 deploy 多個 item,comma 需要做 encode: Don’t: sfdx force:source:deploy -m "Profile:Standard User,Layout:Page,Console" Do: sfdx force:source:deploy -m "Profile:Standard User,Layout:Page%2CConsole" 但我試了還是不行……會出現 Error   MyClass%2C MyClass_Test    An object ' MyClass%2C MyClass_Test ' of type ApexClass was named in package.xml, but was not found in zipped directory 若有人知道我哪裡弄錯了,還請分享告知!