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
Anonymous DeveloperAnonymous Developer 

Scheduled Batch Need Help

So I want a batch that will update the contact record only once.
  • I want to get all the records where the field agency referral is true and the field contact update = false
  • I want to update contact records based on records info
  • and update contact update = false

This is the code that I have for now since I'm stuck and need help.

Apex class:

global class UpdateContactFromStagingObject implements Database.Batchable<sObject> {
    List<Contact> conList = new List<Contact>();
    global Database.QueryLocator start(Database.BatchableContext BC){

        String query = 'SELECT  AS_Contact_Id__c FROM Contact_Staging__c WHERE AS_Agency_Referral__c = True AND Contact_Updated__c = False';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Contact> batch) {
        System.debug('batch=='+batch);
        Set<String> conId = new Set<String>();
        for(Contact_Staging__c a : batch){
            conId.add(a.AS_Contact_Id__c);
        }
    }
    global void finish(Database.BatchableContext BC){
       
    }
}

Scheduled Batch Class:

global class UpdateContactFromStagingObjectSchedulable implements Schedulable {
        
    public static String sched = '0 00 05 * * ?'; 

    global static String scheduleMe() {
        UpdateContactFromStagingObjectSchedulable SC = new UpdateContactFromStagingObjectSchedulable(); 

        return System.schedule('Update Contact Records', sched, SC);
    }

    global void execute(SchedulableContext sc) {
        UpdateContactFromStagingObject batch = new UpdateContactFromStagingObject();
        Database.executeBatch(batch, 200);
    }
}

Is there anything I need to add? Thanks.

Best Answer chosen by Anonymous Developer
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hamad,

Please check the below batch class.
 
global class UpdateContactFromStagingObject implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext BC){

        String query = 'SELECT Name,Contact_Updated__c, AS_Agency_Referral__c,AS_Contact_Id__c FROM Contact_Staging__c WHERE AS_Agency_Referral__c = True AND Contact_Updated__c = False';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Contact_Staging__c> conlist) {
        System.debug('batch=='+batch);
        List<Contact_Staging__c> conuplist= new List<Contact_Staging__c>();
        for(Contact_Staging__c a : conlist){
            a.Name= a.name + 'Updated from batch';
            a.Contact_Updated__c=True;
       conuplist.add(a);
        }
update conuplist
    }
    global void finish(Database.BatchableContext BC){
       
    }
}

Schedular;
 
global class UpdateContactFromStagingObjectSchedulable implements Schedulable {
        
  global void execute(SchedulableContext ctx) { UpdateContactFromStagingObject p = new UpdateContactFromStagingObject(); database.executeBatch(p); 
} 
}

You can schdule this schedular as below in required time through UI.

You can go to Setup > Build > Develop > Apex Classes and then press on the Schedule Apex button

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hamad,

Can you let us know what you are trying to update on Contact_Staging__c object. Are you trying to update any field? If so can you let us know which field you want to update because there is not logic for updation of field in the batch.

Also can you let us know when do you want to schedule the class to run?

Thanks,
 
mukesh guptamukesh gupta
Hi Hamad,

Which fields you want to update on contact records due to batch proecss

 
Anonymous DeveloperAnonymous Developer

Hello Sai and Mukesh

 

Any field be it name or Id or status

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hamad,

Please check the below batch class.
 
global class UpdateContactFromStagingObject implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext BC){

        String query = 'SELECT Name,Contact_Updated__c, AS_Agency_Referral__c,AS_Contact_Id__c FROM Contact_Staging__c WHERE AS_Agency_Referral__c = True AND Contact_Updated__c = False';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Contact_Staging__c> conlist) {
        System.debug('batch=='+batch);
        List<Contact_Staging__c> conuplist= new List<Contact_Staging__c>();
        for(Contact_Staging__c a : conlist){
            a.Name= a.name + 'Updated from batch';
            a.Contact_Updated__c=True;
       conuplist.add(a);
        }
update conuplist
    }
    global void finish(Database.BatchableContext BC){
       
    }
}

Schedular;
 
global class UpdateContactFromStagingObjectSchedulable implements Schedulable {
        
  global void execute(SchedulableContext ctx) { UpdateContactFromStagingObject p = new UpdateContactFromStagingObject(); database.executeBatch(p); 
} 
}

You can schdule this schedular as below in required time through UI.

You can go to Setup > Build > Develop > Apex Classes and then press on the Schedule Apex button

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
Anonymous DeveloperAnonymous Developer

Hey Sai what if when I create a contact staging I want it to mirror the update on my contacts and it only updates the fields that have been filed or ticked and the fields that are not filed are not updated how would that look?

 

User-added image

this is the contact staging so it only updates when the contact Id is the same contact Id as the contact? does that make sense?
 

Sai PraveenSai Praveen (Salesforce Developers) 

Hi Hammad,

I could not understand your ask. I am not sure how the batch which we wrote it related to that UI.


Thanks,
 

Anonymous DeveloperAnonymous Developer

Hi Sai

 

What I mean to ask is that I want to create a batch that can update the contact staging. 

If the contact Id on contact staging exists on contacts it updates only the fields that have a value when creating the contact staging on this picture

User-added image

So if the text area or picklist or checkbox is empty it will not update the contacts but if it's not empty the contact staging updates. 

 

Anonymous DeveloperAnonymous Developer
I want to update contact records based on records info in contact staging