You need to sign in to do that
Don't have an account?

Checking that a SOQL is not empty before working with it
I want to make sure a SOQL is not empty before I start working with it. Is there a way to perform the below code without using 2 SOQL statements? Thanks in advance for your help!
Account[] alist = new Account[]{};
if(![SELECT Id FROM Account WHERE LastModifiedDate = TODAY].isEmpty()){
alist = [SELECT Id FROM Account WHERE LastModifiedDate = TODAY];
for(Account a : alist){
//do some code...
}
}
Account[] alist = new Account[]{};
if(![SELECT Id FROM Account WHERE LastModifiedDate = TODAY].isEmpty()){
alist = [SELECT Id FROM Account WHERE LastModifiedDate = TODAY];
for(Account a : alist){
//do some code...
}
}
Please use the below code :
for(Account acc : [SELECT Id FROM Account WHERE LastModifiedDate = TODAY])
{
//Implement your logic here
}
In above code, for loop execute only if list return the records.
Thanks,
Vishal
All Answers
Account[] alist = new Account[]{};
if([SELECT count() FROM Account WHERE LastModifiedDate = TODAY]>0){
alist = [SELECT Id FROM Account WHERE LastModifiedDate = TODAY];
for(Account a : alist){
//do some code...
}
}
Thx
Please use the below code :
for(Account acc : [SELECT Id FROM Account WHERE LastModifiedDate = TODAY])
{
//Implement your logic here
}
In above code, for loop execute only if list return the records.
Thanks,
Vishal