Can we write trigger on LoginHistory object

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x