Advanced_Best_Practices_for_Apex_1694758882
Advanced_Best_Practices_for_Apex_1694758882
Mohith Srivastava
@msrivastav13 | mohith.shrivastava@salesforce.com
Mohith Shrivastava
Lead Developer Advocate, Salesforce
Daniel Ballinger
Director of Product Management, Apex
Thank you
Forward Looking Statement
What is Apex
try {
Database.insert(new Ticket__c(name='DF23'), AccessLevel.User_mode);
Assert.fail();
} catch (SecurityException ex) {
Assert.isTrue(ex.getMessage().contains('Ticket__c'));
}
// Create a external credential input and add it to the list, we just need
// the developerName of an already created external credential
ConnectApi.ExternalCredentialInput externalCredentialInput = new
ConnectApi.ExternalCredentialInput();
externalCredentialInput.developerName = 'SampleCustomExternal';
externalCredentials.add(externalCredentialInput);
New in Summer 23: Create Named Credentials with
Apex
objectNames.sort();
CPU Time - 1,307 ms
return objectNames;
Heap Size - 80,000 bytes
}
Improve speed by caching static data using
Platform Cache
Example with session cache
Cache.OrgPartition orgPartition = Cache.Org.getPartition(
CACHE_PARTITION_NAME
);
if (orgPartition?.get('objectlistfromdescribe') != null) {
objectNames = (List<Options>) orgPartition.get(
CPU Time - 1,307 ms
'objectlistfromdescribe'
Heap Size - 80,000 bytes
);
} else {
// put it into cache for faster access
orgPartition.put(
'objectlistfromdescribe',
objectNamesViaDescribe,
CPU Time - 20 ms
300, Heap Size - 1,300 bytes
Cache.Visibility.ALL,
true
);
}
Split High Consuming CPU Process into multiple
transactions
Asynchronous process/ Platform events
if (mapAccountIdByContacts.containsKey(con.AccountId)) {
mapAccountIdByContacts.get(con.AccountId).add(con);
} else {
List<Contact> lstContacts = new List<Contact>();
lstContacts.add(con);
mapAccountIdByContacts.put(c.AccountId, lstContacts);
}
}
Code Bulkification
Design code modules that works on collection in a single transaction