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
Jigar Patel 7Jigar Patel 7 

Please anyone help me to write test class for execute method and list function.

Hello,
Please anyone help me to write test class for execute method and list function! Hear i am give you my full apex class..

APEX Class:
global class CopyLoginHistory  {
   

    public List <LoginHistory> getLoginHistory()
    {
            return [SELECT ApiType,
                                                    ApiVersion,
                                                    Application,
                                                    Browser,
                                                    
                                                    Id,
                                                    ClientVersion,
                                                    LoginType,
                                                    LoginUrl,
                                                    Platform,
                                                    SourceIp,
                                                    Status,
                                                    UserId
                                            FROM LoginHistory
                                            WHERE LoginTime <: System.now()
                                            limit 1000];
    }
    
    public void execute ()
    {
        List <LoginHistory> lh = new List <LoginHistory> ();
        {
            lh = [SELECT ApiType, ApiVersion, Application, Browser, Id, ClientVersion, LoginType, LoginUrl, Platform, SourceIp, Status,
                   UserId FROM LoginHistory WHERE LoginTime <: System.now() limit 1000];
            
            string APIType;
            string APIVersion;
            string App;
            string Browser;
            string ClientVersion;
            string ID1;
           // DateTime LoginTime;
            string LoginType;
            string LoginURL;
            string Platform;
            string SourceIP;
            string Status;
            string UserID;
            
            for (LoginHistory login: lh)
            {
                APIType = login.ApiType;
                APIVersion = login.ApiVersion;
                App = login.Application;
                Browser = login.Browser;
                ID1 = login.Id;
                ClientVersion = login.ClientVersion;
              //  LoginTime = login.LoginTime;
                LoginType = login.LoginType;
                LoginURL = login.LoginUrl;
                Platform = login.Platform;
                SourceIP = login.SourceIp;
                Status = login.Status;
                UserID = login.UserId;
            }
            
            if (!lh.isEmpty())
            {
                List <TrackLoginHistory__c> trl = new List <TrackLoginHistory__c> ();
                User LogUser = [SELECT id, Name, Profile.Name, CompanyName, Email, Address FROM User WHERE ID =: UserID];
                for (LoginHistory login1: lh)
                {                
                    trl.add(new TrackLoginHistory__c (ApiType__c = APIType, 
                                         ApiVersion__c = APIVersion, 
                                         Application__c = App,
                                         Browser__c = Browser, 
                                         ClientVersion__c = ClientVersion, 
                                         ID__c = ID1,
                                        // LoginTime__c = String.valueOf(LoginTime),
                                         LoginType__c = LoginType,
                                         LoginURL__c = LoginURL,
                                         Platform__c = Platform,
                                         SourceIP__c = SourceIP,
                                         Status__c = Status,
                                          UserID__c = LogUser.Name,
                                          Profile__c = LogUser.Profile.Name,
                                          Address__c = String.valueOf(LogUser.Address),
                                          Email__c    = LogUser.Email,
                                          CompanyName__c = LogUser.CompanyName
                                           ));
                                         //UserID__c = UserID ));                
                
                }
                
                insert trl;                                         
            }
        }
    }
}

Thank You!
rajat Maheshwari 6rajat Maheshwari 6

@Jigar, Hope below links for sample code snippet will help you :)

https://salesforce.stackexchange.com/questions/82355/how-to-write-test-class-for-schedulable-class

 

Please let us know in case of help :)

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com

v varaprasadv varaprasad
You can not insert the data in Login History object.
In that case you can try SeeAllData=True in your test class.
@isTest (SeeAllData=true)
public class BatchTestClass {
    static testMethod void testMethod1() { 
        // Perform action here only 
    } 
}


Thanks
Varaprasad