Small things in Apex which we can achieve in a smart way! - Part 2
Hope you guys enjoyed reading Part1. Here we go!
Below mentioned are few things which you might have missed while coding. Happy reading!!.
Mixed Save Actions on sObject Lists:
While working in Trigger or an ApexClass, we generally prepare different lists of multiple object types and perform DML operations for each sObject list. For example, to update accounts, contacts & cases we prepare List<Account>, List<contact> & List<case>, then we update each list separately which consumes unnecessary DML operations.
Instead, we can simply put all the three lists into a single sObject list and perform the DML only once.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<sObject> sObjList = new List<sObject>(); | |
sObjList.addAll(contactList); | |
sObjList.addAll(accountList); | |
sObjList.addAll(caseList); | |
update sObjList; |
Data is committed on a chunk-by-chunk basis. Any Apex triggers related to the records in a chunk are invoked once per chunk. Each call can process up to 10 chunks. If the sObjects array contains more than 10 chunks, you must process the records in more than one call.
Note: You have to make sure that SObjList has to be properly populated, because if you populate the SObjList in the following way:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
account1, account2, contact1, contact2, contact3, account3, account4, contact4, contact5 |
Chunk 1 : account1, account2
Chunk 2 : contact1, contact2, contact3
Chunk 3 : account3, account4
Chunk 4 : contact4, contact5
Click here for official documentation.
Running Test classes:
Your production environment has so many applications. You have deployed new application to production. To test the code coverage of the classes related to your application, we usually:
- Go to Apex Test execution wizard.
- Manually, select the test classes one by one.
- Run tests.
Assigning RecordType :
Most of the developers might follow the below approaches for assigning a record type to a record:
- Query the record type from RecordType object or
- Use Schema getDescribe() calls.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//updating record type of account | |
Account acc = new Account(id='<recordid>', Recordtype = new RecordType(name='Trial Account')); | |
update acc; |
For grouping DML statements I'd recommend using the Unit Of Work pattern. It's built in to FinancialForce's framework.
ReplyDeleteAwesome...these are really helpful tricks. Thanks for sharing :)
ReplyDelete