Tuesday, 27 December 2022

Dynamically Pass Bind Variables to a SOQL Query (Spring 23)

 New Database.queryWithBinds, Database.getQueryLocatorWithBinds, and Database.countQueryWithBinds methods, the bind variables in the query are resolved from a Map parameter directly with a key rather than from Apex code variables.

ex :

Map<String, Object> acctBinds = new Map<String, Object>{'acctName' => 'Acme Corporation'};

List<Account> accts = Database.queryWithBinds('SELECT Id FROM Account WHERE Name = :acctName',  acctBinds,   AccessLevel.USER_MODE);


Wednesday, 14 December 2022

Salesforce Flow elements

 1 Create = 1 DML

1 GET = 1 SOQL


1 Update without filter criteria = 1 DML

1 Update with filter criteria = 1 DML + 1 SOQL


1 Delete without filter criteria = 1 DML

1 Delete with filter criteria = 1 DML + 1 SOQL


Statements above count towards flow limits, let's be mindful of them


PS: the criteria in update and delete is like using a get element in the update or the delete.

If you use filter criteria in Update Records or Delete Records is will use a query.

object and field level security check in Apex

 "With Sharing" does not enforce object level and field level by default.


But there are other ways in which you can enforce them.


Here are couple of options:


1. While using an SOQL use "WITH SECURITY_ENFORCED" keyword to enforce field- and object-level security checks to fields and objects referenced in SELECT.


2. In Apex code, you can use sObject describe result methods and field describe result methods that check current user's permission. Methods are isAccessible, isCreateable, isUpdateable and isDeletable.


3. Use the stripInaccessible method to enforce field- and object-level data protection.


Sunday, 11 December 2022

Apex CPU Time Limit

 Usually, all the developers came across this term while developing some complex logic in apex. Apex has System.LimitExceptions and "Apex CPU time limit exceeded" is one of them.


Apex CPU time limits:

1] Synchronous Limit - 10,000 milliseconds (10 Seconds)

2] Asynchronous Limit - 60,000 milliseconds (60 Seconds)


To resolve this exception we can follow below points:


1] Avoid Multiple automation on a Single Object

2] Trigger Framework

3] Avoid multiple Validation Rules

4] Using Map based query

5] Use Async Apex (Future, Batch, Queueable, Schedulable)

6] Aggregate SOQL usage

7] Avoid Nested For loop

8] Avoid using process builder

9] Use Return Early Pattern


What is not counted for Apex CPU Time Limit?


1] Database operations like database operation for DML, SOQL and SOSL.

2] Wait time for Apex Callout

3] Aggregate SOQL