Category Archives: Salesforce

SOQL For Loop

Biswajeet   March 19, 2014   No Comments on SOQL For Loop

The SOQL for loops iterate over ALL the sObjects returned by a SOQL query. Here is the syntax for the for loop in SOQL:

Option 1 – (Include the SOQL in the loop definition):

for (someVariables : [soql_query])
    //Your Code

Note: The variables above must be of the same type as the sObject that are returned by the soql_query. Here’s an example below of a simple for loop function that uses the clauses WHERE and LIKE:

String s = ‘Biswajeet Samal’;
For ( Lead a : [SELECT Id, Name from Lead where Name = : (s)])
    //Your Code

Option 2 – (Create a list of results (sObject list) first and then loop through them):

// Create a list of account records from a SOQL query
Account[] accs = [SELECT Id, Name FROM Account WHERE Name =  'Biswajeet Samal'];
// Loop through the list and update the Name field
for(Account a : accs){
   a.Name = 'Biswajeet Samal';


Introduction to Object Query Language (SOQL)

An introduction to Object Query Language (SOQL):

As a developer looking to extend, SOQL is very important and powerful aspect of coding. You can use SOQL to build your own custom query stings. These query strings can be used in the following places:

  • Apex statements
  • Visualforce getter methods and controllers
  • In the queryString param passed in the query() call
  • Finally, you can use the Schema Explorer in the Eclipse Toolkit

For those are familiar with SQL, will find some differences.

SOQL uses the “SELECT” statement combined with any filter statements to bring back sets of data. The data sets returned may be optionally ordered as well (just like in SQL).

Here is a basic SOQL Select example:

SELECT field1, field2, field3
FROM an object
WHERE filter statement(s) and (optionally) order the results

If you want to get all the names for opportunities created in the last 30 days.
you would use the following SOQL statement:

FROM Opportunity
WHERE CreatedDate = Last_N_Days:30

If you want to get all the Leads from  your account where the email address equals = “” you would use the following SOQL statement:

SELECT ID, Name from Lead WHERE email = ''


Getting the “Count” of results being returned in a SOQL data set is pretty simple as well.
For example, if I wanted to know how many Leads were going to be returned in my SELECT statement above, I can use the COUNT() function below:

SELECT COUNT() from Lead WHERE email = ''

SOQL – Comparison Operators:

Operator Common name
= Equals
!= Not equals
< Less than
<= Less than or equal
> Greater than
>= Greater than or equal
NOT IN Not in (WHERE clause)
INCLUDES EXCLUDES Applies to multi-select picklists
LIKE Like (see section below)

SOQL – Like Operator:

The LIKE operator provides a way to match partial text strings and includes support for wildcards. Let’s say for a moment we want to find all the Leads where the email domain is the same. For this, we can use a “LIKE” operator.  He is an example of a LIKE statement with the % wildcard.

The placement of the percent sign ‘%’ is key here. I am basically saying, bring me back all the Leads where the email ends with “”. Therefore I place the ‘%’ at the beginning of whatever I am looking for.” Anything to the left of the % sign is ignored in the search. If I didn’t know the full domain I could use the following statement:

SELECT Id, Name from Lead WHERE email  LIKE ''

This is going to return all the leads where the email contains “somecomp”.

SELECT Id, Name from Lead WHERE email  LIKE '%somecomp%'

Other wildcard is the underscore “_”. Thing is used to match exactly one character.
Note: Unlike with SQL, the LIKE operator in SOQL performs a case-insensitive match.


If you want to extend the WHERE clause to include multiple values, you can OR. See the example statement below:

SELECT ProductCode FROM PricebookEntry WHERE CurrencyIsoCode = 'USD' or CurrencyIsoCode = 'GBP'

Taking it a step further, you can evaludate multiple things in the WHERE clause:

SELECT ProductCode,UnitPrice FROM PricebookEntry
WHERE (UnitPrice >= 10 and CurrencyIsoCode='USD')
OR (UnitPrice >= 5.47 and CurrencyIsoCode='EUR')

How to deploy any trigger from sandbox to production?

In order to deploy a trigger from sandbox to production, you need to take care of some points:

  • At first I would like to request you all that before going for any deployment, make sure that there is no test failure or less than 75% code coverage in Production Instance. Because when we deploy any thing from sandbox to Production, “Run All Test” is performed automatically in Production. If there is any issue in Production then it will not let you deploy your new code.
  • If you are deploying any trigger in Production, then you must write a test class for that trigger with no Test failure and more than 75% code coverage.
  • Create deployment connection in sandbox.
  • Create Outbound change set in Sandbox and add your trigger and its associated test class in the change set.
  • Create Inbound change set in Production.
  • Add Change set components in Sandbox.
  • Upload the change set.
  • You can validate the change set in Production.
  • Finally deploy the change set in Production.

Hope this will help to deploy a trigger from sandbox to production.

Close popup and refresh parent window in Visualforce Page

Its a very common requirement to open a pop up by clicking a button . Once you are done with pop up, close the pop up and refresh the parent window so that user will be able to see the update on parent window at the same time.

Here is the sample code for it:

Command button in Visualforce Page:

<apex:commandbutton action="{!save}" id="button" oncomplete="javascript:CloseAndRefresh()" value="Update"/>

And here is the java script function:

<script language="JavaScript" type="text/javascript">
function CloseAndRefresh(){


Note: I would suggest to check this setting Setup-> Personal Setup  ->  My Personal Information -> Personal Information
Here check for “Development Mode”. I have seen at various places and at community as well that if this check box is checked then popup would not close. Here, I would suggest to turn this checkbox off while you are testing this.

What is Salesforce?


Today the world is leaded by Cloud computing. is one of the way for moving all your applications to cloud. There are lots of question running in your mind that what is Salesforce? What are its features? What are the benefits of moving apps to Salesforce? So to answer your questions, here is a small article which would help you find your answers. I have tried here to answer all these questions in a simple manner.

Salesforce is a SaaS (software-as-a-service), global enterprise software company Best known for its Customer Relationship Management (CRM), that is now also offering platform-as-a-service. Salesforce is a pioneer  in providing a high quality on-demand CRM software (referred to as “The Sales cloud” by Salesforce). They have diversified into customer service and support on-demand offerings as well (referred to as “The service cloud” by Salesforce).

Salesforce is a classic manifestation of two (SaaS and PaaS) out of the three major components of cloud computing.

Following is a list of products and services offered by Salesforce :

  • Customer Relationship Management (CRM) –  Their CRM is best in class in the on-demand category. With highly robust features and ease to customize, its one of the most adaptable CRM software in the market.
  • Platform – This is the platform-as-a-service offering of Salesforce.
  • Chatter – Think of chatter as a Facebook for enterprise! It facilitates real time collaboration at workplace bringing people, data, and content together in a secure, and trusted network.
  • AppExchange – AppExchange is a directory of applications built for Salesforce by third-party developers, which users can purchase and add to their Salesforce environment.
  • Customization – This is the core essence of Salesforce’s CRM application. User defined fields can be added and existing fields modified to better align the application with the in-use terminology within the company.
  • Web services – Salesforce offers a SOAP Web service API (An XML based method of sending and receiving information between applications via HTTP protocol) that enables integration with other systems.
  • Mobile support – One of the coolest features of Salesforce is the support for mobile devices, where some important functionality of its CRM can be accessed through mobile devices (currently the supported devices include iphone, Blackberry, Android and Windows based smartphones).
  • Programming Languages – Apex is a programming language that is syntactically similar to Java and is used in customization and implementation of applications.