You need to sign in to do that
Don't have an account?

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
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
String query also should be global.
try this:
global String query = 'SELECT id, LastLoginDate FROM User WHERE Id=:UserInfo.getUserId()';
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
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 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
*Regards*
*Varun Rajpoot I Software Developer I Astrea IT
Services I gbu.varun@gmail.com
l +919650540902 l Skype - gbu.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.
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
check that do you have LastLoginDate__c field in sandbox or not.
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();
}
}