3

hi how can i schedule to run an apex class with logic below

  1. select all contact records
  2. select all campaign where start date in year 2014
  3. select all campaign members where campaign in no.2

loop all contact records to find campaign members return in no.3 if match then campaign__c custom field += 1 then update contact.

I have tried on a scheduled apex & a batch apex and both seems to be working (yet to complete the whole code) but i not sure which is better to go with. I understand that batch apex is better if were to update more than 50k records, but within my execute method i have selected the campaign members object that would return me more than 50k records as well, is that the right way?

Schedule Apex:

global void execute(SchedulableContext sc ){
    List <Contact> ct = new List<Contact>();
    List <Campaign> cmpgn = new List <Campaign>();
    List <CampaignMember> cpgn_mb = new List<CampaignMember>();
    List<Contact> updateCtcs = new List<Contact>();

    ct = [SELECT ID, NAME, YTD_Email_Campaign__c FROM CONTACT WHERE RecordTypeId =: statics.sgCtcId];
    cmpgn = [SELECT ID, Campaign_Effective_Start_Date__c, Year__c, Channel__c FROM Campaign WHERE Year__c = '2014' and Channel__c = 'Email' and RecordTypeId =: statics.sgCpgId];
    cpgn_mb = [SELECT Id, ContactId From CampaignMember where CampaignId in :cmpgn];

    if(!ct.isEmpty()){
        for(Contact ctc : ct){
            Integer emailCampaign = 0;
            for(CampaignMember cmb : cpgn_mb){
                if(cmb.ContactId == ctc.Id){
                    emailCampaign += 1;
                }
            }

            if (ctc.YTD_Email_Campaign__c != emailCampaign) {
                ctc.YTD_Email_Campaign__c = emailCampaign;
            }

            updateCtcs.add(ctc);
        }
        if(!updateCtcs.isEmpty()){

            Set<Contact> myset = new Set<Contact>();
            List<Contact> result = new List<Contact>();
            myset.addAll(updateCtcs);
            result.addAll(myset);
            update result;
        }
    }
}

Batch Apex

global Database.QueryLocator start(Database.BatchableContext BC)
{
    String rid = statics.sgCtcId;
    String query = 'SELECT Id,Name,YTD_Email_Campaign__c FROM Contact';
    return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<Contact> scope)
{
    List <Campaign> cmpgn = new List <Campaign>();
    List <CampaignMember> cpgn_mb = new List<CampaignMember>();
    List<Contact> updateCtcs = new List<Contact>();

    cmpgn = [SELECT ID, Campaign_Effective_Start_Date__c, Year__c, Channel__c FROM Campaign WHERE Year__c = '2014' and Channel__c = 'Email' and RecordTypeId =: statics.sgCpgId];
    cpgn_mb = [SELECT Id, ContactId From CampaignMember where CampaignId in :cmpgn];
    System.debug('cmpgn >'  + cmpgn);
    System.debug('cpgn_mb >'  + cpgn_mb);
    System.debug('scope >'  + scope);
    for(Contact ctc : scope){

        System.debug('ctc >'  + ctc);
        Integer emailCampaign = 0;
            for(CampaignMember cmb : cpgn_mb){
                if(cmb.ContactId == ctc.Id){
                    emailCampaign += 1;
                }
            }

            if (ctc.YTD_Email_Campaign__c != emailCampaign) {
                ctc.YTD_Email_Campaign__c = emailCampaign;
            }

            updateCtcs.add(ctc);

    }
    System.debug('updatectcs >>' + updateCtcs);
    List<Database.SaveResult> r4 = Database.update(updateCtcs, false);
    for( Database.Saveresult r : r4 ){
        if ( r.isSuccess()){
            subtotalUpdCst_actual = subtotalUpdCst_actual + 1;
            System.debug('Database.Saveresult >> ' + r);
        }else{
            for ( Database.Error e : r.getErrors() ){
                System.debug ( updateCtcs + '\n' + e.getStatusCode() + ': ' + e.getMessage());
            } 
        }
    }
}   
global void finish(Database.BatchableContext BC)
{
}
2
  • What have you tried so far? Is something not working? Maybe you could share the code.
    – pchittum
    Commented Dec 1, 2014 at 9:43
  • i just updated with my test code above, please comment, thx! Commented Dec 2, 2014 at 1:53

0

You must log in to answer this question.

Browse other questions tagged .