Unleash your superpowers using Dynamic Programming with Apex - Part 1

Dynamic programming with apex? I know what you are thinking :

  • Come on Krishna, don't try to fool us, it is just a fancy expression to express something pretty simple
  • Dynamic with Apex?! Never heard of that!
  • What is the point of anyway? If I already know what I am doing with which objects, what is the point of learning that
  • ... (Please fill this area)

I know, I know. But still, I really feel that it actually is an awesome subject. I will give you some good insights of what you can do with it, and we will explore one specific action : DML on dynamic Sobjects.

What is dynamic programming with Apex?

When you are doing dynamic programming, you are not relying on anything set in stone. You are programming in a way where whatever functionality you are making, it could work with a variety of object without making multitude of " if statement" on the type of objects. I am a bit oversimplifying, but let's understand the concept before digging deeper.

Salesforce represents data in form of Objects. We can compare that a little bit with classic tables from your favorite SQL engine, but Salesforce objects are a bit more profound and capable than that. You have different types of fields, relationship fields, even formula fields, that helps representing the data.

Those objects types are :

  • Standard Objects : Any objet that are "Out of the Box" from Salesforce, or coming from their own managed package. Example : Account, Contact, Order, Lead, Cases etc
  • Custom Objects : Any object that YOU or your peers have created to customized the model. Example : Shoes, Stores, Entrances etc

When coding with apex, we can reference those objects by directing referencing them with their respective API names.

For example for a Standard Object :

List<Account> accounts = [SELECT Id FROM Account WHERE type = 'Customer'];

This will give you all the accounts of type Customer.

Example for Custom Object

List<Shoe__c> shoes = [SELECT Id from Shoe__c where Brand__c = 'KrishNad'];

This will give us all the shoes with the brand set as Krishnad(duh!). Notice the __c suffix  that is essential for referencing a custom object or custom field.

Now let's say we just want an object by its Id. Whatever the type it is an Account object or a Shoe object... In a classical way we would just do :

Account account =  [SELECT Id from Account where id = '**ACCOUNT_ID**'];
Shoe__c shoe = [SELECT Id from Shoe__c where id = '**SHOE_ID**'];

What if i want to only execute on query without specifying the type of object I am working on ? Let's use our magic trick : Dynamic Query

String objectType = 'Account';
Id idToQuery = '****ID_OF_OBJ****';
Sobject obj = Database.query('SELECT Id FROM' + objectType + ' WHERE ID = ' + IdToQuery);

As you see, to execute a dynamic query, we need to use the Database class with query method. Also, it stores it Sobject type variable.

Pretty straight forward right? Let's go into details on the next Part