Inner Query in Query Locator Bad Practice

Recently came across one code which had inner query in QueryLocator. For example some one has put that in Start method of Batch apex.

To give exact example of Query in Start Method of Batch,

I think this is not the right way of doing query.

To get into the reason why, i think this Link will help you.

In a SOQL query with parent-child relationship sub-queries, each parent-child relationship counts as an additional query.

The row counts from these relationship queries contribute to the row counts of the overall code execution.

In addition to static SOQL statements, calls to the following methods count against the number of SOQL statements issued in a request

  • Database.countQuery
  • Database.getQueryLocator
  • Database.query

For example your org contain  only 2 Account and each have 10 Contacts. 

If you execute this with sub Query like below

Select Id, Name, (Select Id, Name from Contacts) From Account

Then the total number of Query rows will be 22 (2+10+10).

From above analogy we can understand like this, If your org contain 40M accounts and each have 1 contact. Then in this scenario you can use sub query up to 25M only.

Like this Select Id, Name, (Select Id, Name from Contacts) From Account limit 25M

 

How to get KeyPrefix of sObject in Salesforce

Every record in Salesforce has an ID which is used to uniquely identify the record. Each ID value is globally unique. The first three digits represent the object type.

For example, the Account object uses the 001 prefix; the Note object uses the 002 prefix, Contact uses 003, Users 005, Opportunity 006 etc.

We can get these prefix values dynamically as well using Apex. To do this, use the below Apex code–

The key prefixes for all the standard objects remain the same.

You can use the above Apex code to get the key prefix of your custom objects. To do this just replace Account with the name of your custom object to obtain the key prefix of that custom object.

Wrapper class and Pagination in Salesforce

This is most commonly used concept in apex.

What is the need for wrapper class?

It is Inner class which consist of various different properties. For example, if you want to have single container which holds various types of variables or Sobject or Collection.

Second Example, Let’s say we want to showcase different opportunity along with Radio button or Checkbox and then by selecting those specific records you want to execute some logic on to this records.

For example we have list of opportunities along with checkbox,

Demo Site available here.

wrapperclass

Apex Class

Visualforce Page:

 

Action Poller in Visualfoce Page

According to SF documentation, It is

A timer that sends an AJAX request to the server according to a time interval that you specify. Each request can result in a full or partial page update.

Examples :

  1. After certain amount of time, you want to perform specific event.
  2. From Apex if you want to get continuous status of batch and then you want to execute specific task.
  3. If you want to split transactions for doing multiple activities.

Document from salesforce is here.

Let’s understand this by below example.

We will print the Booking Status. This sample page will poll the server for getting current status while displaying the specific message “Booking Your Tickets…” when action poller executes.

When the status changed to Booked we want to terminate the action poller.

Live demo available here.

Apex Class :

Visualforce Page :

 

Dynamic Styling in Visualforce Page

This post is about how dynamically you can display the colour of the field on Visual force page.

Here is the example below which will showcase all the opportunities in red which are of amount < 100000 and rest would be in Green.

You can find the live demo here.

Conditional Styling