Salesforce - Developer - Data Types

salesforce-developer

salesforce-developer-data-types-global-picklist

// Salesforce - Developer - Apex - Data Types:

You can define custom fields, either to extend the functionality of a standard 
object, or when creating new custom objects. All fields in an object must be 
defined as a particular data type. Here's a summary of many of the supported 
data types:

1. Auto Number—a system-generated read-only sequence number, analogous to the 
   SQL identity type. These fields can be used to provide a unique ID that is 
   independent of the internal object ID. These fields are not used in 
   creating object relationships.

2. Checkbox—for representing Boolean data.

3. Date or Date/Time—for representing dates or date and time combinations.

4. Number—for representing real numbers, with optional decimal points.

5. Email, Phone and URL—format-validated email, phone and URL string representations.

6. Picklist and Multi-Select Picklists—represent values from a list.

7. Text and Text Areas—for representing text of various lengths.

8. Currency—a formatted number type, with optional multi-currency support.

9. Formula—a read-only field holding data generated from a formula expression.

10. Geolocation—allows you to identify locations by their latitude and longitude 
   and calculate distances between locations.

11. sObject types that represent persistent objects

12. Collections

13. Enumerations

14. User and system defined Apex classes.

The basic Text, Auto Number and Number are found on many relational databases. 
The Formula field type is somewhat different to the others. Instead of holding 
a value, it derives its value from a supplied formula expression. The field is 
then updated whenever any of the source fields are changed. Checkbox fields, 
Email fields, URL fields and Phone fields include some automatic formatting 
capabilities when displayed as part of the automatically generated user 
interface.

Primitive data types include:

1. Blob: for storing binary data

2. Boolean

3. Decimal: for representing arbitrary precision numbers, including currency.

4. ID: the Force.com database record identifier type

5. Integer, Long, Double, and String
// Salesforce Developer - Apex - Data Type - Date / Time

DateTime dt = System.now() + 1;
// Salesforce Developer - Apex - Data Type - Decimal:

Decimal d = Decimal.valueOf('123');
// Salesforce Developer - Apex - Data Types - Set:

A set is an unordered collection of primitives that does not contain any 
duplicate elements:

Set<String> s = new Set<String>{'a', 'b', 'c'};
s.add('c');
System.assert(s.contains('b'));
System.assert(s.size() == 3);
// Salesforce Developer - Apex - Data Types - List:

A list is a collection of elements.  Use a list when the sequence is important.  
Unlike Set, we can have duplicates in a list.

List<Integer> myList =  new List<Integer>();
myList.add(47);
myList.get(0);

We can also use the array syntax for lists.  For example:

String[] colors = new List<String>();
colors[3] = 'Green'
// Salesforce Developer - Apex - Data Types - Map:

Maps are collections of key-value pairs, and support a shortcut syntax for 
populating the collection:

Map<String,String> m = new Map<String, String>{'a' => 'b', 'c' => 'd'};
Map<ID,Contact> m = new Map<ID, Contact>([select id, lastname from contact]);

The last statement populates the map with retrieved contact objects, which in 
turn only have their ID and lastname fields populated.
// Salesforce Developer - Apex - Data Types - Enum:

Apex also support enumerations.

public enum Season { 
  WINTER, SPRING, SUMMER, AUTUMN 
}

Season s = Season.AUTUMN;
// Salesforce Developer - Apex - Data Types - Array:

Integer[] ints = new Integer[]{1,2,3,4,5,6,7,8,9,10};

for (Integer i : ints) {
  System.debug(i);
}
// Salesforce Developer - Apex - Loops:

Integer[] ints = new Integer[]{1,2,3,4,5,6,7,8,9,10};

for (Integer i : ints) {
  System.debug(i);
}

String s = 'Acme';
for (Account a : [select id, name from account where name like : (s+'%')]) {
  ...
}
// Salesforce Developer - Apex - Exception Handling:

try {
  throw new Exception();
} catch (ListException e) {
  // List Exception handling code goes here.
} catch (Exception e) {
  // Generic exception handling code goes here.
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License