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 creating a scheduled batch

Need help creating a scheduled batch class here is my code.

APEX CLASS:

global class AS_UpdateContactFromStagingObject implements Database.Batchable<sObject> {

    global Database.QueryLocator start(Database.BatchableContext BC){

        String query = 'SELECT AS_Contact_Id__c, AS_Date_of_Birth__c, AS_Vaccination_status__c , AS_Safe_to_use_this_phone_number__c, AS_State_Province__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_Updated__c = true;
            conList.add(a);
        }


        update conList;

    }
    global void finish(Database.BatchableContext BC){

    }

}

Scheduler:
global class AS_UpdateContactFromStagingSchedulable implements Schedulable {
    public static String sched = '0 00 00 * * ?'; 

    global static String scheduleBatch() {

        AS_UpdateContactFromStagingSchedulable SC = new AS_UpdateContactFromStagingSchedulable(); 

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

    }

    global void execute(SchedulableContext sc) {

        AS_UpdateContactFromStagingObject batch = new AS_UpdateContactFromStagingObject();
        Database.executeBatch(batch, 50);
 

    }

}

so the logic is when you create a new contact stage the contact Id must be an existing Id on contact. the update happens when the contact staging record is created and the batch is running.
 
if the fields on contact staging are not equal to null it updates and if the fields are null nothing will happen. does that make sense? Need Help. Thanks
 
Best Answer chosen by Anonymous Developer
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below code for batch class.
In my code I copied AS_Date_of_Birth__c field from stagging  object  to Birthdate on contact object.

In the similar way you have to add the other columns and map them.
global class AS_UpdateContactFromStagingObject implements Database.Batchable<sObject> {

    global Database.QueryLocator start(Database.BatchableContext BC){

        String query = 'SELECT AS_Contact_Id__c, AS_Date_of_Birth__c, AS_Vaccination_status__c , AS_Safe_to_use_this_phone_number__c, AS_State_Province__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>();
        map<id,Contact_Staging__c> maptoupdate= new Map<id,Contact_Staging__c>();
        for(Contact_Staging__c a : batch){
            a.AS_Contact_Updated__c = true;
            maptoupdate.put(a.AS_Contact_Id__c,a);
            conList.add(a);
        }
        if(conList.size()>0)
        update conList;
        List<Contact> conlist1=[select id,Birthdate  from contact where id in :maptoupdate.keyset()];
        List<contact> contobeupdated= new List<Contact>();
        
        For(Contact con :conlist1){
            Contact_Staging__c contstgvalue=maptoupdate.get(con.id);
            if(contstgvalue.AS_Date_of_Birth__c!=null)
            con.Birthdate= contstgvalue.AS_Date_of_Birth__c;
            contobeupdated.add(con);
        }
        if(contobeupdated.size()>0)
            update contobeupdated;

    }
    global void finish(Database.BatchableContext BC){

    }

}
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,

Is AS_Contact_Id__c field a contact id field which gets populated on contact staging record?

So you want tha batch to copy the fields from Contact staging to contact and update those fields if the value is not null.

Thanks,
 
Anonymous DeveloperAnonymous Developer
yes that is correct sai I want the fields from contact staging to update those fields if the value is not null
Anonymous DeveloperAnonymous Developer
need help on the code 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Yes this logic need to be implemented in our personal org as these are custom objects it takes time to implement the logic. Please allow some time to answer this.

Thanks,
 
Anonymous DeveloperAnonymous Developer
So far the logic that I came up with is this I don't know if this is correct
Contact Staging List batch > 
List Contact Ids

Map<Id, Contact> idConMap = new map<Id, Contact>();
List contact toBeUpdated;

for ( Contact con : [Select Id, fields here FROM Contact WHERE Id in :listContIds] ) {
    idConMap.put( con.Id, con );
}

for ( Contact Staging a : batch ) {
    Contact mapCont = idConMap.get(a.ContactId__c);

    if ( a.field != null ) {
        mapCont.field = a.field;
    }

    toBeUpdated.add( mapCont );
}

update toBeUpdated;


but I have no clue on how to implement it through code. Thanks
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the below code for batch class.
In my code I copied AS_Date_of_Birth__c field from stagging  object  to Birthdate on contact object.

In the similar way you have to add the other columns and map them.
global class AS_UpdateContactFromStagingObject implements Database.Batchable<sObject> {

    global Database.QueryLocator start(Database.BatchableContext BC){

        String query = 'SELECT AS_Contact_Id__c, AS_Date_of_Birth__c, AS_Vaccination_status__c , AS_Safe_to_use_this_phone_number__c, AS_State_Province__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>();
        map<id,Contact_Staging__c> maptoupdate= new Map<id,Contact_Staging__c>();
        for(Contact_Staging__c a : batch){
            a.AS_Contact_Updated__c = true;
            maptoupdate.put(a.AS_Contact_Id__c,a);
            conList.add(a);
        }
        if(conList.size()>0)
        update conList;
        List<Contact> conlist1=[select id,Birthdate  from contact where id in :maptoupdate.keyset()];
        List<contact> contobeupdated= new List<Contact>();
        
        For(Contact con :conlist1){
            Contact_Staging__c contstgvalue=maptoupdate.get(con.id);
            if(contstgvalue.AS_Date_of_Birth__c!=null)
            con.Birthdate= contstgvalue.AS_Date_of_Birth__c;
            contobeupdated.add(con);
        }
        if(contobeupdated.size()>0)
            update contobeupdated;

    }
    global void finish(Database.BatchableContext BC){

    }

}
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
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Hammad,

Is the issue resolved now?

Thanks,
Anonymous DeveloperAnonymous Developer
Thanks Sai