All about Salesforce Apex at one place – Coder in Me

Introduction to Apex

Apex is a strongly typed, object-oriented programming language used in Salesforce. According to Salesforce Syntax of Apex  that looks like Java and acts like database stored procedures, Apex enables developers to add business logic to most system events, including button clicks, related record updates, and Visualforce pages. Apex code can be initiated by Web service requests and from triggers on objects.

Datatype of Apex

Primitive Type : Integer , Double, Decimal, Date, Datetime, boolean, String, Id, Time,Long, Object

Integer a=1, it can maximum store  32-bit with no decimal point

Long a=1, it can maximum store  64-bit with no decimal point

Double  d = 2.3 , it can take float or decimal point upto 64 bit

Decimal , it’s float and currency type of salesforce and number type of salesforce will be assigned to this variable.

Date : date dt = Date.newInstance(2019,08,27);

              Date.newInstance(Integer year, Integer month, Integer day) ;

DatetimeDatetime dt = Datetime.newInstance(2019,08,27);   Datetime.newInstance(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer second)

String :  Any text, text area, id can be stored in string

Boolean , we know to take data from checkbox, or flag handling true or false

Time it will store time only.

What is a Collection?

A Collection is set of any data type ,similar objects or things.
In Salesforce there are 4 types of Collection.but we will discuss here 3 types only.

  1. List: Collection of elements in an ordered way like an array, it can store duplicate elements.
  2. Set: Collection of unique elements in a unordered way.
  3. Map: Map is key – value pairs.

Every data type also represents object in salesforce, we can implements our logic based on their methods.

For example we have multiple methods in string like LEFT, RIGHT, REPLACE,isAlpha, length, remove, reverse,trim, replace, etc.

We have to do some practice on that. like

String str = 'Welcome to Coder In Me';

Now if we want first 4 characters

String str = 'Welcome to Coder In Me';
string frst = str.left(4);

Output will look like this

 

apex

similarly apex has very good methods to change or use date and datetime methods.

 

Data types Related methods
String Length()
Left(integer)
Right(integer)
Replace(‘string a’,’string b’)
trim()
isAlpha()
isNumeric()
contains(‘string’)
indexOf()
Date date dt = date.newInstance(year, month, day)
date dt =date.parse(’12/27/2009′)
dt.day()
dt.month()
dt.year()
dt.toStartofWeek();
firstDate.daysBetween(secondDate);
firstDate.monthsBetween(secondDate);
date.today();
dt.toStartOfMonth();
dt.addDays(integer value);
dt.addMonths(integer value);
dt.addYears(integer );
Here dt is date variable and Date is used as Date.methodname
Datetime Datetimedt = date.newInstance(year, month, day)
Datetime dt =date.parse(’12/27/2009′)
dt.day()
dt.month()
dt.year()
system.now();
dt.getTime();
dt.addDays(integer value);
dt.addMonths(integer value);
dt.addYears(integer );
dt.addHours(integer value);
dt.addMinutes(integer value);
dt.addSeconds(integer );
dt.format(‘string’);
apex support all the formats click here to check
Here dt is datetime variable

To learn about Collection, Pelase check List and set of collection in Apex and  Learn Map in apex

If, else if

if( Condition ){

   <Logic>

}else if ( Other Condition ){

    <Logic>

}else{

     <Logic>

}

Iteration( Loop )

for

( initialization; Condition; Increment ){

<Logic>

}

for(i=0; i<5; i++){
    system.debug(i);
}    

List<String> strList= new List<String>();
strList.add('abc');
strList.add('saif');
strList.add('abc1');
For(string str: strList){
 system.debug(str);
}

 

 

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.