Posts

Showing posts with the label clone

Salesforce Fact #753 | Save one for loop using clone() method

Image
Suppose we need to find out which accounts have related contacts or no related contacts in the apex code.  Now as per the usual logic, we would fetch the data from contact object based on the AccountIds which would give us the count of accounts having related contacts. And after that, we would run another loop to iterate over the master account list and check which one does not have any related contact. Well, we can use the clone() method of Set class which would help us to save one loop. Attached is the screenshot. Note: The SOQL and list lengths are only for demonstration purpose. As per the best practice, SOQLs need to be as much as selective with proper access checks to retrieve only the intended records.

Salesforce Fact #739 | Clone with related

While using the 'Clone with Related' option, the related records are cloned in batches of 200. Validation rules, workflow rules and apex triggers are applied separately for each batch. Also, the selected related records can't be more than 999. Reference:  https://help.salesforce.com/s/articleView?id=sf.basics_clone_records.htm&type=5