The Salesforce testing process is the same as that of testing web applications, but it has some special things that are slightly different. The tester should have a clear understanding of the custom functions that are created and focus only on these functions.
In this article, I will try to describe the most popular tips and tricks for testing the Salesforce platform to make testing easier and decrease testing time.
Say you need to check some records created with special conditions after the code is triggered. Of course, you can create a custom filter view using Salesforce UI as an instrument to configure it, but you’ll need to spend some time to create it and display needed fields in the list. And, after that, you probably need to delete created custom filters for testing, especially if you have several thousands of records on your org.
The best way is to use query language to display needed records. Its takes several seconds to write a query and you can select any combination of fields you want. Salesforce use SOQL (Salesforce Object Query Language) for that. SOQL is similar to the SELECT statement in the widely used SQL but is designed specifically for Salesforce data. To learn more about using SOQL follow this link.
Simple Example:
You need to check all opportunity records with the stage “closed won” and that are not private.
1 2
SELECT id, Name, StageName, IsPrivate FROM Opportunity WHERE StageName = 'Closed Won' AND IsPrivate = False
Steps:
Go to “Developer Console” and click the “Query Editor” tab
Enter your SOQL query and click the “Execute” button
Also, if you need to quickly delete selected records you add ‘delete’ to the script and execute using the anonymous window.
Simple example:
You need to delete all opportunity records where stage does not equal “closed won”.
Steps:
Go to “Developer Console” and click “Query Editor” tab
Click on “Debug” tab
Select the ”Open Execute Anonymous Window” option or press CTRL+E
Insert script and click “Execute” button
Script:
1
delete[SELECT id, Name, StageName, IsPrivate FROM Opportunity WHERE StageName != 'Closed Won'];
Sometimes when you’re testing some triggers or pages you need a lot of records to check current functionality. There are several ways to resolve this issue:
Use Dataloader.io or Import wizard to import some CSV files with generated data
Create records manually
Execute script for generating test data using anonymous window
The fastest way to generate test data is the last option of using anonymous window. You don’t need to understand or learn Apex language for it, you can just ask your developers to write some scripts and change the data each time you need to create test records. You can also just Google how to write a simple script for generating test data.
Simple Example:
You need to create 100 contacts for testing some functionality.
Steps:
Go to “Developer Console” and click “Query Editor” tab
Click on “Debug” tab
Select ”Open Execute Anonymous Window” option or press CTRL+E
Insert script and click “Execute” button
1
2
3
4
5
List < Contact > conts = new List < Contact > ();
for (Integer a = 0; a < 100; a++) {
conts.add(new Contact(FirstName = 'Test', LastName = 'Test' + a));
}
insert conts;
Batch Apex needs to build complex, long-running processes that run on thousands of records. Batch Apex operates over small batches of records, covering your entire record set and breaking the processing down to manageable chunks. For example, a developer could build a data cleansing operation that goes through all contacts on a nightly basis and updates them if necessary, based on some needed criteria.
Batch jobs can be scheduled to run:
at specific times using the Apex scheduler
using the Schedule Apex page in the Salesforce user interface.
executing batch manually using anonymous window
In our example, we have a batch that updates all contacts with new status according to criteria every Monday. We need to check if our batch works correctly, but we can’t wait until Monday. In our case, we can execute the batch manually using anonymous window, which only takes several seconds.
Steps:
Batch name ContactUpdaterBatch
Go to “Developer Console” and click “Query Editor” tab
Click on “Debug” tab
Select ”Open Execute Anonymous Window” option or press CTRL+E
Insert script and click “Execute” button
1
2
ContactUpdaterBatch a = new ContactUpdaterBatch();
Database.executeBatch(a);
After that you can check the status of the executing job.
Go to Setup
Enter “Job” to the Search field
Select “Apex Jobs”
Those are the main things you can use for quick testing Salesforce functionality and generating test data. There are several reasons to use such tricks:
You will reduce time when testing Salesforce functionality
You do not need to ask developers to create or delete test data, because you can do it yourself.
And finally, simple steps can confirm your professional skills.