Category: dynamic apex

10 Apex Programming Best Practices

Apex is a strongly-typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Lightning Platform. It is a key component of the Lightning Platform and allows developers to build robust business logic and integrations. There are a number of best practices that Apex developers should follow to ensure their code is efficient, maintainable, and scalable. Below are ten tips for writing high-quality Apex code. You can read – Lightning Web Component Best Practices Salesforce Flow Best Practices 10 Apex Programming Best Practices Use of "sObject" Data Type Use the “sObject” data type when...

Read More

Dynamic Apex – Build Dynamic SOQL and SOSL

In this post, I will explain another very powerful feature of Dynamic Apex i.e. Dynamic SOQL and SOSL.   Dynamic SOQL: Dynamic SOQL refers to the ability of creating SOQL statement in runtime with Apex code i.e. you can select which fields you want to query based on situation or user input and then prepare the SOQL statement and finally perform the query to fetch the data from the organisation. You can execute the below statement to perform the query: Dynamic SOQL can return concrete sObjects i.e. Account, Lead or MyCustomObject__c or the generic sObject data type. At runtime, system will match the return type with the variable’s declared type. If the types are not matching, then runtime will be thrown. So it is the responsibility of the developer to cast from generic sObject data type to a concrete sObject. In terms of governor limits, Dynamic SOQL queries have the same governor limits as static queries. Below example is going to fetch the fields from Account sObject, prepare the SOQL statement and then finally query Account sObject to fetch the data. Dynamic SOQL Consideration: In Dynamic SOQL, you cannot use bind variable strings in a query string. For example, the below section of code will not work and will throw an error that Variable doesn’t exist. To solve that you need to resolve the variable into a String variable and...

Read More

Dynamic Apex – Playing with CRUD and FLS

Security is one of the important things in today’s world. And when we talk about cloud, Security is the top most priority items. No doubt. Salesforce is not an exception and they are also considering Security really seriously.In Salesforce, we have different types of Security controls – starting from org level security to object level, followed by record level and field level. This post focuses on methods for controlling data at the object and field level.As always I always believe explaining concept with examples is the best and effective way. Today also I will do the same thing.For this post, I have prepared a small object called Book and below is the detail -Important Note -In Visualforce, when you use any sObject or sObject field, the platform will enforce CRUD and FLS automatically. If the user is not having FLS to a field, Salesforce will automatically remove the field from the Visualforce page. For example, apex:inputField tag will be rendered as read-only for those fields having FLS set as read-only. But the exception here are the tags apex:inputText, apex:inputTextArea. Using these tags tells Visualforce to not treat them as sObject field, thus platform will not automatically enforce FLS for those  fields. Apex Code to check CRUD manually:Requirement:Write a Visualforce page with controller behind which should display all Books present in the org in the form of Picklist. When we will execute...

Read More

Dynamic Apex – Sky is The Limit – Explained with Example

Today I am going to explain Dynamic Apex with examples. Dynamic Apex will help developer to create more flexible and robust application because with the help of Dynamic Apex, developer can access the sObject and field describe information, access Salesforce application information. Another great help is that with Dynamic Apex, developer can now write dynamic SOQL, SOSL and DML statements. So basically with dynamic apex, you have the magic wand in your hand.So without any further delay, let’s start the power of magic wand.Basics of Apex Describe:In order to get information about sObjects in Salesforce, you have two options – either use tokens or use describeSObjects Schema methods.Apex provides two data structures and a method for fetching information about sObjects and fields. They are listed below -Token – This is a lightweight, serializable reference to an sObject or a field that is validated at run time. Since they are lightweight, using them make your code faster and more efficient.The describeSObjects method – This method from the Schema class will perform the describe on sObject Types.Describe result – This is basically the object of  type Schema.DescribeSObjectResult which holds the result i.e. contains all the describe properties for the sObject or field. These objects are not serializable and validated always at runtime. Let’s get into more details before we jump into the examples -Schema Describe -Schema Describe is a way to programmatically...

Read More
Loading