function readOnly(count){ }
Don't have an account?
Search for an answer or ask a question of the zone or Customer Support.
You need to sign in to do that
Sign in to start searching questions
Signup for a Developer Edition
Sign in to start a discussion
All Answers
Can you paste the code as the comment or answer so it would be easy for expert to copy paste the code and try a test class for the same.
Thanks,
global class ASOverrideContacRegion implements Database.Batchable<sObject>{
List<Contact> conlist = new List<Contact>();
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT MailingPostalcode,MailingCity,Region_Override__c,Region__c FROM Contact WHERE MailingPostalcode != null AND Region_Override__c = FALSE AND MC4SF__MC_Subscriber__c = null';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Contact> batch) {
System.debug('batch=='+batch);
Set<String> postcodes = new Set<String>();
for (Contact a : batch) {
postcodes.add(a.MailingPostalcode);
}
Map<String,Id> mapregcode = new Map<String,Id>();
for(Region__c reg: [SELECT Id,Postcode__c,Suburb_Name__c FROM Region__c WHERE Postcode__c IN :postcodes]) {
mapregcode.put(reg.Postcode__c,reg.Id);
}
for(Contact con : batch) {
if(mapregcode.containskey(con.MailingPostalcode)) {
Contact conTemp = new Contact();
conTemp.Id = con.Id;
conTemp.Region__c = mapregcode.get(con.MailingPostalcode);
conlist.add(conTemp);
}
}
System.debug('postcodes >>>> ' +postcodes);
System.debug( 'mapregcode >>>>>' +mapregcode );
update conlist;
system.debug('conlist=='+conlist);
}
global void finish(Database.BatchableContext BC) {
}
}
@isTest
public class ASOverrideContacRegionTest {
static testMethod void setup(){
List<Contact> tempConList = new List<Contact>();
Contact con = new Contact();
con.FirstName = 'Erin';
con.LastName = 'Daly';
con.MailingCity = 'Brisbane';
con.MailingPostalCode = '4000';
con.Region_Override__c = TRUE;
tempConList.add(con);
insert tempConList;
Region__c reg = new Region__c();
reg.Suburb_Name__c = 'Brisbane';
reg.Postcode__c = '4000';
insert reg;
Test.startTest();
ASOverrideContacRegion OCR = new ASOverrideContacRegion();
Id jobId = Database.executeBatch(OCR);
Test.stopTest();
}
}