Q, Write a Batch apex class to automatically deactivate users when the user's
Last Login Date is greater than 30 days.
global class DeactivateUsers implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext bc){
string query = 'SELECT Name,isActive,LastLoginDate FROM User WHERE
LastLoginDate < LAST_N_DAYS:30';
return database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<User> users){
List<user> userlist = new List<User>();
for(User u : users){
u.IsActive = false;
userlist.add(u);
}
update userlist;
}
global void finish(Database.BatchableContext bc){
}
}
===========================================================================
Q, Write a Batch apex class to automatically delete the Leads whose Lead Status is
"Closed- Not Converted"
Explaination::
SELECT Id,Name,Status FROM Lead where Status = 'Closed - Not Converted'
DeleteLeadsNotConverted: Class must implement the global interface method:
Iterable<SObject> start(Database.BatchableContext)
from Database.Batchable<SObject>,
DeleteLeadsNotConverted: Class must implement the global interface method:
void execute(Database.BatchableContext, List<SObject>)
from Database.Batchable<SObject>,
DeleteLeadsNotConverted: Class must implement the global interface method:
void finish(Database.BatchableContext)
Coding::
global class DeleteLeadsNotConverted implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext bc){
string query = 'SELECT Id,Name,Status FROM Lead where Status = \'Closed - Not Converted\'';
return database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<Lead> leads){
// Delete the Leads which are not converted.
Delete Leads;
// Removes the Leads from the Recycle Bin.
database.emptyRecycleBin(leads);
}
global void finish(Database.BatchableContext bc) {
}
}
==========================================================================
Q, Write a Batch Class to Update the Status of the Campaign to Completed if it is not Completed.
global class UpdateCampaignStatus implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext bc) {
string query = 'SELECT Id,Name,Enddate,Status FROM Campaign WHERE Enddate < TODAY';
return database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<Campaign> campaigns) {
List<Campaign> camplist = new List<Campaign>();
for(Campaign c :campaigns){
// Check if the Status is not Completed
if(c.status != 'Completed'){
// Change the Status to Completed
c.status = 'Completed';
camplist.add(c);
}
}
update camplist;
}
global void finish(Database.BatchableContext bc) {
}
}
==========================================================================
Aditional information::
Test.StartTest() and Test.StopTest() are the Test methods Which are used to test the asynchronous processes like
Batch Apex,Schedule Apex,Queueable Apex and future methods.
After the Call to the Test.stopTest() the code gets executed synchronously.
A separate set of Governor Limits are Introduced for the code in between the Test.StartTest() and Test.StopTest() Methods.
==================================================================
// Executing a Batch Class
// Create an Instance of the Batch Class
UpdateCampaignStatus obj = new UpdateCampaignStatus();
// Call the Batch
Database.executeBatch(obj,200);
No comments:
Post a Comment