發表文章

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

Apex Solution for Splitting Orders Based on Order Line Items in Salesforce

圖片
When managing orders in Salesforce, we sometimes face the need to split orders based on their order line items. Recently, I encountered such a challenge: under a specific contract, there were 18 orders, each containing three order line items. The task was to split these orders so that each new order would contain only one order line item. The first step in this task was to query all the orders under a specific contract. Then, for each order, we needed to query its associated order line items. Initially, I considered directly modifying the OrderId field of the OrderItem records, but this is not allowed in Salesforce, as once an OrderItem is created, its OrderId cannot be changed. Therefore, I took a different approach: create a new order and a new order item record for each order line item. In this way, each new order would have only one associated order line item. This required associating the new order items with the new orders after inserting the new orders. Here is an example of...

在 Salesforce 中根據訂單項目拆分訂單的 Apex 解決方案

圖片
在處理 Salesforce 的訂單管理時,我們可能會遇到需要根據訂單項目(Order Line Items)拆分訂單的情況。最近,我遇到了這樣一個挑戰:在特定合同下有18張訂單,每個訂單包含三個訂單項目。任務是將這些訂單拆分,使每張新訂單只包含一個訂單項目。 這個任務的第一步是查詢特定合同下的所有訂單。然後,對於每個訂單,我們需要查詢其相關的訂單項目。最初,我考慮直接修改 OrderItem 記錄的 OrderId 字段,但是這在 Salesforce 中是不允許的,因為一旦 OrderItem 被創建,其 OrderId 就不能更改。 因此,我採取了不同的方法:為每個訂單項目創建一個新的訂單和一個新的訂單項目記錄。這樣,每個新的訂單都將只有一個與之關聯的訂單項目。這需要插入新訂單後,再將新訂單項目與新訂單關聯。 以下是實現此功能的 Apex 代碼示例:   public class SplitOrders {     public static void splitOrdersByLineItems(String contractId) {         // 查詢特定合同下的所有訂單         List<Order> originalOrders = [SELECT Id FROM Order WHERE ContractId = :contractId];         List<Order> newOrders = new List<Order>();         List<OrderItem> newOrderItems = new List<OrderItem>();         for (Order originalOrder : originalOrders) {             List<OrderItem> orderItems = [SELECT Id, ...

如何在 list view 一次刪除多筆 record / How to mass delete records from list view

圖片
要如何在 list view 選擇多筆 record 後一鍵刪除呢? 這個功能在 Salesforce 目前沒有內建。 你可以選擇下列幾種方式 安裝 APP 用 flow 來做 用 apex class 來做 安裝 App 有一個由 Salesforce Labs 釋出的 app ,名字很直白就叫做 Mass Delete 官方說明文字 A set of custom list buttons that you can add to your standard list views or related lists. Users can select any number of records and delete all of them with a single click. 優點 快速簡單,即裝即用。 缺點 僅支援下列幾個現成的 standard object,如果需要用在 custom object,需要進行一點客製修改。 支援 object:  Product Solution Contact Contract Lead Opportunity Account Asset Campaign Case 使用在 custom object 的修改教學參考: https://blog.cloudanalogy.com/salesforce-lightning-how-to-mass-delete-records-from-list-view/ 概念上來說就只要 clone 它的 Visualforce Page 然後改第一行的 controller 即可。 用 Flow 來做 教學影片: https://youtu.be/3cdj8QCtYvI 用 Apex Class 來做 https://muskanagarwal1499.blogspot.com/2022/06/mass-delete-records-using-list-button.html

[Apex] List all non-editable fields

When we want to customize a FieldHistory object, such as the Opportunity Field History , we would write a small program to check the modification status of all fields when the Opprotunity record is updated. However, some fields are unnecessary, or even should not be checked. For example, some system fields like CreateDate and IsDeleted. In concept, if a field is not Updateable, there is no need to check it. So how do we confirm if a field is Updateable or not? Here is a ready-made method: (Using Opportunity as an example): Opportunity . sObjectType . getDescribe (). fields . getMap (). get ( fieldName ). getDescribe (). isUpdateable () The plain translation of the above line is roughly as follows: Retrieve the field map of the Opportunity object, and then extract the properties of the specified field from the field map. One of the properties that can be checked is isUpdateable. 

[Apex] 列出所有不可編輯的欄位

當我們想要自己客製化一個 FieldHistory object,例如 Opporutunity Field History,我們就會寫一支小程式,在 Opprotunity reocrd 被更新的時候檢查 所有欄位 的異動狀態。 但其實有些欄位是不必要、甚至是不應該檢查的。 例如某些系統欄位:CreateDate、IsDeleted 之類的。 概念上來說,如果某個 field 不是 Updateable,就毋須檢查。 那如何確認某個 field 是不是 Updateable呢? 這裡有一個現成的方法可用: 以 Opportunity 為例: Opportunity . sObjectType . getDescribe (). fields . getMap (). get ( fieldName ). getDescribe (). isUpdateable () 上面這行的白話翻譯大致是∶把 Opportunity 這個 object 的 field map 抓出來之後,再從 field map 裡面提取指定 field 的屬性,其中一個可以檢查屬性就是 isUpdateable。

HttpRequest and Named Credential 的介紹與說明 from ChatGPT

圖片
最近在把 JWT 的 authorization 換成 Named Credential 的方式,自覺對於一些基本的知識點還不夠紮實,所以就請教了 ChatGPT,我覺得它真的講解得蠻好的。 以下節錄他的說明: 解釋範例程式 問: 請幫我解釋這段 Salesforce apex code: HttpRequest req = new HttpRequest(); req.setMethod('POST'); req.setHeader('Content-Type', 'application/json; charset=utf-8'); req.setHeader('Content-Length', String.valueOf(payLoad.length())); req.setHeader('Authorization', 'Bearer ' + jwtToken); req.setEndpoint(requestUrl); req.setBody(payLoad); 答: 這段 Apex 代碼主要在建立一個 HTTP 請求,並且設置請求的方法、標頭、終結點和請求正文。 具體來說: HttpRequest req = new HttpRequest(); 建立了一個 HttpRequest 對象,用於表示一個 HTTP 請求。 req.setMethod('POST'); 設置了請求的方法為 POST。 req.setHeader('Content-Type', 'application/json; charset=utf-8'); 設置了請求的 "Content-Type" 標頭為 "application/json; charset=utf-8"。 req.setHeader('Content-Length', String.valueOf(payLoad.length())); 設置了請求的 "Content-Length" 標頭為請求正文的長度...