Apex Data Types In SalesForce

About Salesforce

Salesforce is a cloud-based online solution for customer relationship management or CRM. Salesforce provides all of our departments like marketing, sales, commerce, and service with a shared view of our customers with a single integrated CRM platform.
Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on Salesforce servers in conjunction with calls to the API. Using syntax 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. Salesforce Apex Primitive datatypes are, Blob, Boolean, Date, DateTime, Decimal, Double, ID, Integer, Long, Object, String and Time.
Reading this article, you can learn and test the Apex primitive datatypes in SalesForce.
The prerequisites for testing Apex primitive datatypes in SalesForce as, Register a Salesforce Free Trial account using the following link.
Step 1
Log into your Salesforce Account and click the Developer Console.
Apex Data Types In SalesForce
Step 2
Now, we can create new Apex Class ApexDatatype and the file named as ApexDatatype.apxc,
Apex Data Types In SalesForce
After creating Apex class ApexDatatype,
Apex Data Types In SalesForce
Add a DTtest method for create and test primitive datatypes,
Apex Data Types In SalesForce
Step 3
The first primitive datatype is, Blob - A collection of binary data stored as a single object. For example,
  1. String tStr='CsharpCorner';  
  2. Blob myBlob=blob.valueOf(tStr);  
  3. String dStr=myBlob.toString();  
  4. System.debug('Blob Content is : '+ dStr); 
Next we have, Boolean data type- A value that can only be assigned true, false, or null.
  1. Boolean isRollno = true;  
  2. System.debug('Boolean Values is : '+ isRollno);  
Next we have, Date data type -A value that indicates a particular day. Unlike
Datetime values, Date values contain no information about time.
  1. Date todate = date.today();  
  2. System.debug('ToDate is : '+ todate);  
Next we have, DateTime data type - A value that indicates a particular day and time, such as a timestamp.
  1. Datetime fixdatetime=Datetime.now();  
  2. System.debug('fixdatetime is : '+ fixdatetime);  
Next we have, Decimal data type -A number that includes a decimal point. Decimal is an arbitrary precision number. Currency fields are automatically assigned the type Decimal,
  1. Decimal feeamount=154.70;  
  2. System.debug('FeeAmount(Decimal) is : '+ feeamount.abs());  
Next we have, Double data type -A 64-bit number that includes a decimal point.
  1. Double scient=154.70;  
  2. System.debug('scient(Double) is : '+ scient);  
Next we have, ID data type - Any valid 18-character Force.com record identifier.
  1. ID recordid='00400000003MKSBB01';  
  2. System.debug('recordid is : '+ recordid);  
Next we have, Integer data type - A 32-bit number that does not include a decimal point. Integers have a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647.
  1. Integer Mark = 95;  
  2. System.debug('Mark is '+ Mark);  
Next we have, Long data type - A 64-bit number that does not include a decimal point.
  1. Long RegNo = 52412548711L;  
  2. System.debug('RegNo is '+ RegNo);  
Next we have, String data type - Any set of characters surrounded by single quotes.
  1. String text = 'Welcome to CSharp Corner';  
  2. System.debug('text is '+ text);  
Finally we have, Time data type - A value that indicates a particular time.
  1. DateTime dt = Datetime.now();  
  2. Time myTime = Time.newInstance(dt.hour(), dt.minute(), dt.second(),  
  3. dt.millisecond());  
  4. System.debug('Time is '+ myTime);  
Finally, the ApexDatatype.apxc class code is,
  1. public class ApexDatatype {  
  2.     public static void DTtest() {  
  3.         //Blob  
  4.         String tStr = 'CsharpCorner';  
  5.         Blob myBlob = blob.valueOf(tStr);  
  6.         String dStr = myBlob.toString();  
  7.         System.debug('Blob Content is : ' + dStr);  
  8.         //Boolean  
  9.         Boolean isRollno = true;  
  10.         System.debug('Boolean Values is : ' + isRollno);  
  11.         //Date  
  12.         Date todate = date.today();  
  13.         System.debug('ToDate is : ' + todate);  
  14.         //DateTime  
  15.         Datetime fixdatetime = Datetime.now();  
  16.         System.debug('fixdatetime is : ' + fixdatetime);  
  17.         //Decimal  
  18.         Decimal feeamount = 154.70;  
  19.         System.debug('FeeAmount(Decimal) is : ' + feeamount.abs());  
  20.         //Double  
  21.         Double scient = 154.70;  
  22.         System.debug('scient(Double) is : ' + scient);  
  23.         //ID  
  24.         ID recordid = '00400000003MKSBB01';  
  25.         System.debug('recordid is : ' + recordid);  
  26.         //Integer  
  27.         Integer Mark = 95;  
  28.         System.debug('Mark is ' + Mark);  
  29.         //Long  
  30.         Long RegNo = 52412548711 L;  
  31.         System.debug('RegNo is ' + RegNo);  
  32.         //String  
  33.         String text = 'Welcome to CSharp Corner';  
  34.         System.debug('text is ' + text);  
  35.         //Time  
  36.         DateTime dt = Datetime.now();  
  37.         Time myTime = Time.newInstance(dt.hour(), dt.minute(), dt.second(), dt.millisecond());  
  38.         System.debug('Time is ' + myTime);  
  39.     }  
  40. }  
Step 4
For Debugging the Apex class ApexDatatype, Click Debug menu and Select Open Execute Anonymous Window,
Apex Data Types In SalesForce
Now, Write the Apex code for calling DTtest method, and enable the Open log option for Viewing output and click Execute,
Apex Data Types In SalesForce
Step 5
Now we can verify the output, After clicking Execute, Log will be open automatically and select Debug Only option,
Apex Data Types In SalesForce


Now, you have successfully tested the Apex primitive datatypes in the Salesforce environment.