You are on page 1of 4

Batch Apex Scenarios

1) Write a batch Apex program to update account name in the account


object with suffix ‘Mr’ before the account name.

2) Write a batch Apex program to phone number in the contact object with
the phone number in the corresponding account object where contact is
child of account.

3)
a) Create a visualforce page with input text button .
b) When we click on the replace button in custom visualforce page.
c) Fetch all the records in customer object with the customer name
matching with name given in the visualforce page.
d) Update their account type as a „savings’ account.

4) considerate we record of 1258 expecting the field in OLX update by 115 and
it is updating regularly rightly while I run the batch for database not
execute chocolate where is if I split the same day job batch Enfield 858 le 2
laddu I used best class for scenario where I had to send a email to all
contact owner 15 days before payment due date there where mm of
contract night to at one time you could be many contract owners meeting
this criteria also part of from sending email id to update of lakh on
contract so I schedule a daily nightly batch job for this

5) I have batch apex code updating two field in opportunity line item(OLI).
While I run the code for complete batch the value in a field are updating
correctly. However if I split the batch Apex in tow or many. I am facing the
issue of the large value getting updated on opportunity line item Instead of
complete one.

Consider three records of value 50. I am expecting the field in


opportunity line item(OLI) to be update by 150 and it is updating rightly
wear while I run the batch for database.executebatch(job,3) where as if I
split same the database.executebatch(job,2) then the field getting update
by 50 instead of 150

6) I used Batch class for scenario where I had to send a mail to all Contract
owners 15 days before payment due date. There were 1000 of contract had
at one time There Could be many contractorowners meeting this criteria.
also apart from Sending email I had to update flag on a contract. so I
schedule daily nightly batch job for this.

7) Let us consider an example of our existing Chemical Company and assume


that we have requirement to update the Customer Status and Customer
Description field of Customer Records which have been marked as Active
and which have created Date as today. This should be done on daily basis
and an email should be sent to a User about the status of the Batch
Processing. Update the Customer Status as 'Processed' and Customer
Description as 'Updated Via Batch Job'.

1|Page
Batch Apex Scenarios
8)

Business Use Case


An organization’s sales team is using Salesforce CRM primarily to manage
their Leads, Contacts, and opportunities. This is akin to a retail setting
whereby there are multiple physical locations and each location has its own
sales team and sales manager (think car dealership). In this setting, Leads
(and/or Contacts) enter through multiple channels such as internet marketing,
phone calls, walking through the front door, etc and are assigned to a sales
rep based on a round-robin format. That sales rep becomes the owner of the
Lead, but only has protection for a certain period of time before the Lead is
redistributed. The protected period of time is based on the sales rep’s last
activity to the Lead/Contact. In other words, if the rep is not actively working
the Lead/Contact then they lose protection, and if that Lead/Contact ends up
buying a product then the rep will not be credited for the commission.

The question then became, how can we automate this process of removing
Lead protection based on the sales rep’s activity? One of the great features
of working with the Force.com platform is that there are always multiple ways
to solve these types of problems. And these are good problems to solve
because the solutions free up people’s time and help advance business. In
this situation, the basic design for this scenario is that when the number of
days since the last activity is greater than 5 days, revert the ownership of the
Lead/Contact from the sales rep to the sales manager. This lets the sales
manager redistribute the Lead/Contact to another sales rep.

Make It Happen
The first step was to create a new formula field that returns a Number and call
it Days_Since_ Last_Activity. The formula is this: Today() – LastActivityDate. At
this point, you might think about creating a Workflow Rule that says when
Days_Since_Last_Activity is greater or equal to 5 then Update the Owner
field. However, a Workflow Rule is only fired under three circumstances: a)Only
when a record is created; b) Every time a record is created or edited or; c)
When a record is edited and did not previously meet the criteria. This means
that the workflow rule won’t get fired until the record gets edited, and we want
the ownership to change immediately upon the passing of the protected
period regardless of whether or not someone edits the record.

The solution in this case is to use Batch Apex to query the database for all
Leads/Contacts (we use Contacts in the example code) that have crossed the
protected period, i.e. the Days_Since_Last_Activity fields is greater than
5. Then, we reassign ownership to the correct sales manager based on which
physical location this Contact is associated with. Next, we create a new Task
associated with this Contact so that the Days_Since_Last_Activity gets
reset. Lastly, we schedule this Batch Apex to run each night so that the
ownership is being recalculated on a daily basis.

2|Page
Batch Apex Scenarios
9)

Now we have a limit of using “sendEmail” in apex and it is 10 times. Isn’t this
bad??

Now when I am dealing with anything in bulk first thing comes to my mind
is Batch Class. Now what is the scenario which leads me to send more than 10
emails?? I have 15 system administrators in my organization and each logs 2-3
cases in a day. Now I want to send an email which contains details of case
logged by each system administrator separately, means To address
and Body of each email will be different.

10)

We have a business process to execute batch inserts/updates and Searches.


Here is the scenario, We have a community Portal where external users will
select List of records (custom records) based on a certain logic. This list may
have up to 100-500 records.

And based on their selection, we have to process them. Like create Accounts,
Contacts and create set of Custom records, send email alerts etc.. + Searches.
This process is significantly heavy. And after inserting some of above custom
records, there are triggers + Apex classes to run too. This will simply exceeds
most of the governor limits.

Apparently this community was in a Client side application previously, where


we used SOAP API to handle the Process. In that way we didn't have to bother
about governor limits.
So I seek the help from yours to understand the best option to follow to
implement this. Few options we are considering right now is,

1. Using Batch Apex


2. Using BulkTK - BulkTK
3. Using @future
4. Use Apex REST?

11)
The ask or the requirement: A partner account should be deactivated if any of
its contacts fail to log monthly time-sheet within time in salesforce. Consider
the time-sheet be filled in a custom object which is child of contact. (This
scenario will also demonstrate how to use batch apex to update records in
salesforce)

This requirement can be met by implementing a batch apex that queries all
the partner accounts in the start method of batch apex. Then use batch apex
execute method to update the records (accounts) that is deactivate the
account if there are any contacts that haven't submitted the time-sheet for
previously month. Consider the batch apex size set at one, the execute
method will receive 1 account for each execution. In execute method query
contacts or each of the account and check if the time-sheet was submitted or
not. If any of the contacts are found to be not complying with the time-sheet
i.e if any of the contacts do not have time-sheet record logged for last moth

3|Page
Batch Apex Scenarios
then mark that account for deactivation.

12)
The ask or the requirement : For all the accounts that do not qualify for the
business extension for next month make sure their contacts are invited for a
meeting to discuss the next steps. So basically automatically create meeting
or the event for all primary contacts of such accounts on every starting of
month. This scenario will help in understanding how to use batch apex to
insert records.

Solution: Write a batch apex with a satrt method that queries all the accounts
that do not qualify the criteria of extensions for next month. The pass each of
the account to execute method. Write logic to query each of the contacts of
the accounsa nd insert a aciity recird or the event record and set the subject
as meeting and all other fields and insert the record.

13)
The requirement or the ask : All the partner accounts that have remained
deactivated for more than 6 months should be deleted from the system. This
will example scenario will also demo the use of delete records in batch apex.

Solution: Query all the accounts in start method with a where clause that uses
the the custom field that store the deactivation date. Where clause will help in
querying all such accounts that have deactivation date greater six months. In
the execute method simply use delete dml to delete such accounts.

14) Scenario: Create a batch process on Accounts, capture the failures in a


CSV file and send an email to the batch job creator.
Note: Maximum size for email attachment is 3MB.

15) Run Schedule a Class In Every 5 Mins in Salesforce

4|Page

You might also like