What happened to test classes?

I was working on a major deployment when I noticed that some of my existing test classes are failing in the sandbox. On some more research I found that it is because of Spring12. Since spring 12 has a new token for test classes, (SeeAllData=true), the default value sets to false. It means if you haven’t specified this your test classes can not query for the existing data. Though relying on existing data is a bad practice, I had a query for Profile object which caused the issue.

Get ready to change your test classes before Spring 12 otherwise your deployments will fail if you have relied on existing data.

 

Posted in APEX, salesforce | Tagged , , , | Leave a comment

Updating Multiple Objects in a Single DML statement

This is a very common situation that I am sure everyone would have faced it at least once where we have to update multiple objects at the same time. I faced a situation where I wanted to update a Contact and an Account right after.

Consider this:

Code:
Account acc = [Select Id from Account limit 1];
Contact con = [Select Id from Contact where AccountId = :acc.Id limit 1];

In order to update them we can do it in the following way:

Code:
con.LastName = 'Smith';
update con;
acc.Name = 'Updated Account';
update acc;

The above method includes two DML statements. These two could have been merged into one single DML statement as:

Code:
update new sObject[]{con, acc};

Simple! I tested it and the order of execution is the order of items added in the list/array. I found it very useful as it can save multiple DML statements.

I hope it helps!

Posted in APEX, salesforce | Tagged , | Leave a comment

Dev-501: Advanced Developer Exam

I cleared the dev501 exam and I will give you just one word advise: “Experience”. I am working on force.com since almost 3 years and this is the easiest exam I found. All of the people looking for sample questions for the exam, if you have 2 years experience on force.com, believe me you don’t need any sample questions.

Moreover, I found blog posts by Matt and Steve very useful.

Thanks

Posted in APEX, salesforce | Tagged , , | 6 Comments

Invalid Parameter Value

It happened with me quite a few times earlier when I tried to do any DML operation in a force.com site. At first I had no clue why is it happening but later I noticed the pattern as the page I was working on had several conditions. I noticed that whenever I have “id” as a query string parameter, it throws the “invalid parameter value” exception. When I renamed the query string parameter to something else, it worked like a charm.

All I can say is since Salesforce uses ID as a parameter value to identify the current record, it doesn’t allow to use ID as a custom query string parameter.

I hope it helps someone who is stuck in this issue.

Posted in APEX, salesforce, VisualForce | Tagged , , , , | 2 Comments

Covering exceptions in test methods

I try to use try/catch around every method I create in a class. But with many different methods the code coverage creates an issue when it gets difficult to reach 75%. I found a sneaky way of covering the exceptions in the methods.

I will write a code snippet to demonstrate how am I doing it.

Code:
public class myAccount
{
public static Boolean ThrowException = false;
public static void ProcessAccount()
{
try
{
Account acc = new Account (Name = 'John Smit');
if(!Test.isRunningTest())
insert acc;
acc.AccountNumber = '123123123';
if(!ThrowException)
update acc;
}
catch(Exception e)
{
///Exception handling
}
}
static testMethod void myTest()
{
myAccount.ThrowException = true;
myAccount.ProcessAccount();
}
}

//This will not insert the account and will try to update the account
//will throw an exception

I hope it helps.

Posted in APEX, salesforce | Tagged , , | Leave a comment

Debugging Apex using Developer Console

I just watched the video of Dreamforce session regarding debugging apex. The new break point debugging feature is simply awesome and it was a requirement for all #forcedotcom developer since long time. Though it’s not exactly the same as we have in Visual Studio but we need to understand the multitenancy of salesforce.com. A request cannot be paused on their servers. But still I consider it a great feature and would help me a lot in debugging my code.

Here is the video:

Posted in APEX, General, salesforce | Tagged , , , | Leave a comment

How to: Sort a list of sObject

Since Apex doesn’t allow to sort a list of sObject by default, developers face this issue while creating their awesome applications. I just wanted to share the code for what I did to achieve this for one of the issue I faced.

The class below will help you to implement this functionality

public static List<sObject> Sort(List<sObject> unSortedList, 
String sortField, String sortOrder){
      List<sObject> resultList = new List<sObject>();

      Map<object, List<sObject>> objectMap =
new Map<object, List<sObject>>();

      for(sObject sObj : unSortedList)
      {
        if(objectMap.get(sObj.get(sortField)) == null)
            objectMap.put(sObj.get(sortField), new List<Sobject>()); 

        objectMap.get(sObj.get(sortField)).add(sObj);
       }       

       List<object> keys = new List<object>(objectMap.keySet());
       keys.sort();

       for(object key : keys){
           resultList.addAll(objectMap.get(key));
       }

       List<sObject> sortedList = unsortedList;
       sortedList.clear();
       if(sortOrder.toLowerCase() == 'asc')
       {
           for(Sobject sObj : resultList)
               sortedList.add(sObj);
       }
       else if(sortOrder.toLowerCase() == 'desc')
       {
           for(integer i = resultList.size()-1; i >= 0; i--)
               sortedList.add(resultList[i]);
       }

       return sortedList;
}

I hope it helps

Posted in APEX, salesforce, VisualForce | Tagged , , , , | Leave a comment

Redirection on Visualforce Page Load

Folks,

I am writing after a long time since I came back from holidays. I was working on a page when I came across an unusual scenario. I placed a logic in a method to conditionally redirect to a different page. This method was called on page load in <apex:page> with “action” attribute. I found out that we cannot redirect to a different page on page load unless we set PageReference.setRedirect(true).

Since the controller of current and target page was same, I had to put data in query string to retain the session. I am sure this is a common issue and will help others.

Feel free to ask questions

Posted in salesforce, VisualForce | Tagged , , | 3 Comments

Increasing Heap Size Available To Eclipse

I was refreshing static resources in eclipse when I encountered an error. The error occurred because of the heavy files uploaded to salesforce.com organization on which I was working on. On doing some more research on the issue I found that certain amount of heap space is allocated to eclipse and since the data I was retrieving was large, it exceeded the space.

I searched on the eclipse official website and found a solution. When opening eclipse from Start->Run, we can specify certain arguments which will cause the heap size to increase

Code:
"Path to eclipse folder"/eclipse.exe -vmargs -Xmx256M

This will allocate 256M of heap size to eclipse. This is can also be changed in the eclipse.ini file which is available in the eclipse folder.

An example of the modified eclipse.ini file:

Code:
 -startup
plugins/org.eclipse.equinox.launcher_1.0.100
.v20080501.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.gtk.linux.x86_64_1.0.
100.v20080428-1330
-showsplash
org.eclipse.platform
-vm
/usr/lib/jvm/java-1.5.0-sun/jre/bin/java
-vmargs
-Xms512m
-Xmx1024m
-XX:+UseParallelGC
-XX:PermSize=256M
-XX:MaxPermSize=512M

I hope it helps.

Posted in General | Tagged , , , | 1 Comment

Using WS-Security enabled Web Services in Apex

There are Ws-Security based Web Services which are traditional web services but with security enabled in it. According to wikipedia

WS-Security (Web Services Security, short WSS) is a flexible and feature-rich extension to SOAP to apply security to web services. It is a member of the WS-* family of web service specifications and was published by OASIS. The protocol specifies how integrity and confidentiality can be enforced on messages and allows the communication of various security token formats, such as SAMLKerberos, and X.509. Its main focus is the use of XML Signature and XML Encryption to provide end-to-end security.

It allows secure transfer of data and ensures confidentiality. There is a firewall which always allows authorized requesters to create a SOAP request.

WS-Security Firewall



 

 

 

We don’t have to type XML based signatures in the SOAP request. More information can be found here and here.

Since salesforce.com doesn’t support WS-Security based WSDL now, the parsed proxy class will not work and the requester will receive an error “Invalid Security”. To overcome this issue I found a very useful post in force.com community. The current solution is to tweak the proxy class a bit so that the SOAP request that is being created, also includes the security header. The post can be found here.

I hope it helps. Feel free to ask any question.

 

Posted in APEX, salesforce | Tagged , , , , , | 12 Comments