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 

Need Help on Scheduled Batch

I want to create a batch that can update the contact details if it found a contact record
 
  • the batch will update the contact record
  • will only update the record once
This is my code:


global class AS_UpdateContactFromStagingObject implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'SELECT AS_Contact_Id__c, AS_Pronouns__c, AS_Aboriginal_or_Torres_Strait_Islander__c, AS_Date_of_Birth__c, AS_CALD__c, AS_Vaccination_status__c, AS_Require_an_interpreter__c,   AS_TIS_Interpreter_Language_Required__c, AS_Safe_to_use_this_phone_number__c, AS_Is_it_safe_to_use_this_email_address__c, AS_Can_a_voicemail_be_left__c, AS_Street__c, AS_City__c, AS_State_Province__c, AS_Zip_Postal_Code__c, AS_Country__c, AS_Why_is_this_their_current_location__c, AS_Agency_Referral__c, AS_Contact_Updated__c FROM Contact_Staging__c WHERE AS_Agency_Referral__c = True AND AS_Contact_Updated__c = False';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Contact_Staging__c> batch) {
        System.debug('batch=='+batch);
        List<Contact_Staging__c> conList = new List<Contact_Staging__c>();
        for(Contact_Staging__c a : batch){
            a.AS_Contact_Id__c  = '';
            a.AS_Pronouns__c  = '';
            a.AS_Aboriginal_or_Torres_Strait_Islander__c = '';
            a.AS_Date_of_Birth__c = '';
            a.AS_CALD__c = '';
            a.AS_Vaccination_status__c = '';
            a.AS_Require_an_interpreter__c = '';
            a.AS_TIS_Interpreter_Language_Required__c = '';
            a.AS_Safe_to_use_this_phone_number__c = '';
            a.AS_Is_it_safe_to_use_this_email_address__c = '';
            a.AS_Can_a_voicemail_be_left__c = '';
            a.AS_Street__c = '';
            a.AS_City__c = '';
            a.AS_State_Province__c = '';
            a.AS_Zip_Postal_Code__c = '';
            a.AS_Country__c = '';
            a.AS_Why_is_this_their_current_location__c = '';
            a.AS_Agency_Referral__c = False;
            a.AS_Contact_Updated__c = True;
            conList.add(a);
        }
        update conList;
    }
    global void finish(Database.BatchableContext BC){
    }
}




 
Anonymous DeveloperAnonymous Developer
I want to update contact records based on records info in contact staging object
Anonymous DeveloperAnonymous Developer
so if the field is not null it updates but if its null nothing will happen to the record
AnkaiahAnkaiah (Salesforce Developers) 
Hi,
 
global class AS_UpdateContactFromStagingObjectSchedule implements Schedulable

{

     global void execute(SchedulableContext sc)

     {

     AS_UpdateContactFromStagingObject b = new AS_UpdateContactFromStagingObject(); // Your batch class

       database.executeBatch(b);

     }

}

You can schedule the above the by using salesforce UI.

If this helps, Please mark it as best answer.

Thanks!!
 
Anonymous DeveloperAnonymous Developer

Hi Ankaiah

 

the scheduled batch is already finished I could not rename the title when it was created

Anonymous DeveloperAnonymous Developer
What I want to ask is about the controller code if the field given above is not null it updates if its null nothing happens I want to know the code on the logic
AnkaiahAnkaiah (Salesforce Developers) 
May i know, what is the issue now??
 
Anonymous DeveloperAnonymous Developer
so the issue is I want to get all records and update contact records based on the records info on contact staging.User-added image

So this is the contact staging i created I want when the batch updates the fields that are filled or checkboxes that are checked are updated on the contact record with the same contact Id on contactsUser-added image

does it make sense? if it does I need help. THanks
Anonymous DeveloperAnonymous Developer
so the created contact staging with the same contact Id in contacts will be updated the same as contact staging only the filled fields are updated