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
Ross Bai18Ross Bai18 

apex code - list - size() not return any value but null

hi, i have issue for this apex code.

loAccount2ndList is actually has the data return, but for some reason, when the code with loAccount2nd.size() > 0 logic, it seems not able to return any data but null.

i am just wondering if there is the way to query pass to the reference and having loAccount2ndList[0].value able to get some value.

many thanks.
 
public with sharing class PCL2CreateSessionImportData {

    public Account getAccount()  
       {
       
       oppId = Apexpages.currentpage().getparameters().get('id');
       
       List<Account> loAccount2ndList = [SELECT Id,OwnerId,PersonContactId,FirstName,LastName,,Outstanding_Judgements_Value__c,US_Citizenship_Value__c   
       FROM Account WHERE PersonContactId in (SELECT ContactId FROM OpportunityContactRole WHERE Role = 'Realtor' AND OpportunityId = : oppId) and IsPersonAccount = true LIMIT 1];
       
       if(loAccount2ndList.size()>0)
           loAccount2nd=loAccount2ndList[0];
       else
           loAccount2nd = new Account(); 
       return loAccount2nd;
       
       }


    public HttpResponse parseCreateSessionImportData() {

        String oppId = Apexpages.currentpage().getparameters().get('id');

        List<Account> loAccount2nd = new List<Account>();

        String RealtorEmail;  

        if (loAccount2nd.size() > 0 && loAccount2nd[0].PersonEmail != null) RealtorEmail = '                                                                                '.substring(0, (80-string.valueof(loAccount2nd[0].PersonEmail).length())) + loAccount2nd[0].PersonEmail; else RealtorEmail = '                                                                                '.substring(0,80);
  
    }

}








 
arpit vijayvergiyaarpit vijayvergiya
Hello Ross,

It seems to be some data issue. Please make sure that your soql query returns some data by either run your query in developer console or use system.debug(loAccount2ndList) before you check the size of the list.

Thank, 
Manj_SFDCManj_SFDC
At line 24 you have created a list loAccount2nd but you have not assigned 
any value to it , hence the issue 
please mark as solved if this helps you
Manj_SFDCManj_SFDC
If you try to use that list in both the methods declare outside of both the methods 
may be at line 2 and comment line 24 , that’s move line 24 to line 2
Ross Bai18Ross Bai18
@manj_SFDC - thanks.  that is what i thought as well - a list loAccount2nd but you have not assigned 
any value to it - but as i use workbench to test query - by hardcode OpportunityId , there is a row of value return. thanks.
Ross Bai18Ross Bai18

@manj_SFDC - thanks.  List<Account> loAccount2nd = new List<Account>();    //this reference should be under the method -- parseCreateSessionImportData() -- enable reference outside method loAccount2ndList  -- but i will try it as will.

i also think to simply the query of loAccount2nd or may be remove the LIMIT 1 and see what happen ?

Manj_SFDCManj_SFDC
Limit should be fine you can try by changing the reference 
Manj_SFDCManj_SFDC
Do you get the compiler error if you comment line 24, can u check 
mukesh guptamukesh gupta
Hi Rose,

first you need to remove extra comma :
User-added image
second is:-  

decleare public in class:-
       public  List<Account> loAccount2nd = new List<Account>();

and remove loAccount2nd decleration from line : 24,


Please let me know if you have any issue.

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 
Manj_SFDCManj_SFDC
Or try my second reply 
Manj_SFDCManj_SFDC
Yes Mukesh