+ Start a Discussion
jsorribasjsorribas 

System.SObjectException

Hi all,

I have this code into a class in which I have to program to execute daily in SalesForce.

 

list<User> registrosUser = [select id, title, firstname, lastname, email, phone, ContactId
                                FROM User WHERE ContactId <>: null AND LastLoginDate = YESTERDAY];
            
        for(User rUser : registrosUser){
            
            Contact ctc = [select id, Inicio_Sesion__c, Ultimo_inicio_de_sesion__c FROM Contact WHERE id=: rUser.ContactId LIMIT 1];
            System.debug(ctc.Ultimo_inicio_de_sesion__c);
            ctc.Ultimo_inicio_de_sesion__c = rUser.LastLoginDate;
            ctc.Inicio_Sesion__c = true;
            update ctc;
        }

 

I have tried this code within the developer shell, and the query returns the correct register, but this line appears on the debug output:

 

13:06:13.033 (33547000)|EXCEPTION_THROWN|[8]|System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: User.LastLoginDate
13:06:13.033 (33585000)|HEAP_ALLOCATE|[8]|Bytes:95
13:06:13.033 (33725000)|FATAL_ERROR|System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: User.LastLoginDate

 

Could you help me about this exception??

 

Thanks in advance!

Navatar_DbSupNavatar_DbSup

http://boards.developerforce.com/t5/Apex-Code-Development/System-SObjectException/td-p/364891

 

Hi,

 

You are trying to use LastLoginfield without query that causes the exception, so now you can use your code below as i query LastLoginDate field in your query.


                list<User> registrosUser = [select id, title, firstname, lastname, email, phone, ContactId,LastLoginDate
                FROM User WHERE ContactId <>: null AND LastLoginDate = YESTERDAY];


                for(User rUser : registrosUser)
                {
                                Contact ctc = [select id, Inicio_Sesion__c, Ultimo_inicio_de_sesion__c FROM Contact WHERE id=:                                     rUser.ContactId LIMIT 1];
                                System.debug(ctc.Ultimo_inicio_de_sesion__c);
                                ctc.Ultimo_inicio_de_sesion__c = rUser.LastLoginDate;
                                ctc.Inicio_Sesion__c = true;
                                update ctc;
                }

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

 

Shashikant SharmaShashikant Sharma

Let me explain what this error means

 

SObject row was retrieved via SOQL without querying the requested field: User.LastLoginDate

 

This means you are accessing LastLoginDate field in your code without querying it. If you look at your query you have used this field "LastLoginDate" as filter but you have not queried this field in output.

 

In your query output fields are : "id, title, firstname, lastname, email, phone, ContactId", but not LastLoginDate 

 

Just add this field in query and your code will work fine.

 

Please let me know if any issues in above explaination.

Rohan TelangRohan Telang
@Shashikant Sharma- Thank you for explaining.