You need to sign in to do that
Don't have an account?
Anonymous 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.
try with below code.
Schedule Apex:
How to schedule a schedule apex class in Salesforce UI
Setup --> apex classes --> click on Schedule Apex button --> and choose the schedule class(BatchUpdateRegiononcontactSchedule)
for instant checking: run the below query in dev console.
If this helps, Please mark it as best answer.
Thanks!!
All Answers
try with below code.
Schedule Apex:
How to schedule a schedule apex class in Salesforce UI
Setup --> apex classes --> click on Schedule Apex button --> and choose the schedule class(BatchUpdateRegiononcontactSchedule)
for instant checking: run the below query in dev console.
If this helps, Please mark it as best answer.
Thanks!!
@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.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!!
@Ankaiah My bad the postcode field is a text datatype so let's say I change the Mailing City Zip/Postal Code on Contacts
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 Brisbaneif the region override is ticked the region is not updated if it is not ticked the region automatically updates. Does that make sense?
Thanks!!
Thanks for the clarification.
You can use my above code and modify below lines.
Replace the query
from to
Remove the below line in the execute method.
Rest of the code follow my 1st comments.
Please let me know if any issues.
Thanks!!
nothing will update on the region object its static data but the region lookup on contacts will get the values inside the region object
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 )
@Ankaiah Hello, does the code above work on yours? mine doesn't seem to work this is the code.