+ Start a Discussion
Jack A 2Jack A 2 

How to count if a particular field has some value?

I want to count how many Contact has value under description field using for loop. Similarly if Contact object has custom field then how to count those field which has some value
SELECT COUNT() Amo__c FROM Contact WHERE Description != null
The above query is not working
Best Answer chosen by Jack A 2
Keyur  ModiKeyur Modi
Hi Jack A, 

If you want to count number of contact where description is not null then you can do it like this 
SELECT COUNT() id FROM Contact WHERE Description != null
or else you can do it with one more way 
list<Contact> lstCont = new list<Contact>();

lstCont = [SELECT id, Name FROM contact WHERE Description != null ];


integer totalContact;

totalContact = lstCont.size(); // list.size() is salesforce standard method to get list size


Please let me know if you need more guidance on this.

Thanks,
Keyur Modi 

All Answers

VineetKumarVineetKumar
You can not use long text area fields in where clause of query.
Perhaps you can create a formulae field on contact of type checkbox, that cn check this description field is null or not for you.
Then use this formulae field in your query.
Keyur  ModiKeyur Modi
Hi Jack A, 

If you want to count number of contact where description is not null then you can do it like this 
SELECT COUNT() id FROM Contact WHERE Description != null
or else you can do it with one more way 
list<Contact> lstCont = new list<Contact>();

lstCont = [SELECT id, Name FROM contact WHERE Description != null ];


integer totalContact;

totalContact = lstCont.size(); // list.size() is salesforce standard method to get list size


Please let me know if you need more guidance on this.

Thanks,
Keyur Modi 
This was selected as the best answer
VineetKumarVineetKumar
@Keyur : Did you try running your code?
Jack A 2Jack A 2
@Keyur Modi First one is not working Second one is working. Thanx!