1

I want to copy login history data in custom object when new entry added to login history. If trigger is not possible then is there workaround for the same

1 Answer 1

5

You can't create a trigger on LoginHistory object. But as workaround you can create a scheduled apex which will query LoginHistory objects

global class CopyLoginHistory implements Schedulable {
    global void execute(SchedulableContext ctx) {
        List<LoginHistory> loginHistories = [SELECT ApiType,
                                                    ApiVersion,
                                                    Application,
                                                    Browser,
                                                    ClientVersion,
                                                    Id,
                                                    LoginTime,
                                                    LoginType,
                                                    LoginUrl,
                                                    Platform,
                                                    SourceIp,
                                                    Status,
                                                    UserId
                                            FROM LoginHistory
                                            WHERE LoginTime <: System.now()
                                            AND LoginTime >: System.now().addMinutes(-1)];
        if ( !loginHistories.isEmpty()) {
            List<LoginHistoryCopy__c> lhc = new List<LoginHistoryCopy__c>();
            for (LoginHistory lh: loginHistories) {
                lhc.add(new LoginHistoryCopy__c(ApiType__c       = ApiType ,
                                                ApiVersion__c    = ApiVersion ,
                                                Application__c   = Application ,
                                                Browser__c       = Browser ,
                                                ClientVersion__c = ClientVersion ,
                                                Id__c            = Id ,
                                                LoginTime__c     = LoginTime ,
                                                LoginType__c     = LoginType ,
                                                LoginUrl__c      = LoginUrl ,
                                                Platform__c      = Platform ,
                                                SourceIp__c      = SourceIp ,
                                                Status__c        = Status ,
                                                UserId__c        = UserId
                ));
            }
            insert lhc;
        }
   }
}

and copy these records to custom object

5
  • But in this case we can get real time data. right?
    – pornima
    Commented Feb 19, 2015 at 6:42
  • @pornima What is the reason for copying this data? Commented Feb 19, 2015 at 6:45
  • If the user don't have manage user permission he can't access login history data. I want to show login history details to users who don't have manage user permission. I just want to copy login history in custom object and give access to that custom object.
    – pornima
    Commented Feb 19, 2015 at 6:54
  • @pornima and why do you need a real time data? Is it critical data for your users? Commented Feb 19, 2015 at 6:59
  • 1
    @pornima Scheduled apex has limit in 250.000 executions per 25-hour period. Running scheduled apex every minute is required 1440 executions per 24h. Commented Feb 19, 2015 at 7:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .