Method :

public List<Case> getCaseDetails() 

   List<Case> cd = new List<Case>(); 

     if(cd == null) cd = [SELECT Field1__c,Field2_Contact__c FROM Case WHERE Field1__c =:currentRecordId limit 1 ]; 

 return cd; 

While testing this method in Dev Console , it's returning empty List . Can you please help me in finding what is missing/wrong?

  • where you are setting value of currentRecordId in Dev console? – Himanshu Jul 28 '16 at 6:49
  • 2
    your cd list is never null as you have initialized it just above. so the if condition get failed and returns the empty list. Anyway I'm not seeing any benefit of checking for null or empty just after initializing the list as you know it's not null and it's empty – highfive Jul 28 '16 at 6:50
  • @Himanshu- In Dev Console I was just checking by removing the WHERE clause – SFDCRookie Jul 28 '16 at 7:05

SO when you initialize your list like

 List<Case> cd = new List<Case>(); 

means cd is not null. It is just an empty list

SO you need to check like

  cd = [SELECT Field1__c,Field2_Contact__c FROM Case WHERE Field1__c =:currentRecordId limit 1 ]; 

For the condition for null

If your list is initialize like

List<Case> cd; 

then you need to check like

if(cd == null)

Your if statement is testing to see if the list is null, and if it is, then it's assigning records from the query. However, you're initialising the list to an empty list, so it's not null, but an empty list, these are very different things.

You want to check for the current size, not to see if it's null:

if(cd.size() == 0) cd = [query];

An empty list is still a list, it's an instantiated object that exists, it just doesn't contain anything. If you declared cd like so:

List<Case> cd;

...then cd would be a variable that can point to a list of cases, but currently it holds no reference and therefor has the value null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.