Sunday 7 July 2019

Related reference Fields Value in sObject SOQL


In Dynamic SOQL we create query string using Apex code at run time and use Database.Query method to fetch the results.

example: 
String queryString = ' SELECT Id, Name, AccountId FROM Contact LIMIT 1';
sObject result = Database.query(queryString);

above query will return me a contact information, if I wanted to access record's Name I can access it using sObject get() method i.e. result.get('Name') will give me Name value.




Output:


But the real challenge we will face when you try to access fields value from related object field, i.e. if you try to access Account.AnnaualRevenue you will get an exception 'System.SObjectException: Invalid field Account.AnnualRevenue for Contact'.

To fix this we we will use getSObject() method to traverse the relationship.

below code will traverse the relationship and get you the value:

References: sObject Class, String Class, Martin Borthiry

Asynchronous Apex Triggers

In Summer 19 release Salesforce introduce a new feature to process data using a combination of 'Asynchronous Apex Triggers' and Change Data Capture.
Any DML on Salesforce records causes platform to start a series of calculations, run business logic and database updates, all these are called as an Apex Transactions. You can reduce the Apex transaction time to complete the business logic and limit constraints by decoupling resource-intensive, non-transactional logic from database transaction and execute asynchronously. 

Asynchronous Apex triggers are change event triggers that run asynchronously after a database transaction is completed. They are ‘after-insert’ triggers and can be defined with the after insert keywords. They can be created the same way we create regular Apex triggers. You set the trigger to use a change event object instead of an sObject. The change event object name is suffixed with ChangeEvent. For example, Account change event object is named AccountChangeEvent. The change event object name for the custom object is suffixed with __ChangeEvent.

Before creating after-insert triggers make sure you have enable objects for Change Data Capture. If not you can enable it following below steps:

Setup > Integrations > Change Data Capture, Select your Object.



You can create from after-insert triggers in the Developer Console the same way you create an Apex object trigger or you can try below command in VS Code terminal.

sfdx force:apex:trigger:create- n OpportunityChangeAyncTrigger -s OpportunityChangeEvent -e 'after insert' -d 'force-app/main/default/triggers'



Testing Change Event Triggers

To test change event triggers, enable the generation of change event notifications for the test method.

Test.enableChangeDataCapture();

The Test.enableChangeDataCapture() method ensures that Apex tests can fire change event triggers regardless of the entities selected in Setup. This method doesn’t affect the Change Data Capture entity selections for the org.

After performing DML operations, deliver the event messages to the corresponding trigger with:

Test.getEventBus().deliver();

Alternatively, if you use the Test.startTest(), Test.stopTest() method block in your test, change event messages fire the associated trigger after Test.stopTest() executes.