Increasing Code Coverage to Include Catch Blocks [Salesforce]

In Salesforce, it can sometimes be frustrating driving your Apex Class & Trigger Code Coverage up to get beyond the 75%, 80%, or 85%+ marks.  Some of the more difficult parts of that process can be covering all scenarios to achieve this. In particular, this post focuses on code not executed within the try-catch constructs count against the overall Code Coverage calculations. With a simple refactor, we can remedy that without changing the underlying functionality of the core class — just the test class. The try-catch we will start with before our changes have the following format: try { // … Read More

Leveraging the Power of Salesforce External Objects

Here at M&S we are working on a ground-breaking new project containing multiple data sources being brought into one Salesforce org and displayed using Visualforce with Bootstrap CSS. A crucial and challenging area of our client request involved not replicating data from external enterprise systems by storing it in a Salesforce Object (neither Standard nor Custom). This led us to the use of Salesforce External Objects. External objects are similar to custom objects, except that they map to data that’s stored outside your Salesforce organization. Each external object relies on an external data source definition to connect with the external … Read More

Assigning Group User from Apex

Assigning Group User from Apex

Public Groups in serve as a useful tool to share content in an environment with multiple users. In our projects at M&S, we have used public groups in a variety of capacities including assigning users to records based on record criteria. In this post, we’d like to show you the benefit of using public groups for users outside of the traditional Salesforce assignment. One useful aspect of Public Groups is that you can manipulate the group without effecting custom code. For example: If you have a line of code similar to the following: String userId = UserInfo.getUserId(); User u … Read More

Sending Emails from Apex

Sending Emails from Apex in Salesforce

Salesforce allows administrators to send emails based on workflow actions that are triggered within the environment. You can perform the same functionality utilizing Apex trigger code after you set your trigger logic. In this example we are going to send an email to an Account Owner once an Opportunity has been created for their Account. We will cover the following functionality: Querying the Account object for the Owner Querying for the required Email Template Establishing the email Sending the email   //list for emails List emails = new List(); //query email template object EmailTemplate et=[Select id from EmailTemplate where name=:’Hello … Read More Code Optimization

Rule #1: When developing custom Apex code in, it is not enough to simply create a working trigger to fit your specific scenario, you must also take into consideration bulk data loading. If your trigger cannot handle loading multiple records, you will hit the error [message_box title=”Error:” color=”red”]System.LimitException: Too many SOQL queries: 101[/message_box] This error is typically caused by having a SOQL query within a for loop.  To remedy this,  you must add  your SOQL query before your for loop. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Set<Id> projIds … Read More

Keep Your Versions Straight

Keep Your Versions Straight

Winter is just around the corner, and so is the next version of! Currently if you are using, and have multiple instances such as a production and sandbox environment you may be running two different  API versions. The sandbox may be at version 29.0 which is winter ’14, when production is running 28.0 Summer ’13. This can cause issues with change sets that contain items created in the 29.0 version environment. Production environments will see the new version in October. Below is a handy link that can help you identify when your version will see Winter ’14. … Read More