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
DevilJDevilJ 

Need help with an empty SObject List

Hello Everyone,

 

I need some  help with a bit of apex code.  I am creating a query list that I need to check to see if it is empty.

 

Here is my code:

if (oldPhoneName != null){
    	 List<Transportation_Services__c> oldRecord = [Select Name, Pickup_Time__c, Client__r.Last_Name__c,  Pickup_Location__r.Name,
        Drop_Off_Location__r.Name, Phone_Name__c, Notes_Transportation__c from Transportation_Services__c 
        WHERE Pickup_Time__c = TODAY AND Phone_Name__c = :oldPhoneName AND Name != :valueTwo Order by Pickup_Time__c];
        if(oldRecord.isEmpty){
        	createXML(record, oldPhoneName, true);
        }else{
        	createXML(oldRecord, oldphoneName, false);
        }
    } 

 

 

Apex does not like my 'records.isEmpty'.  I get this error on compile:  

Save error: Initial term of field expression must be a concrete SObject: LIST<Transportation_Services__c>  

 

Does anyone know of a way that I can check if a SOQL generated list came back with no records (empty)?

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
MarkWaddleMarkWaddle

You are missing the parentheses

 

if(oldRecord.isEmpty()){

 

 

 

All Answers

MarkWaddleMarkWaddle

You are missing the parentheses

 

if(oldRecord.isEmpty()){

 

 

 

This was selected as the best answer
Ankit AroraAnkit Arora

It bit simple, try this :

 

 

if (oldPhoneName != null){
    	 List<Transportation_Services__c> oldRecord = [Select Name, Pickup_Time__c, Client__r.Last_Name__c,  Pickup_Location__r.Name,
        Drop_Off_Location__r.Name, Phone_Name__c, Notes_Transportation__c from Transportation_Services__c 
        WHERE Pickup_Time__c = TODAY AND Phone_Name__c = :oldPhoneName AND Name != :valueTwo Order by Pickup_Time__c];
        if(oldRecord.size() > 0){
        	createXML(record, oldPhoneName, true);
        }else{
        	createXML(oldRecord, oldphoneName, false);
        }
    } 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

 

DevilJDevilJ

OK...I'm gonna crawl back into my newbie hole now.....sorry!!

 

Thanks guys.

 

(I'm such an idiot......)

MarkWaddleMarkWaddle

Nah, it always helps to have a second set of eyes look at something.

 

Would you mind marking either my post or Ankit's post as the solution, and not your own?

 

Thanks,

Mark