function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
swapna muthiyaluswapna muthiyalu 

i have the following code

set<string>mEmpId = new set<string>();
set<string>hEmpId = new set<string>();
 
list<contact>listcon = [select id, empid__c,AccountId, Type__c from contact where (empid__c IN : mEmpId and AccountId IN :accountids and indexm__c = ‘yes’) or (empid__c IN : hEmpId and AccountId IN :accountids and indexh__c = ‘yes’) ]

now i want to check if mEmpId and hEmpId are blank and if it is blank i have to run this query? how to check the set is empty?
 
Best Answer chosen by swapna muthiyalu
sfdcMonkey.comsfdcMonkey.com
try this :
* // run if mEmpId and hEmpId both are NOT blank
set<string>mEmpId = new set<string>();
set<string>hEmpId = new set<string>();
 
 // run if mEmpId and hEmpId both are NOT blank
 if(mEmpId.size() > 0 && hEmpId.size() > 0){
    list<contact>listcon = [select id, empid__c,AccountId, Type__c from contact where (empid__c IN : mEmpId and AccountId IN :accountids and indexm__c = ‘yes’) or (empid__c IN : hEmpId and AccountId IN :accountids and indexh__c = ‘yes’) ]	; 
 }
 

// run if mEmpId and hEmpId both are blank
 
set<string>mEmpId = new set<string>();
set<string>hEmpId = new set<string>(); 
// run if mEmpId and hEmpId both are blank 
 if(mEmpId.size() == 0 && hEmpId.size() == 0){
 list<contact>listcon = [select id, empid__c,AccountId, Type__c from contact where (empid__c IN : mEmpId and AccountId IN :accountids and indexm__c = ‘yes’) or (empid__c IN : hEmpId and AccountId IN :accountids and indexh__c = ‘yes’)];
 }


Thanks, let us know if it helps you

 

All Answers

Gustavo BertolinoGustavo Bertolino

The method is isEmpty() which returns true if the Set is not empty and false otherwise. To see this and other Set Class's methods, take a look at this:

 

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_set.htm

sfdcMonkey.comsfdcMonkey.com
try this :
* // run if mEmpId and hEmpId both are NOT blank
set<string>mEmpId = new set<string>();
set<string>hEmpId = new set<string>();
 
 // run if mEmpId and hEmpId both are NOT blank
 if(mEmpId.size() > 0 && hEmpId.size() > 0){
    list<contact>listcon = [select id, empid__c,AccountId, Type__c from contact where (empid__c IN : mEmpId and AccountId IN :accountids and indexm__c = ‘yes’) or (empid__c IN : hEmpId and AccountId IN :accountids and indexh__c = ‘yes’) ]	; 
 }
 

// run if mEmpId and hEmpId both are blank
 
set<string>mEmpId = new set<string>();
set<string>hEmpId = new set<string>(); 
// run if mEmpId and hEmpId both are blank 
 if(mEmpId.size() == 0 && hEmpId.size() == 0){
 list<contact>listcon = [select id, empid__c,AccountId, Type__c from contact where (empid__c IN : mEmpId and AccountId IN :accountids and indexm__c = ‘yes’) or (empid__c IN : hEmpId and AccountId IN :accountids and indexh__c = ‘yes’)];
 }


Thanks, let us know if it helps you

 
This was selected as the best answer
swapna muthiyaluswapna muthiyalu
Thanks Gustavo,

set<string>mEmpId = new set<string>();
set<string>hEmpId = new set<string>();
if (!mEmpId. isEmpty() && !hEmpid.isEmpty())
 
list<contact>listcon = [select id, empid__c,AccountId, Type__c from contact where (empid__c IN : mEmpId and AccountId IN :accountids and indexm__c = ‘yes’) or (empid__c IN : hEmpId and AccountId IN :accountids and indexh__c = ‘yes’)

will this work?
Gustavo BertolinoGustavo Bertolino
I think so, as well as the code snippet above, which employed size() method instead. I didn't check the query details though. I'm just evaluating the implementation design and the method alternatives.
swapna muthiyaluswapna muthiyalu
Thanks Piyush,
will this work?
if (!mEmpId. isEmpty() && !hEmpid.isEmpty())
Pradeep SinghPradeep Singh
Yes,Both isEmpty() and .size() > 0 will work. isEmpty gives you the boolean value(true if the set is empty and false if not) AND .size() will return you the size of the set. If the set is empty, it means the size is 0 otherwise it would be greater than 0. 
Maharajan CMaharajan C
Hi Swapna,

yes you can use the both the Size() and  IsEmpty(). Mostly used is Size().

Size() = Returns the number of elements in the set .  So by using Size() > 0  we can check  there is Element is presented or not. 

IsEmpty() =  Returns true if the set has zero elements.

Always Use the Salesforce Provided Technical document that will be ver usefull!!!
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_set.htm 

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj
swapna muthiyaluswapna muthiyalu
Thanks All..
swapna muthiyaluswapna muthiyalu
Hi All,

i want the check all these conditions now, how to convert this query into dy.namic query. Please suggest

if (mEmpId .size() == 0){
list<Contact> mgrHrRecs = [select id, empid__c,AccountId, Type__c from contact where (empid__c IN : hEmpId and AccountId IN :accountids and indexh__c = ‘yes’) ];
} else if(hEmpId.size() == 0){
list<Contact> mgrHrRecs = [select id, empid__c,AccountId, Type__c from contact where (empid__c IN : mEmpId and AccountId IN :accountids and indexh__c = ‘yes’) ];
}else if (mEmpId.size() > 0 && hEmpId.size() > 0)
{list<contact>listcon = [select id, empid__c,AccountId, Type__c from contact where (empid__c IN : mEmpId and AccountId IN :accountids and indexm__c = ‘yes’) or (empid__c IN : hEmpId and AccountId IN :accountids and indexh__c = ‘yes’) ];} ;