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
Ashu sharma 38Ashu sharma 38 

how to capture last login date time using batch apex

Hi,

How to captute user last login date in custom object .

Jakub Kołodziej 7Jakub Kołodziej 7
What is business case?
In batch apex you can write easy query - SELECT LastLoginDate FROM User
Ashu sharma 38Ashu sharma 38
Hi Jakub Kołodziej 7,

Basically I have capture the last login date time history of user in my custom object hed__Application__c.

Here is my code.. as I am getting error too.FATAL_ERROR System.QueryException: unexpected token: '('



global class LastLoginDateTime implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'SELECT id, LastLoginDate FROM User WHERE Id=:UserInfo.getUserId()';
        system.debug('query' + query);
        return Database.getQueryLocator(query);
        
    }
    
    global void execute(Database.BatchableContext BC, List<hed__Application__c> scope){
        
        Schema.User u = [SELECT id, LastLoginDate FROM User WHERE Id=:UserInfo.getUserId()];
        system.debug('user>>>' +u);
        list<hed__Application__c> hedApp=[select id,Login_History__c from hed__Application__c];
        
        // Iterate through the whole query 
        for(hed__Application__c a : scope) {
            a.Login_History__c = u.LastLoginDate;
        } 
        update hedApp;
    }
    
    global void finish(Database.BatchableContext BC){
    }
    
}