Tuesday, September 4, 2018

JTD-DesignerSeries-4-ApexBeginner


What is Apex
Salesforce Classic CRM Apps are architected using MVC frameworks, and delivered in a cloud computing based SAAS model as oppose to on-premise apps. Java, based on Object Oriented Principles & being platform neutral, open-source, was hugely supported by Java EE & Spring community, from 90s to 2000s & even today. Though Force.com platform multitenant design requires a language that is hosted & complied on the platform, support efficient utilization of resources by sticking to governor limits & integrated with Data layer through sObjects to support ease of use. Then, Salesforce engineering team designed Apex Code as first multitenant, on-demand programming language for developers interested in building next generation business applications. So, Apex is strongly typed object oriented language & follows Java like syntax and integrates with other features & services provided by the platform to build next generation lightning cloud applications.

Apex Execution Engine
Apex is compiled, stored & run entirely on the lightning platform. Apex code is compiled and stored as metadata that is interpreted by Apex Runtime Interpreter. Categorically, the runtime behavior of Apex Execution resonates more with scripting languages like Javascript, Ruby and quite different from Java where compiler generates the .class files.
Developers uses tools like Developer Console, Force.com IDE to write, debug and test the apex code.

Apex Language Support
Apex language support is quite similar to other object oriented languages by providing data types, collections [List, Set, Map], Control Flows [While, For Loops] & supports Abstraction, Encapsulation by providing Classes, Objects & Interfaces.

Unlike Java EE / or Spring framework which uses loosely coupled modules & ORM tooling to manage the database objects, Lightning Platform is tightly integrated with its data layer through sObjects. Apex developers create sObject variable for custom & standard objects, and collection like List, Map are used heavily to implement programming constructs in the form of Apex classes & methods.

DML statements [insert, update, delete] & SOQL queries helps query & manipulate object records and bulk queries are used to avoid hitting governor limits on the multitenant platform.
Invoking Apex
Salesforce ecosystem supports multiple delivery personas, from BA [no code], Admin [low code], Developers [code]. Solution that addresses requirements with code push developers to write Apex code, but implementation can span across multiple layers and apex component can be part of it. Few ways to invoke apex component:

a) Apex Triggers enable programmatic control before & after changes to Salesforce records like insertions, updates, deletions.

b) Asynchronous Apex enable capture the event data & behavior, spawn another thread for long running methods & jobs and provide an acknowledgement to calling thread.

c) Apex supports inbound and outbound integration scenarios with Apex Integration services. Apex runtime framework provide WSDL2Apex utility & Httprequest classes to make outbound callouts. Apex classes can be exposed as SOAP Services (webservice) and Restful resources (@RestResource) to support inbound callout from external applications.

d) Custom UI behavior can be implemented with Visualforce Pages integrated with controller layer using custom controller & extensions written as apex classes.
Testing Apex
Salesforce Deployments require 70% code coverage when the release artifacts have apex components. These days, best practices like DRY, Test Driven Development are necessary to support frequent releases & agile approaches. It is quite important to test bulk actions to ensure that runaway apex code don't monopolize resources.

Testing framework supports unit test with classes annotated with @isTest & test setup methods with @testSetup.
Links & References
a) Apex Developer Guide
b) https://trailhead.salesforce.com/en/modules/apex_database
c) https://trailhead.salesforce.com/en/modules/apex_testing
d) https://trailhead.salesforce.com/en/modules/apex_integration_services
e) https://trailhead.salesforce.com/en/modules/apex_triggers

No comments:

Post a Comment