Tag Archives: SOQL

SOQL query in javascript

We can use SOQL in java-script on Visual Force pages or we can get it executed on click of a button or link present on detail page of a record. Below is the simple example of SOQL query in javascript on click of a button:
Javascript code:

{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}
try{
var query = "SELECT Id,Name from Contact LIMIT 1";
var result = sforce.connection.query(query);
var arrayResult = result.getArray('records'); 
 
if(arrayResult.length == 0){
alert('There is no Contact');
}
else{
var contactName = arrayResult[0].Name;
alert(contactName);
}
}
catch(e){
alert('An error has occured');
}

Salesforce Tips & Tricks

  • If you wanted to query ‘accountId, account.name’ fields in a query just specify ‘account.name’ which will retrieve ‘accountId’ automatically.
  • SOQL query can be ORDER BY 32 fields.
  • SOQL/SOSL statements cannot exceed 10,000 characters.
  • SOQL query can’t run more than 120 seconds.
  • For best performance, SOQL queries must be selective, particularly for queries inside of triggers. To avoid long execution times, non-selective SOQL queries may be terminated by the system. Developers will receive an error message when a non-selective query in a trigger executes against an object that contains more than 100,000 records. To avoid this error, ensure that the query is selective.
  • The maximum number of records that an event report returns for a user who is not a system administrator is 20000; for system administrators is 100000.
  • Inbound Email Services: Maximum Number of Email Messages Processed (Includes limit for On-Demand Email-to-Case) is number of user licenses multiplied by 1000, up to a daily maximum of 1,000,000.
  • Inbound  Email Services: Maximum Size of Email Message (Body and Attachments) is 10 MB.
  • Apex Limit: Maximum number of characters for a class is 1 million.
  • Apex Limit: Maximum number of characters for a trigger is 1 million.
  • Apex Limit: Maximum amount of code used by all Apex code in an organization1 is 3 MB.
  • Apex Limit: Default timeout of callouts (HTTP requests or Web services calls) in a transaction is 10 seconds.
  • Apex Limit: Maximum size of callout request or response (HTTP request or Web services call) is 3 MB.
  • Apex Limit: Maximum SOQL query run time before the transaction can be canceled by Salesforce is 120 seconds.
  • Apex Limit: Maximum number of class and trigger code units in a deployment of Apex is 5,000.
  • Apex Limit: For loop list batch size is 200.

Get Map Result From SOQL Query in Salesforce

//Creating Map with account id as key and account record as value
Map<Id,Account> accountMap = new Map<Id,Account>([SELECT Id, Name, Site, Rating, AccountNumber From Account LIMIT 100]);
 
//Getting list of account using map.values method
List<account> accList = accountMap.values();
 
//Getting set of account Id's using map.keySet method
Set<Id> accIdSet = accountMap.keySet();
 
//Getting list of account Id's using list.addAll method
List<Id> accIdList.addAll(accIdSet);

Salesforce Like Operator in SOQL Query

Like Operator in SOQL act as a Comparison operator for String Field Expression. Expression is true if the value in the specified fieldName matches the characters of the text string in the specified value. The LIKE operator in SOQL and SOSL is similar to the LIKE operator in SQL; it provides a mechanism for matching partial text strings and includes support for wildcards.

Use Cases Of LIKE Operator:

  • The LIKE operator is supported for string fields only.
  • The % and _ wildcards are supported for the LIKE operator.
  • The % wildcard matches zero or more characters.
  • The _ wildcard matches exactly one character.
  • The text string in the specified value must be enclosed in single quotes.
  • The LIKE operator is supported for string fields only.
  • The LIKE operator performs a case-insensitive match, unlike the case-sensitive matching in SQL.
  • The LIKE operator in SOQL and SOSL supports escaping of special characters % or _.
  • Don’t use the backslash character in a search except to escape a special character.

Example1:
The following query matches Appleton, Apple, and Appl, but not Bappl:

SELECT Id, Name
FROM Account
WHERE Name LIKE 'appl%'

Example2:
The following query matches Appleton, Apple, and Bappl, but not Appl:

SELECT Id, Name
FROM Account
WHERE Name LIKE 'appl_%'

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';
}