You need to sign in to do that
Don't have an account?
April Barclay 8
Apex class to update a check box field
Hello all. I am VERY new to the development side of SF but have been doing admin for over 5 years. I want to use Apex to edit a checkbox on the contacts object every day...it can run once daily or throughout the day if possible. How do I accomplish this? I assume I need to write an apex class but have no idea what the code should be. Please help.
This is your batch class. Just replace checkbox__c with the API name of your checkbox field on Contact.
global class UpdateContacts implements Database.Batchable<sObject>
{
global Database.queryLocator start(Database.BatchableContext ctx )
{
String str = 'SELECT Id, checkbox__c FROM Contact WHERE checkbox__c = false';
return Database.getQueryLocator(str);
}
global void execute(Database.BatchableContext ctx, List<Contact> contactToProcess)
{
List<Contact> contactList = new List<Contact>();
for(Contact conObj : contactToProcess){
checkbox__c = true;
contactList.add(conObj);
}
update contactList;
}
global void finish(Database.BatchableContext ctx)
{
AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob WHERE Id =
:ctx.getJobId()];
// Send an email to the Apex job's submitter notifying of job completion.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Sharing Recalculation ' + a.Status);
mail.setPlainTextBody
('The batch Apex job processed ' + a.TotalJobItems +
' batches with '+ a.NumberOfErrors + ' failures.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
You also need to run this batch from developer console as follows:
Id batchJobId = Database.executeBatch(new UpdateContacts(), 200);
All Answers
List<Contact> listName = [SELECT Id, checkbox FROM Contact where checkbox = false]; // This query needs to be selective else you can hit the governor limits.
for(Contact obj : listName){
obj.checkbox = true;
}
update listName;
You need to make use of batch apex if you want this to happen once daily or at regular intervals.
Thanks
Abhijeet
This is your batch class. Just replace checkbox__c with the API name of your checkbox field on Contact.
global class UpdateContacts implements Database.Batchable<sObject>
{
global Database.queryLocator start(Database.BatchableContext ctx )
{
String str = 'SELECT Id, checkbox__c FROM Contact WHERE checkbox__c = false';
return Database.getQueryLocator(str);
}
global void execute(Database.BatchableContext ctx, List<Contact> contactToProcess)
{
List<Contact> contactList = new List<Contact>();
for(Contact conObj : contactToProcess){
checkbox__c = true;
contactList.add(conObj);
}
update contactList;
}
global void finish(Database.BatchableContext ctx)
{
AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob WHERE Id =
:ctx.getJobId()];
// Send an email to the Apex job's submitter notifying of job completion.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Sharing Recalculation ' + a.Status);
mail.setPlainTextBody
('The batch Apex job processed ' + a.TotalJobItems +
' batches with '+ a.NumberOfErrors + ' failures.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
You also need to run this batch from developer console as follows:
Id batchJobId = Database.executeBatch(new UpdateContacts(), 200);
checkbox__c = true;
contactList.add(conObj);
With my api name for the field it variable does not exist.
After I have the class created...don't I need to build a trigger to run this each morning?
Id batchJobId = Database.executeBatch(new UpdateContacts(), 200);
for(Contact conObj : contactToProcess){
checkbox__c = true;
contactList.add(conObj);
I replace the field with my api name and it says variable does not exist.
And when I create the Apex trigger with the above code...when will it know to run? I want it to run each day at 6AM
for(Contact conObj : contactToProcess){
conObj.checkbox__c = true;
contactList.add(conObj);
Create a scheduler class as follows:
global class MySchedulableClass implements Schedulable {
global void execute(SchedulableContext ctx) {
database.executeBatch(new new UpdateContacts(),200);
}
}
Please follow below Steps:-
Step1 :- go to classes
Setup->Apex classes then click on Schedule Apex button
Step 3:- Then add date and time & select the above scheduler class in Apex class lookup
global class UpdateContacts implements Database.Batchable<sObject>
{
global Database.queryLocator start(Database.BatchableContext ctx )
{
String str = 'SELECT Id, update_contact__c FROM Contact WHERE update_contact__c = false';
return Database.getQueryLocator(str);
}
global void execute(Database.BatchableContext ctx, List<Contact> contactToProcess)
{
List<Contact> contactList = new List<Contact>();
for(Contact conObj : contactToProcess){
conObj.update_contact__c = true;
contactList.add(conObj);
}
update contactList;
}
global void finish(Database.BatchableContext ctx)
{
AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob WHERE Id =
:ctx.getJobId()];
// Send an email to the Apex job's submitter notifying of job completion.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {a.CreatedBy.Email};
mail.setToAddresses(toAddresses);
mail.setSubject('Apex Sharing Recalculation ' + a.Status);
mail.setPlainTextBody
('The batch Apex job processed ' + a.TotalJobItems +
' batches with '+ a.NumberOfErrors + ' failures.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
global class MySchedulableClass implements Schedulable {
global void execute(SchedulableContext ctx) {
database.executeBatch(new UpdateContacts(),200);
}
}
trigger Update_Contacts_Trigger on Contact (before insert) {
Id batchJobId = Database.executeBatch(new UpdateContacts(), 200);
}
Thanks
Abhijeet
This will be your test class. Create a new class as above, save it and click on the 'Run Test' button.