Friday, 18 October 2013

Simplifying the way to understand Dynamic Apex in Salesforce: Part - 3


Keeping the momentum with the previous two blog-posts Part - 1 and Part - 2 , here I would like to cover the way to write Dynamic SOQL/SOSL/DML.

Creation of SOQL/SOSL/SObjects at the runtime with Apex code is referred to as Dynamic SOQL/SOSL/DML which can be used to create more flexible applications like creating a search based on end user's input.

DYNAMIC SOQL

Database query methods are used to create a dynamic SOQL.

Example:

                  Return a single sObject when query comes back with one record
                   sObject s = Database.query(query_string_limit_1);

                  Return a list of sObjects when query comes back with more than one record
                   List listSObject = Database.query(query_string);

Points to remember(SOQL)

  • We can use simple bind variables in dynamic SOQL query strings.
          String sTestString = 'Test Name';          
          List listSObject = Database.query('Select Id from MyCustomObject__c where Name =: sTestString');

  • Unlike inline SOQL, Dynamic SOQL can't use bind variable fields in the query string.

          MyCustomObject__c oCustomObject = new MyCustomObject__c(field1__c = 'TestField');
          List listSObject = Database.query('Select Id from MyCustomObject__c  where field1__c =: oCustomObject.field1__c'); 


          MyCustomObject__c oCustomObject = new MyCustomObject__c(field1__c = 'TestField');
          String myField = oCustomObject.field1__c;
          List listSObject = Database.query('Select Id from MyCustomObject__c  where field1__c =' + myField'); 


DYNAMIC SOSL

Search query  methods are used to create a dynamic SOSL.

Example:

          List> myQuery = search.query(SOSL_search_string);

          String searchquery = 'FIND \'Edge*\' IN ALL FIELDS RETURNING Account(id,name),Contact, Lead';
          List> searchList = search.query(searchquery);


Points to remember(SOSL)

  • Dynamic SOSL statements evaluate to a list of lists of sObjects, where each list contains the search results for a particular sObject type.
  • The result lists are always returned in the same order as they were specified in the dynamic SOSL query.
  • From the example above, the results from Account are first, then Contact, then Lead.


DYNAMIC DML

We can also create sObjects dynamically and insert them into the database using DML.

Example

//Get a new Account
Account A = new Account();
//Get the token for that Account
Schema.sObjectType tokenA = oAccount.getSObjectType();
// Error because the token is a generic object, not ac Account
Account B = tokenA.newSObject(); 
//Correct way
Account B = (Account) tokenA.newSObject(); 


public class DynamicSObjectCreation{

     public static SObject createObject(String sObjectTypeName){
          Schema.SObjectType objectType = Schema.getGlobalDescribe().get(sObjectTypeName);
          if(objectType == null){
              // throw an exception
          }
         // instantiate an SObject with the type passed in as an argument at run time
          return objectType.newSObject();
     }
}

Points to remember(DML)

  • To create a new sObject of a given type, use the newSObject() method on an sObject token.
  • The token must be cast into a concrete sObject type (such as Account).

No comments:

Post a Comment

Thank you for visiting. Your comments are highly appreciated.