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

Batch Apex Question
Hi ,
I'm trying to write a batch apex on User. I have to capture the system generated lastlogindate into the custom field called LastLoginDate__c For this i wrote a code which is as below.
global class SearchAndReplace implements Database.Batchable<sobject>{
global final String Query;
global final String Entity;
global final String Field;
global final String Value;
global SearchAndReplace(String q, String e, String f, String v){
Query=q; Entity=e; Field=f;Value=v;
}
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<User> scope){
for(User u:Use)
{
u=[select LastLoginDate from User where Id=:UserInfo.getUserId()];
u.LastLoginDate__c=u.LastLoginDate;
u.put(Value);
}
update use;
}
global void finish(Database.BatchableContext BC){
}
}
The error what i get is Error: Compile Error: Argument type of global method must also be global: LIST<User> at line 17 column 16.
could any one guide me.Thank you in advance
pass the query :
[select LastLoginDate from User where Id=:UserInfo.getUserId()];
in batch, scope will have all the records from this query. iterate through this scope and update accordingly.
Shamily,
If I understand what you are trying to do, this code should work for you:
If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!
-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator
Error Error: Compile Error: Variable does not exist: LastLoginDate__c at line 14 column 13
i had a custom field LastLoginDate__c.but y it is showing error
Error must be at the right hand side of expression
u.LastLoginDate__c = u.LastLoginDate;
LastLoginDate. If its a custom field append it with __c
nope The one in the LHS is custom field and the other is standard
Shamily,
I created this class in my DE org and added the custom field "LastLoginDate__c" to User as a Date/Time field. I am able to save the code without error.
Please doublecheck that your User object has the custom field "LastLoginDate__c", check the spelling, and make sure it is a Date/Time field - not just a Date field.
Let me know if you continue to have problems.
-Glyn
Shamily,
Just curious - why do you want to do this as Batchable? Unless your org has more than 10,000 users (or is likely to in the future), you can do this in a non-Batchable class. If you want it to be Schedulable, you could implement Schedulable instead of Batchable.
I thought about doing this as a Workflow rule on User, but you can't access the LastLoginDate field in a Field Update. :-(
I also thought about doing this in a trigger on User, but my guess is that the User trigger won't run when the system updates the LastLoginDate field (which other forum posts seem to confirm).
Just wondering...
-Glyn