Getting started with Box for Salesforce development: Part 2

Alex Novotny
Box Developer Blog
Published in
5 min readNov 8, 2022
Box for Salesforce

Welcome to part 2 on customizing the Box for Salesforce composite app. If you haven’t read through part 1, you’ll want to go back and do that now.

In today’s blog, we are going to go over changing up the previous code snippets and adding test classes. If you remember, last time we added an APEX class and trigger that changed the default managed package behavior from creating folders under each individual object type to consolidating all folders under an account. However, a user still needed to click the create folder button.

Let’s change that!

Solution Description

Before making any changes, let’s discuss why we see developers remove the create folder button and the ramifications of doing so.

Create Folder button

To create a more seamless experience for users, lots of Box customers ask to remove the create folder button. Anytime an account or opportunity is created or updated (to backfill the objects created prior to the code changes) a folder is automatically created.

To remove the button, we will want to edit our previous APEX class and add two new triggers.

A word of caution before we get into making changes — In terms of scalability, removing the create folder button can hit some Box and Salesforce API limits. If you have a large organization and expect several thousand records, you will want to create a process to clean up older folders and collaborations. I will not be going over that process here, but it is something to plan when implementing your solution. Our friends at Box Consulting can also help with advising on best practices.

You can find the finished code in the box-salesforce-demo-102 folder of the repository we used in part 1 or you can follow along!

Edit APEX Class

The class we wrote in part 1 can be extended to automatically create the account and opportunity folders by adding a new method called createRecordFolders. This accepts a list of records IDs from the triggers we will create next. For each ID, it creates a folder if one doesn’t already exist.

If you open the RecordFolderHandler class from part 1 in Visual Studio code, you can copy/paste the new method below into the file and save it.

Part 1 RecordFolderHandler class
New Record Folder Handler class

Add APEX Triggers

In order to watch for account and opportunity records being created or updated, we need two triggers to call the new method in our APEX class.

You can create these triggers in the terminal of Visual Studio Code using the SFDX CLI. This will create the trigger code file and the respective xml metadata file.

This command creates a trigger called AccountTrigger which will watch all account sObjects for the events after insert and after update. It places the new files in the path identified.

sfdx force:apex:trigger:create -n AccountTrigger -s Account -e 'after insert,after update' -d <PATH TO YOUR TRIGGERS FOLDER>

This command creates a trigger called OpportunityTrigger which will watch all opportunity sObjects for the events after insert and after update. It places the new files in the path identified.

sfdx force:apex:trigger:create -n OpportunityTrigger -s Opportunity -e 'after insert,after update' -d <PATH TO YOUR TRIGGERS FOLDER>
SFDX CLI trigger creation commands

Each trigger should contain the following code.

Account trigger
Opportunity trigger

Test Classes

Creating tests within your solution is vital to make sure your code is doing what you think it is doing. Salesforce has documented testing and code coverage requirements — specifically 75% of code deployed to production must have a working test class. While the above examples were deployed to a sandbox environment, let’s still write test classes for them.

So far, we’ve created a class that does two things: creates account and opportunity folders automatically after they are created or updated and moves opportunity folders under the account folder. We’ve also created three triggers: two that watch for accounts and opportunities to be created or updated and one that watches for records to be added the the FRUP table.

All of the above should be tested.

This command will create the FRUP trigger test APEX class. It tests inserting a new FRUP record.

sfdx force:apex:class:create -n FRUPTriggerTest -d <PATH TO YOUR CLASSES FOLDER>

Copy/paste the below code into the new file and save it.

FRUP trigger test class

This command will create the account trigger test APEX class. It tests both inserting and updating an account record.

sfdx force:apex:class:create -n AccountTriggerTest -d <PATH TO YOUR CLASSES FOLDER>

Copy/paste the below code into the new file and save it.

Account trigger test class

This command will create the opportunity trigger test APEX class. It tests both inserting and updating an opportunity record.

sfdx force:apex:class:create -n OpportunityTriggerTest -d <PATH TO YOUR CLASSES FOLDER>

Copy/paste the below code into the new file and save it.

Opportunity trigger test class

This command will create the record folder handler class test APEX class. It tests creating a new account object’s folder, as well as moving an opportunity folder under an account’s main folder.

sfdx force:apex:class:create -n RecordFolderHandlerTest -d <PATH TO YOUR CLASSES FOLDER>

Copy/paste the below code into the new file and save it.

Record folder handler class

To run the tests, we need to deploy our code!

Deploy

Once all of the above code is written, we can deploy our changes by running the deploy command in the terminal window of Visual Studio Code.

sfdx force:source:deploy -m ApexClass,ApexTrigger

If successful, you will see the below.

Deploy Successful

To run our tests, we run the following command.

sfdx force:apex:test:run --testlevel RunLocalTests

This will generate the subsequent command, which if ran should show you the following successful test screen.

sfdx force:apex:test:report -i <TEST ID> -u <YOUR USER>
Run tests in Visual Studio Code

Now, we can test out the changes live by creating a new account and new opportunity. After either object is created, you’ll see a folder automatically is created — and the opportunity folder still moves under the account folder!

Automatically created account folder

Additional Resources

List of Box for Salesforce Developer Toolkit Methods

Admin configuration guide

Creating the Box Sign button in Salesforce guide

APEX code samples

FAQs

…And as always, feel free to post questions on our developer forum.

Special thanks to Kyle Adams, nicholas read, and Randy Johnston!

Black’s Beach in San Diego

--

--

Alex Novotny
Box Developer Blog

I’m a Box Developer Advocate, helping others learn how to maximize their investment through Box Platform.