26th October, 2018
In Apex all variables and expressions have a data type they are two types of data types
A primitive data types such as an Integer, Double, Long, Date, Date Time, String, ID, or Boolean.
Ex:- Initialize a Boolean Variable to False.
Apex Primitive data types include. Boolean: A value that can only be assigned true, false, or null.
Ex:- Boolean is Active=True;
Date: value that indicates a particular day. Date values contain no information about time. Date values must always be created with a system static method.
Ex:- Date myDate= Date.newinstance(2012, 05 , 28);
o/p is is 2012-05-28 00:00:00
Time and Date Time: These are date types associated with dates and times along with Date data type. The time data types stores times (hours, minutes, second and milliseconds). The Date data types stores dates (Year month and day). The Date time data type stores both dates and times. Each of these classes has a newInstance method with which we can construct particular date and time values.
EX:- Time mytime= newInstance(18,30,2,20);
O/P: is 18:30:02
Date myDatetime = Datetime.now(); Date today= Date.today();
EX:- Time t= Datetime.now().time();.
---> finally we can also manipulate the values by using a range of instance methods. EX:- Date myToday = Date.today(0; Date myNext30 = myToday.addDays(30) We will get something like this as the output. 2012-05-28 00:00:00 2012-06-29 00:00:00 Integer, Long, Double and Decimal: To store numeric values in variables, declare variables with one of the numeric data types. Integer,Long Double and Decimal. Integer: A 32-bit number that doesn't include a decimal point. Integers have a minimum value of -2, 147,483,648 and a maximum value of 2,147,483,647.
EX:- Interger i=1;
Long: A 64 bit number that doesn't include a decimal point longs have a minimum value of -263 and a maximum value of 263-1;
EX:- Long l= 2147483648L;
Double: A 64 bit number that doesn't include a decimal point longs have a minimum value of -263 and a maximum value of 263-1;
EX:- Long l= 2147483648L;
Decimal: A number that includes a decimal point. Decimal is an arbitrary precision number. Currency fields are automatically assigned the type decimal.
EX:- Decimal dec=19.23;
Note:- We can use the value of static method to cast a string to a numeric type. For example, the following creates an Integer from string ‘10’ and then adds 20 to it. EX:- Integer CountMe= Omteger.Valueof('10') +20; Null Variables:-If we declare a variable and don't initialize it with a value, it will be null. Null means the absence of a value. We can also assign null to any variable declared with a primitive type. Both of these statements result in a variable set to null;
String: Strings are set of characters and are enclosed in single quotes. They store text values such as a name or an address.
EX:- String myvariable= ' Capital info Solutions';
We can also create strings from the vales of other types, such as dates, by using the string static method value of(). EX:- Date myDate= Date today(); *** String mystring= String value of (myDate); The o/p of above example should be today's date 2012-05-29
At TekSlate, we offer resources that help you in learning various IT courses. We avail both written material and demo video tutorials. To gain in-depth knowledge and be on par with practical experience, then explore Salesforce Online Training.
An Sobject, can be a generic SObject or be a specific SObjects, such as an Account, Contact, or MYcustom__c.
EX:- Account a= new Account (); MyCustomObject__c co= new MYCusomobject__c (); API name of Custom Object.
Accessing SObject Fields: SObject fields can be accessed or changes with simple dot notation. EX:-Account a = new Account (); a.Name='Capital Info solutions'; Access the account name field and assign it 'Capital Info solutions'.
Accessing SObject Fields Through Relationships: Sobject records represent relationship to other words with two fields an ID and an address that points to a representation of the associated sobject. EX:- The contact sobject has both an AccountID field type ID and an Account field of type accounts that points to the associated sobject record itself. The ID field can be used to change the account with which the contact is associated, while the Sobject reference field can be used to access data from the account. The following Apex code shows how an account and contact can be associated with one other, and then how the contact can be used to modify a filed on the Account. EX:- Account a=new account (Name ='Osmania University') Insert a; Inserting the record automatically assigns a value to its ID field EX:- Contact c= new contact (Last name='Anil Reddy') C. Account ID=a.ID The new contact now points at the new account. Insert C; Anonymous Blocks: An anonymous block is Apex code that does not get stored in the meta data , but that can be compiled and executed using developer console. *We can use Anonymous Blocks to quickly evaluate Apex on the fly such as in the developer console. Note:
Your Name ---> Developer console --->Click on "click here to enter Apex code" and give the apex code and Click on “Execute" button ---> Select the particular Operation ---> Click on "Open Raw Log" button ---> Check the result in the USER-DEBUG statement EX:- 1. Interger i=10; System debug (i); 2. Integer i=10; Interger J=20; System debug (The sum of two numbers'+ (i+j)); For Concatenating two strings 3. String S1='Anil'; String s2='sateesh'; System.debug (‘names are’(s1+s2)); 4. Account a =new Account (); a.name ='capital Info solutions '; System.debug (a name);
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills .
Write For Us