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 

Help Creating 2 Batch

I have a lookup field to Region on Contact I want the batch to run every midnight. the batch updates the region if the field postcode on contact is changed and the region override is false.

Contact Fields:

Region Override = checkbox

Region(Lookup)

any idea on how to make it? The output for example is like when a contact is created and the postcode has been filled, the batch will update the region overnight.

Best Answer chosen by Anonymous Developer
AnkaiahAnkaiah (Salesforce Developers) 
Hi,

try with below code.
global class BatchUpdateRegiononcontact implements Database.Batchable <sObject> {

    List<Contact> conlist = new List<Contact>();
    
    global Database.QueryLocator start(Database.BatchableContext bc) {
//here you can modify the query as per your need
        String query = 'select id,Name,Region_Override__c,Region__c from contact where postcode__c!=Null';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc,List<Contact> batch) {
        system.debug('batch=='+batch);

        set<sting> postcodes = new set<id>();
        for (Contact a : batch) {
               postcodes.add(a.postcode__c);
            }
        map<string,id> maprgcode = new map<string,id>();
       for(Region__c rg: [select id,postcode__c, from Region__c where postcode__c=:postcodes]){
	   maprgcode.put(rg.postcode__c,rg.id);
	   }
        for(Contact con:batch){
            if(maprgcode.containskey(con.postcode__c)){               
                con.Region__C = maprgcode.get(con.postcode__c);
				con.Region_Override__c = true;
               conlist.add(con);
                
            }
               
            }
        update conlist;
        
        system.debug('conlist=='+conlist);
    }
    
    global void finish(Database.BatchableContext bc) {
        //Do Nothing.
    }
}

Schedule Apex:
global class BatchUpdateRegiononcontactSchedule implements Schedulable {
   global void execute(SchedulableContext SC) {
      BatchUpdateRegiononcontact M = new BatchUpdateRegiononcontact(); 
   }
}

How to schedule a schedule apex class in Salesforce UI

Setup --> apex classes -->  click on Schedule Apex button --> and choose the schedule class(BatchUpdateRegiononcontactSchedule)

User-added image

for instant checking: run the below query in dev console.
BatchUpdateRegiononcontact b = new BatchUpdateRegiononcontact();
database.executeBatch(b);

If this helps, Please mark it as best answer.

Thanks!!

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi,

try with below code.
global class BatchUpdateRegiononcontact implements Database.Batchable <sObject> {

    List<Contact> conlist = new List<Contact>();
    
    global Database.QueryLocator start(Database.BatchableContext bc) {
//here you can modify the query as per your need
        String query = 'select id,Name,Region_Override__c,Region__c from contact where postcode__c!=Null';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc,List<Contact> batch) {
        system.debug('batch=='+batch);

        set<sting> postcodes = new set<id>();
        for (Contact a : batch) {
               postcodes.add(a.postcode__c);
            }
        map<string,id> maprgcode = new map<string,id>();
       for(Region__c rg: [select id,postcode__c, from Region__c where postcode__c=:postcodes]){
	   maprgcode.put(rg.postcode__c,rg.id);
	   }
        for(Contact con:batch){
            if(maprgcode.containskey(con.postcode__c)){               
                con.Region__C = maprgcode.get(con.postcode__c);
				con.Region_Override__c = true;
               conlist.add(con);
                
            }
               
            }
        update conlist;
        
        system.debug('conlist=='+conlist);
    }
    
    global void finish(Database.BatchableContext bc) {
        //Do Nothing.
    }
}

Schedule Apex:
global class BatchUpdateRegiononcontactSchedule implements Schedulable {
   global void execute(SchedulableContext SC) {
      BatchUpdateRegiononcontact M = new BatchUpdateRegiononcontact(); 
   }
}

How to schedule a schedule apex class in Salesforce UI

Setup --> apex classes -->  click on Schedule Apex button --> and choose the schedule class(BatchUpdateRegiononcontactSchedule)

User-added image

for instant checking: run the below query in dev console.
BatchUpdateRegiononcontact b = new BatchUpdateRegiononcontact();
database.executeBatch(b);

If this helps, Please mark it as best answer.

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

@Ankaiah the postcode is not inside the contact object but rather on another custom object I created a lookup on contact named region.

Contact Fields:

  • Region (Lookup) to region custom object
  • Region override (checkbox)
Region Fields:
  • Postcode
  • Region Name
so the output would be overnight batch will update region if the postcode is filled or changed and override is false if it's true the batch will not update.
AnkaiahAnkaiah (Salesforce Developers) 
Hi,

I am not clear on your requirement.

What is the datatype of postcode field in Region object?

If postcode changes, What are the fields you need to update in region object?

Thanks!!
Anonymous DeveloperAnonymous Developer

@Ankaiah My bad the postcode field is a text datatype so let's say I change the Mailing City Zip/Postal Code on Contacts User-added image

I want it to map the Zip and show it automatically on the Region Custom Field that I created since the postal code is 4000 it should show the region of BrisbaneUser-added imageif the region override is ticked the region is not updated if it is not ticked the region automatically updates. Does that make sense?

Thanks!!

AnkaiahAnkaiah (Salesforce Developers) 
Hi,

Thanks for the clarification.

You can use my above code and modify below lines.

Replace the query
from
String query = 'select id,Name,Region_Override__c,Region__c from contact where postcode__c!=Null';
to 
String query = 'select id,Name,Region_Override__c,Region__c from contact where postcode__c!=Null AND Region_Override__c=FALSE';

Remove the below line in the execute method.
con.Region_Override__c = true;

Rest of the code follow my 1st comments.

Please let me know if any issues.

Thanks!!

​​​​​​​



 
Anonymous DeveloperAnonymous Developer

nothing will update on the region object its static data but the region lookup on contacts will get the values inside the region object

  • Contact Fields
    • Region(Lookup) to Region field
    • Region override (checkbox)
Anonymous DeveloperAnonymous Developer
@Ankaiah Quick question there were no errors in the code but it does not seem to work or maybe I'm doing something wrong during testing. Can you take a look at this code format, is this the same as your code? If not does this format work?

query Contact
    MailingPostcode
    MailingCity
    Region__c
    Region_Override__c
    WHERE Region__r.Postcode__c != null
    AND Region_Override__c=FALSE 

    batch = list of contact
    create listStringvar (list of Postcodes)
    loop batch , get batch.MailingPostcode, add listStringvar
    query Id, Postcode, Suburb, Name FROM Region  Where postcode in
listStringvar ( Region Var )
    create map <String, List<Region__c>> (String is Postcode, Region is object)
    loop RegionVar
        check if key is already in map, if key exists, add list
        if not, put in regionvar ( Postcode, Region obj )

 
AnkaiahAnkaiah (Salesforce Developers) 
Yes, you can apply same format.

 
Anonymous DeveloperAnonymous Developer

@Ankaiah Hello, does the code above work on yours? mine doesn't seem to work this is the code.

User-added image

Anonymous DeveloperAnonymous Developer
is it because it is missing scheduling on the interval time and systematic method to schedule apex class because I cant seem to make scheduled apex?