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
Matthias KimmigMatthias Kimmig 

I would like to initiate a List<Account> with the result of a SOQL querry.

Hi,

I would like to initiate a List<Account> with the result of a SOQL querry. But it doesn't works.

global class UebermittlungBestellungAccount{
    
    static Boolean checkAccountId(String accountId){
        
        List<Account> acc = new List<Account>();
        Boolean check;
    	String aId;
        
        acc = [SELECT Name, Id FROM Account WHERE Id = :accountId];
        aId = acc.get(0).Id;

        If(aId==accountId){
            check=true;
        }else{
            check=false;
        }
        return check;
    }
}
Could you help me?
thanks

Regards,
Matthias

Denis VakulishinDenis Vakulishin
Hi Matthias,
try this sampe 
global class UebermittlungBestellungAccount{
    
    static Boolean checkAccountId(String accountId){
        
        Account acc = null;
        Boolean check;
    	String aId;
        try{
           acc = [SELECT Name, Id FROM Account WHERE Id = :accountId limit 1];
        }catch(Exception ex)
        { 
           return false; // record doesn't exists
        }
        aId = acc.Id;

        If(aId==accountId){
            check=true;
        }else{
            check=false;
        }
        return check;
    }
}

It should works fine
Matthias KimmigMatthias Kimmig
hi,

thanks for your help. I have changed the code now and written a test. 
global class UebermittlungBestellungAccount{
    
    public static Boolean checkAccountId(String accountId){
        
        Account acc = null;
        Boolean check;
    	String aId;
        
        try{
            acc = [SELECT Name, Id FROM Account WHERE Id = :accountId limit 1];
        }catch(Exception e){
            System.debug('Message: ' + e.getMessage());    
            System.debug('Cause: ' + e.getCause());    
            System.debug('Line number: ' + e.getLineNumber());    
            System.debug('Stack trace: ' + e.getStackTraceString());
            return false;
        }
        
        aId = acc.Id;

        If(aId==accountId){
            check=true;
        }else{
            check=false;
        }
        return check;
    }
}

I still get the following error message: DEBUG|Message: List has no rows for assignment to SObject
What can I do?

thanks
Matthias KimmigMatthias Kimmig
Is the declaration of the class an the method correct?
Denis VakulishinDenis Vakulishin
This error means that you don't have Account object with provided Id. Due to you logic, as I understand, you should return FALSE.
Denis VakulishinDenis Vakulishin
Also your code could be shortened to this
global class UebermittlungBestellungAccount{
   
public static Boolean checkAccountId(String accountId){
       
        Account acc = null;    
        try{
            acc = [SELECT Name, Id FROM Account WHERE Id = :accountId limit 1];
        }catch(Exception e){
            System.debug('Message: ' + e.getMessage());   
            System.debug('Cause: ' + e.getCause());   
            System.debug('Line number: ' + e.getLineNumber());   
            System.debug('Stack trace: ' + e.getStackTraceString());
            return false;
        }
        return true;
    }
}


As I understand in your code you check if Account with needed Id exists.
Denis VakulishinDenis Vakulishin
And don't forget that SOQL accepts 15-digit ID, but always returns 18-digit ID.
Matthias KimmigMatthias Kimmig
Could you explain me why SOQL accepts 15-digit Id?
I search for a 18-digit Id like 0011100000UtfxUAAR. 
When I search for this Id I get no result. But it should found one result.
Denis VakulishinDenis Vakulishin
Sorry, I didn't explain correctly. SOQL accepts both 15 and 18 digit ID, but always returns 18 digit. 
15 digit ID case sensative
18 digit ID case INsensative, due to APEX is naturally case insensative it operates with 18-digit ID. That's why it always returns 18-digit ID(As I understand this)
Matthias KimmigMatthias Kimmig
Thank you for your explanation.

I have tested the code again and again. Unfortunately, he still does not work. I belive that the list is not correct declare or initiate. The result is that the list is empty. 

Do you have any other ideas or tips for me?
Denis VakulishinDenis Vakulishin
Try to compose request in developer console(Query editor) an look if it returns the value 
SELECT Name, Id FROM Account WHERE Id = 'youIdGoesHere' limit 1

I've tried to this code in developer console anonymous code and it works fine. If Query editor  will return 0 rows, so the problem is with Id tha you are passing to the method.
Matthias KimmigMatthias Kimmig
The query is correct. The Id is correct. I think the declaration from the object account acc is false.
The object must contain a row, but it has none. Could you check this, please!? thanks
Denis VakulishinDenis Vakulishin
Here's code that works fine for me
public class TestCheckAccount {
    public static Boolean checkAccountId(String accountId){
        Account acc = null;    
        try{
            acc = [SELECT Name, Id FROM Account WHERE Id = :accountId limit 1];
        }catch(Exception e){
            System.debug('Message: ' + e.getMessage());   
            System.debug('Cause: ' + e.getCause());   
            System.debug('Line number: ' + e.getLineNumber());   
            System.debug('Stack trace: ' + e.getStackTraceString());
            return false;
        }
        return true;
    }
}

Do you have "with sharing" in your class declaration where checkAccountId is placed ?