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
sindoorasindoora 

Batch apex

Hi all,

I'm trying to create a batch apex to capture system generated lastlogindate on to custom field.But this is firing an error.

Here is the code

global class CaptureLastLoginDate implements Database.Batchable<sObject>{
 
    //This is the query that is passed to the execute method.  .
    
    String query = 'SELECT id, LastLoginDate FROM User WHERE Id=:UserInfo.getUserId()';
 
    global database.queryLocator start(Database.BatchableContext BC) {
        return database.getQueryLocator(query);
 
    }
    //close start method
 global void  Execute(Database.BatchableContext BC, List<User> scope) {
 User u = [SELECT id, LastLoginDate FROM User WHERE Id=:UserInfo.getUserId()];
 
      
       // Iterate through the whole query
        for(User a : scope) {
            
                a.LastLoginDate__c = u.LastLoginDate;
            } //close if statement
         update LastLoginDate__c;
         }
        
        //close for-loop
      
     //close execute method
 
    global void finish(Database.BatchableContext BC) {
 
        
    } //close finish method

 

Could anyone help me.Thank you in advance

error:

Error: Compile Error: Argument type of global method must also be global: LIST<User> at line 12 column 15

 

Best Answer chosen by Admin (Salesforce Developers) 
gbu.varungbu.varun

I find out your error:

 

Look at you code :

// Iterate through the whole query
for(User a : scope) {
a.LastLoginDate__c = u.LastLoginDate;
} //close if statement
update LastLoginDate__c;  // Error

}

 

 

Try the following code:

 

 

List<User> ua = new List<USER> ();
for(User a : scope) {
a.LastLoginDate__c = u.LastLoginDate;
ua.add(a);
} //close if statement
update ua;

 

 

 

All Answers

gbu.varungbu.varun

    String query also should be global.

try this:

global    String query = 'SELECT id, LastLoginDate FROM User WHERE Id=:UserInfo.getUserId()';

sindhu123sindhu123
thanks for the reply.
still i get the same error msg
Error Error: Compile Error: Argument type of global method must also be global: LIST<User> at line 12 column 15
gbu.varungbu.varun

I find out your error:

 

Look at you code :

// Iterate through the whole query
for(User a : scope) {
a.LastLoginDate__c = u.LastLoginDate;
} //close if statement
update LastLoginDate__c;  // Error

}

 

 

Try the following code:

 

 

List<User> ua = new List<USER> ();
for(User a : scope) {
a.LastLoginDate__c = u.LastLoginDate;
ua.add(a);
} //close if statement
update ua;

 

 

 

This was selected as the best answer
sindoorasindoora
global class CaptureLastLoginDate implements Database.Batchable<sObject>{

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

global String query = 'SELECT id, LastLoginDate,LastLoginDate__C FROM User WHERE Id=:UserInfo.getUserId()';

global database.queryLocator start(Database.BatchableContext BC) {
return database.getQueryLocator(query);

}
//close start method
global void Execute(Database.BatchableContext BC, List<User> scope) {
User u = [SELECT id, LastLoginDate FROM User WHERE Id=:UserInfo.getUserId()];


// Iterate through the whole query
List<User> ua = new List<USER> ();
for(User a : scope) {
a.LastLoginDate__c = u.LastLoginDate;
ua.add(a);
} //close if statement
update ua;
}


//close for-loop

//close execute method

global void finish(Database.BatchableContext BC) {


} //close finish method
}
Still the error remains
gbu.varungbu.varun
When are you getting error while you saving or running batch?
*Regards*

*Varun Rajpoot I Software Developer I Astrea IT
Services I gbu.varun@gmail.com
l +919650540902 l Skype - gbu.varun *
sindoorasindoora
while saving
gbu.varungbu.varun

I am able to save following code:

 

 

global class CaptureLastLoginDate implements Database.Batchable<sObject>{

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

String query = 'SELECT id, LastLoginDate FROM User WHERE Id=:UserInfo.getUserId()';

global database.queryLocator start(Database.BatchableContext BC) {
return database.getQueryLocator(query);

}
//close start method
global void Execute(Database.BatchableContext BC, List<User> scope) {
User u = [SELECT id, LastLoginDate FROM User WHERE Id=:UserInfo.getUserId()];

List<user> ua = new List<user>();
// Iterate through the whole query
for(User a : scope) {
a.LastLoginDate__c = u.LastLoginDate;
ua.add(a);
} //close if statement
update ua;

}

//close for-loop

//close execute method

global void finish(Database.BatchableContext BC) {


} //close finish method
}

 

 

If you get some error while saving please give this error to me.

sindoorasindoora
Hi Varun,
This is strange i'm using the same code but still it is firing error.
Error Error: Compile Error: Argument type of global method must also be global: LIST<User> at line 12 column 13
sindoorasindoora
Yup in dev this is saved. but when i try to save the same cod on sanbax it is showing an error message
gbu.varungbu.varun

check that do you have LastLoginDate__c  field in sandbox or not.

sindoorasindoora
Hi Varun,
Error has been resolved.Here is the process.
1.written batch apex.
2.trigger
3.scheduled batch apex
1.1.Batch Apex

global class CaptureLastLoginDate implements Database.Batchable<sObject>{

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

String query = 'SELECT id, LastLoginDate FROM User WHERE Id=:UserInfo.getUserId()';

global database.queryLocator start(Database.BatchableContext BC) {
return database.getQueryLocator(query);

}
//close start method
global void Execute(Database.BatchableContext BC, List<Schema.User> scope) {
Schema.User u = [SELECT id, LastLoginDate FROM User WHERE Id=:UserInfo.getUserId()];

// Iterate through the whole query
for(Schema.User a : scope) {
a.LastLoginDate__c = u.LastLoginDate;
insert a;
} //close if statement


}

//close for-loop

//close execute method

global void finish(Database.BatchableContext BC) {


} //close finish method
}

2.Trigger

trigger LastLogin on User (after update) {
CaptureLastLoginDate obj=new CaptureLastLoginDate();
database.executebatch(obj,10);

}

3.Scheduled class
global class scheduledMerge implements Schedulable {
global void execute(SchedulableContext SC) {
CaptureLastLoginDate M = new CaptureLastLoginDate();
}
}