Skip to main content
added 1327 characters in body
Source Link
Matt Lacey
  • 25.8k
  • 7
  • 68
  • 153

Update

Thank you for the replying. Here is the test method I'm using:

@IsTest(SeeAllData=true)
 Private class LastLogintest
 {
 
String ids = UserInfo.getUserId();

    // Making the assumption that at least one user will match this. Better to assign to list and check that has at least one record.
    Schema.User testUser = [Select Id, LastLoginDate__c from User where LastLoginDate != null limit 1];
    // Set it to anything other than LastLoginDate so we can tell it has changed.
   testUser.LastLoginDate__c = null;
   update testUser;

    Test.startTest();
    CaptureLastLoginDate captureBatch = new CaptureLastLoginDate();
    // Modify the query here so we only get our one testing user of interest
    captureBatch.query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User where Id =: testUser.Id limit 1';
    ID batchprocessid = Database.executeBatch(captureBatch);
    Test.StopTest();

    User afterBatchUser = [Select Id, LastLoginDate, LastLoginDate__c from User where Id =:testUser.Id limit 1];
    System.AssertEquals(afterBatchUser.LastLoginDate, afterBatchUser.LastLoginDate__c);
}

ERROR:Error: Compile Error: unexpected token: 'testUser.LastLoginDate__c' at line 10 column 3

Update

Thank you for the replying. Here is the test method I'm using:

@IsTest(SeeAllData=true)
 Private class LastLogintest
 {
 
String ids = UserInfo.getUserId();

    // Making the assumption that at least one user will match this. Better to assign to list and check that has at least one record.
    Schema.User testUser = [Select Id, LastLoginDate__c from User where LastLoginDate != null limit 1];
    // Set it to anything other than LastLoginDate so we can tell it has changed.
   testUser.LastLoginDate__c = null;
   update testUser;

    Test.startTest();
    CaptureLastLoginDate captureBatch = new CaptureLastLoginDate();
    // Modify the query here so we only get our one testing user of interest
    captureBatch.query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User where Id =: testUser.Id limit 1';
    ID batchprocessid = Database.executeBatch(captureBatch);
    Test.StopTest();

    User afterBatchUser = [Select Id, LastLoginDate, LastLoginDate__c from User where Id =:testUser.Id limit 1];
    System.AssertEquals(afterBatchUser.LastLoginDate, afterBatchUser.LastLoginDate__c);
}

ERROR:Error: Compile Error: unexpected token: 'testUser.LastLoginDate__c' at line 10 column 3

Tweeted twitter.com/#!/StackSalesforce/status/412352748043374592
added 294 characters in body
Source Link
Chris Duncombe
  • 24.3k
  • 13
  • 77
  • 116

could anyone help in writing a test class for the following code global class CaptureLastLoginDate implements Database.Batchable{

//This is the query that is passed to the execute method. .

String ids = UserInfo.getUserId(); String query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User ';

global database.queryLocator start(Database.BatchableContext BC) { System.debug(query); return database.getQueryLocator(query); } //close start method global void execute(Database.BatchableContext BC, List scope){ List<Schema.User> u = new List<Schema.User> (); for(sObject s: Scope){ Schema.User inv = (Schema.User)s; inv.LastLoginDate__c= inv.LastLoginDate; u.add(inv); System.debug(u); } update u;

}

    global class CaptureLastLoginDate implements Database.Batchable<sObject>{
        
        //This is the query that is passed to the execute method. .
        
        String ids = UserInfo.getUserId();
        String query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User ';
        
        global database.queryLocator start(Database.BatchableContext BC) {
             System.debug(query);
             return database.getQueryLocator(query);
        }
        //close start method
        global void execute(Database.BatchableContext BC, List<sObject> scope){
            List<Schema.User> u = new List<Schema.User> ();
            for(sObject s: Scope){
                Schema.User inv = (Schema.User)s;
                inv.LastLoginDate__c= inv.LastLoginDate;
                u.add(inv);
                System.debug(u);
            }
            update u;
        }
        
        global void finish(Database.BatchableContext sc){ 

        }       
   }

} Thank you in advance

could anyone help in writing a test class for the following code global class CaptureLastLoginDate implements Database.Batchable{

//This is the query that is passed to the execute method. .

String ids = UserInfo.getUserId(); String query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User ';

global database.queryLocator start(Database.BatchableContext BC) { System.debug(query); return database.getQueryLocator(query); } //close start method global void execute(Database.BatchableContext BC, List scope){ List<Schema.User> u = new List<Schema.User> (); for(sObject s: Scope){ Schema.User inv = (Schema.User)s; inv.LastLoginDate__c= inv.LastLoginDate; u.add(inv); System.debug(u); } update u;

}

global void finish(Database.BatchableContext sc){
}

} Thank you in advance

could anyone help in writing a test class for the following code

    global class CaptureLastLoginDate implements Database.Batchable<sObject>{
        
        //This is the query that is passed to the execute method. .
        
        String ids = UserInfo.getUserId();
        String query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User ';
        
        global database.queryLocator start(Database.BatchableContext BC) {
             System.debug(query);
             return database.getQueryLocator(query);
        }
        //close start method
        global void execute(Database.BatchableContext BC, List<sObject> scope){
            List<Schema.User> u = new List<Schema.User> ();
            for(sObject s: Scope){
                Schema.User inv = (Schema.User)s;
                inv.LastLoginDate__c= inv.LastLoginDate;
                u.add(inv);
                System.debug(u);
            }
            update u;
        }
        
        global void finish(Database.BatchableContext sc){ 

        }       
   }
Source Link
Sindoora
  • 735
  • 1
  • 11
  • 24

Test class for batch apex

could anyone help in writing a test class for the following code global class CaptureLastLoginDate implements Database.Batchable{

//This is the query that is passed to the execute method. .

String ids = UserInfo.getUserId(); String query = 'SELECT id, LastLoginDate,LastLoginDate__c FROM User ';

global database.queryLocator start(Database.BatchableContext BC) { System.debug(query); return database.getQueryLocator(query); } //close start method global void execute(Database.BatchableContext BC, List scope){ List<Schema.User> u = new List<Schema.User> (); for(sObject s: Scope){ Schema.User inv = (Schema.User)s; inv.LastLoginDate__c= inv.LastLoginDate; u.add(inv); System.debug(u); } update u;

}

global void finish(Database.BatchableContext sc){
}

} Thank you in advance