Author - StudySection
Post Views- 127 views

Salesforce Apex Programming

What is Apex?
Apex is an object-oriented programming language designed for Salesforce cloud platform. The syntax of the apex is like java which is also an object-oriented programming language. Apex has built-in functions or procedures for operations like INSERT, UPDATE, DELETE and also DML Exception handling. It uses a direct reference to schema objects like sObject.

Apex is part of the salesforce releases and updates in the Salesforce cloud platform automatically with every new Salesforce release.

Some of the important apex applications are:
1.) Create web service and integrate with external systems.
2.) Email services.
3.) Create custom transactional logic.
4.) Perform complex validation over multiple objects at the same time and also custom validation implementation.

Apex works with SOSL and SOQL query languages that are designed specifically to work in the Salesforce cloud platform.

Apex SOSL :
SOSL refers to the Salesforce Object Search Language. It has the capability of searching a particular string across multiple objects. Some points on SOSL:
1.) Many objects can be searched at a time.
2.) It can be used in classes but not in the trigger.
3.) DML operations can not be performed on searched results.
4.) It can only query EMAIL, text and phone type of fields.
5.) It returns the fields and not the object.

Apex SOQL :
SOQL refers to the Structured Object Query language. It is used to perform DML operations like INSERT, UPDATE, DELETE, UPSERT, etc. Some points on SOQL:
1.) Only one object can be searched at a time.
2.) It can be used in classes and triggers and can query all type of fields.
3.) DML operations can only be performed on query results.
4.) Returns records and not the fields.

Apex classes can be executed from so many ways in the salesforce:
1.) Scheduling the apex class.
2.) Using triggers or process builder.
3.) Using web services.
4.) Using SOAP or REST API.
5.) Using an anonymous block.
6.) Asynchronous apex or invoking javascript in vf page.

Apex triggers:
Apex triggers are like stored procedures which execute when a particular event occurs. A trigger executes before and after an event occurs on record. Events on which triggers can be executed:
1. Insert
2. Update
3. Delete
4. Upsert
5. Undelete
6. merge

The syntax of apex programming language:

// Define a class in salesforce
public class createAccount 
{

  //Define a function in apex
  public void create() 
  {
    Account account = new Account();
    account.Name = ‘test’;

    // Save the data in account object using Database.insert function
    Database.SaveResult accountResult = Database.insert(account, false);

    // Iterate through each returned result
    if (accountResult.isSuccess()) 
    {
      // Operation was successful, so get the ID of the record that was processed
      System.debug('Successfully created account. ID: ' + accountResult.getId());
    } else 
    {
      // Operation failed, so get all errors                
      for (Database.Error err: accountResult.getErrors()) 
      {
        System.debug('The following error has occurred.');
        System.debug(err.getStatusCode() + ': ' + err.getMessage());
        System.debug('Account fields that affected this error: ' + err.getFields());
      }
    }
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *