Wednesday, 15 August 2018

Apex Basics

Apex is a strongly typed,object -oriented programming language that allows developers
to execute flow and transaction control statements on the Force.com platform.

Apex enables developers to add business logic to most system events,including button clicks,
related record updates and visualforce pages.

Data Types :
============
1.primitives
2.sObjects
3.Collections

Apex primitive data types include :

1.Boolean : can only be assigned true,false or null
2.Decimal : A number that includes a decimal points.
3.Double : A 64-bit number that includes a decimal point
4.ID : Any Valid salesforce.com ID
5.Integer : A 32bit number that does not include a decimal point.
6.Long : A 64-bit number that does not include a decimal point.
7.String : Any set of characters surrounded by single quotes.
8.Date : A value that indicates a particular day.
9.DateTime : A value that indicates a particular day and time
10.Time : A value that indicates a particular time.

sObjects :
=============
the term sObject refers to any object that can be stored in the Force.com platform database.

An sObject variable represents a row of data and can only be declared in Apex.

ex :
Contact c= new Contact(FirstName='raj';LastName='kiran';
Email ='test@gmail.com');

Account a =new Account(Name='TCS');
insert a;

Account a1=[select Name from Account
where ID='857598jh'];
a1.Name='new  Name';
update a1;

Collection :
============
collection of primitives or sObjects.

A LIST is an ordered collection.so use
list when you wnat to identify list element
based on INDEX NUMBER.
List can contain duplicates.

List<string> colors =new List<string>('red','purple','green');

List<Account> accts=[select Name,Type from Account
where Industry='Energy'];

List<Contact> email_contacts=
[select FirstName,LastName from Contact where
Email!=Null];

1. Set
2. List
3. Map

A set is a collection of unique,unordered elements.it can contain primitive data types or sObjects.

A set is an unordered collection of primitives or sObjects that do not contain any duplicate elements.so use SET if you want to make sure that your collection should not conatin Duplicates.

ex :
Set<String> s = new Set<String>();
Set<String> s = new Set<String>{'Jon', 'Quinton', 'Reid'};

commonly you'll see developers construct a set of IDs from a query,trigger context,etc.
and the use it as part of WHERE clause in their SOQL query.

ex :

Set<ID> ids = new Set<ID>{'0017000000cBlbwAAC','0017000000cBXCWAA4'};
List<Account> accounts = [Select Name From Account Where Id = :ids];


Map :
=====
A MAP is a collection of key-value pairs where each unique key maps to a single value.
Keys can be any primitive data type,while values can be a primitive,sObject,Collection
types or an Apex Object.

Use a map when you want to quickly find something by a key.Each key must be unique
but you can have duplicate values in your MAP.

Map<Integer, String> m = new Map<Integer, String>{5 => 'Jon', 6 => 'Quinton', 1 => 'Reid'};
Map<ID, Set<String>> m = new Map<ID, Set<String>>();
// creates a map where the key is the ID of the record
Map<Id,Account> aMap = new Map<Id, Account>([Select Id, Name From Account LIMIT 2]);


No comments:

Post a Comment