Apex Programming

Apex Programming Tutorial:


Session 1: Introduction to Apex


https://www.youtube.com/watch?v=RwrA6Y7Bvf0&list=PL-gW8Fj5TGro_a8irvZUZUKJdEpWIGMB8&index=2


Apex is Object Oriented Programming language.


Use Apex when you want to:

1. Create Web Services like REST and SOAP Inbound and Outbound Web Services.

2. Email Services.

3. Triggers

4. Asynchronous Apex like Batch, Schedulable, Future, Queueable.

5. Lightning Server Controllers.

6. Test Classes.

7. Visualforce controllers.

8. Invokable methods.


First apex class:

-----------------

public class MyFirstApex {

}


To test the functionality of apex classes, we can use Anonymous Block in developer console.



Session 2: Data types in Apex:

A constructor is code that is invoked when an object is created from the class blueprint. You do not need to write a constructor for every class. Constructor are used to initialize the state of object,where as method is expose the behaviour of object.


sample class:

------------

public class DataTypeDemo {

// Constructor

public DataTypeDemo(){ 

System.debug('I am in constructor');  

}

}


Whenever class is called, constructor will be called automatically.


Call the class from Aonymous block:

----------------------------------


DataTypeDemo d = new DataTypeDemo();


Result after executing from anonymous block of developer cosnole: I am in constructor


Primitive Data types:

Boolean isActive = true;

Integer Num = 0;

Decimal Price = 1000.50;

String s = 'Hello World';

ID id = '001100000009r2xxyyyyzzzz'; // 15 or 18 digit id of salesforce record.


public class DataTypeDemo {

Boolean b; // primitive variable

//method

public void methodDemo(){

b = true;

System.debug('I am in method');

System.debug('b = '+b);

}

}


Execute from anonymous block:

DataTypeDemo d = new DataTypeDemo();

d.methodDemo();


Result: 

I am in method

b = true;


Specific sObject:


Examples:


Account acc = new Account();

Account acc = new Account(Name = 'Test Account');

Account acc = new Account();

acc.Name = 'Test';

acc.Phone = '99999999999';


CustomObject__c c = new CustomObject__c();


Sample class:

------------

public class DataTypeDemo {

Boolean b; // primitive variable

public void methodDemo(){ // method

Account acc = new Account();

acc.Name = 'Test';

acc.Phone = '1234567890';

System.debug('Account = ' +acc);

}

}


Result: Account = Account: {Name=Test, Phone=1234567890}


Generic sObject:


sObject sObj1 = new Account(Name = 'Test');

sObject sObj2 = new CustomObject__c(Name = 'Test');


Casting Generic sObject to Specific object.

Account acc = (Account)sObj1;


Note: One of the benefit of casting is to be able to access fields using dot notation, which is not available on the generic sObjects.

After casting only, we can use dot notation for generic objects.


The benefit of using sobject generic is we can refer them more than 1 specific object.


Sample class:

------------


public class DataTypeDemo {

Boolean b; // primitive variable

public void methodDemo(){ // method

sObject obj = new Account(Name = 'Test');

Account acc = (Account)obj;

obj = new Contact();

System.debug('Account = ' +acc);

}

}


Result:

Account = Account: {Name=Test}


Collections: List

List is an ordered collection of elements.

Each element of list has an index for identification.

Index position of first element is always '0' (zero).

List can be nested and even multidimensional.

Elements can be of any data type. (Primitive types, collections, sObjects, User defined types, Built-In types. Here User defined types, Built-In types are refer to classes.).


Example:


List<String> myList = new List<String>();

List<Account> accList = new List<Account>();

List<Integer> myList = new List<Integer>();


myList.add(20);

Integer i = myList.get(0);

myList.add(1, 30);

myList.clear();

Integer s = myList.size();

Boolean b = myList.isEmpty(); // returns true if List is empty.

List<Account> accList = [SELECT Id, Name FROM Account LIMIT 10];


public class DataTypeDemo {

Boolean b;

public void methodDemo(){

List<String> strList = new List<String>();

strList.add('A');

strList.add('B');

System.debug('List contains ... '+strList); // Result = List contains ... (A,B)

List<Account> accList = [SELECT Id, Name FROM Account LIMIT 2];

System.debug('accList = '+accList); //Result: (Account: {Id =011111, Name=Test}, Account:{Id=0222222, Name=Test2})

}

}



Collection: Set

An un-ordered collection of elements. 

It doesn't contain duplicate elements.

Sets can contain collections that can be nested within one another.

Elements can be of any data type: (Primitive types, collections, sobjects, user-defined types, built-in types.).


Example:

Set<String> stringSet = new Set<String>();

Set<Id> accIdSet = new Set<Id>();


Set<Integer> intSet = new Set<Integer>();

intSet.add(20);

intSet.add(30);

intSet.remove(20);


Boolean b = intSet.contains(30); // returns true if set contains 30

Integer s = intSet.size();

Boolean b = intSet.isEmpty(); // returns true if set is empty


Collection: Map


A Map is a collection of key-value pairs.

Keys are always unique having a value associated.

Values can be duplicate.

Keys and values can be of any data type: Primitive, Collection, sObjects, User defined types, Built-in types.

Example: 

Map<String, String> strToStrMap = new Map<String, String>();

Map<Integer, String> intTiStrMap = new Map<Integer, String>();

intToStrMap.put(1, 'First');

intToStrMap.put(2, 'Second');

Boolean b = intToStrMap.containsKey(1);

String value = intToStrMap.get(2);

Set<Integer> s =  intToStrMap.keySet();


public class DataTypeDemo {

Boolean b;

public void methodDemo(){

Map<String, String> demoMap = new Map<String, String>();

demoMap.put('1', 'First');

demoMap.put('2', 'Second');

System.debug('demoMap  = '+demoMap); // Result = {1=First, 2=Second}

}

Collection: Map

Example:

Map<ID, Account> idToAccountMap = new Map<ID, Account>();


List<Account> accList = [ SELECT ID, Name FROM Account];

Map<ID, Account> isToAccountMap = New Map<ID, Account>(accList);


Map<Account, List<Contact>> accToConMap = new Map<Account, List<Contact>>();


Enums:

Enum is an abstract data type.

Enums are used to define a set of possible values.

After creation of enum, one can create variables, methods args, and return types of that type.

Example:

public enum Weekdays {MON, TUE, WED, THU, FRI, SAT, SUN}

Weekdays W = Weekdays.MON;

if(w == Weekdays.TUE)

return w;

No comments:

Post a Comment