Tuesday, 25 March 2014

Reserved Keywords in Salesforce

Total Number of  Reserved Keywords-130

    abstract
    global
    private
    activate*
    goto*
    protected
    and
    group*
    public
    any*
    having*
    retrieve*
    array
    hint*
    return
    as
    if
    returning*
    asc
    implements
    rollback
    autonomous*
    import*
    savepoint
    begin*
    inner*
    search*
    bigdecimal*
    insert
    select
    blob
    instanceof
    set
    break
    interface
    short*
    bulk
    into*
    sort
    by
    int
    stat*
    byte*
    join*
    super
    case*
    last_90_days
    switch*
    cast*
    last_month
    synchronized*
    catch
    last_n_days
    system
    char*
    last_week
    testmethod
    class
    like
    then*
    collect*
    limit
    this
    commit
    list
    this_month*
    const*
    long
    this_week
    continue
    loop*
    throw
    convertcurrency
    map
    today
    decimal
    merge
    tolabel
    default*
    new
    tomorrow
    delete
    next_90_days
    transaction*
    desc
    next_month
    trigger
    do
    next_n_days
    true
    else
    next_week
    try
    end*
    not
    type*
    enum
    null
    undelete
    exception
    nulls
    update
    exit*
    number*
    upsert
    export*
    object*
    using
    extends
    of*
    virtual
    false
    on
    webservice
    final
    or
    when*
    finally
    outer*
    where
    float*
    override
    while
    for
    package
    yesterday
    from
    parallel*

    future
    pragma*


Note:
               Keywords marked with an asterisk (*) are reserved for future use.

The following are special types of keywords that aren't reserved words and can be used as identifiers.
  • after
  • before
  • count
  • excludes
  • first
  • includes
  • last
  • order
  • sharing
  • with

Salesforce : APEX : Class ( Simple/Regular Vs Abstract Vs Virtual )

Note : simple/regular means, class without abstract and virtual keyword

Before going into differences between these, understand regular, abstract, virtual methods :
Regular Method  : Has code body or code implementation  and cannot be overridden

Virtual Method    : Has code body or code implementation and can be overridden by extending class method using override keyword

Abstract Method : Doesn't have body or code implementation and can be overridden by extending class method using override keyword



Regular Class:
a) Can be constructed ( can create object ).
b) Can not be extended
c) Can have regular methods
d) Can have virtual methods
e) Can not have abstract methods

Virtual Class:
a) Can be constructed ( can create object ).
b) Can be extended
c) Can have regular methods
d) Can have virtual methods
e) Can not have abstract methods

Abstract Class:
a) Can not be constructed ( can not create object ).
b) Can be extended
c) Can have regular methods
d) Can have virtual methods
e) Can have abstract methods

Note:
i) Extending class can not reduce visibility of super method by overriding it.
ii)Extending class can not change return type of super method by overriding it.
iii)Extending can do method over loading e.g.

Super class has 
public void getName(){
return 'My Name'; 
}

and Extending class can have
public getName(String str){
return str; 
}

iv) class can't extend two classes but can extend one class (virtual/abstract) and implements multiple interfaces.  

Saturday, 15 March 2014

Attempt to de-reference a null object salesforce

Thought of posting about a very common error we come across in apex. You must have seen this a number of times.
Visualforce Error: System.NullPointerException: Attempt to de-reference a null object

This error occurs whenever you try to access value/values from a variable/collection whose new new instance has not been created or is null in other words.

for example: if you have a list of accounts (List<account> accList) defined and you have not created a new instance of this list. And then, accidentally you try to access record from this list; then you will get the above error.

As can be seen from this example:

Visualforce Page


?
1
2
3
4
5
<apex:page controller="DeferenceDemoController">
 <apex:form >
   <apex:commandButton value="Call Method" action="{!Demo_method}"/>
 </apex:form>
</apex:page>

Controller
?
1
2
3
4
5
6
7
8
9
10
Public with sharing class DeferenceDemoController {
List<account> accList;
   Public DeferenceDemoController(){
     
   }
  Public void Demo_method(){
   for(Account ac:accList )
       system.debug('****'+ac.name);
  
}




A list "accList" of accounts has been defined but a new instance of this list has not been created. So when we press the button "Call Method" we get the error "Attempt to deference a null object" as in the called method we are trying to access the "accList" list.

This error can be avoided by creating a new instance of the accList before trying to access it as below:

Modified controller:

?
1
2
3
4
5
6
7
8
9
10
Public with sharing class DeferenceDemoController {
List<account> accList;
   Public DeferenceDemoController(){
      accList =New List<account>();
    }
  Public void Demo_method(){
   for(Account ac:accList )
       system.debug('****'+ac.name);
  
}