Entries in Salesforce Tips (14)


Three Common Apex Coding Topics

If you're an admin of a big Salesforce org, then you probably had some users complain about unfriendly errors. If your Salesforce org is in any way customized then those errors are likely related to performance issues in the custom Apex code. Although custom Apex code can be powerful, it can sometimes hinder the overall performance of your Salesforce org. Today, I'm going to introduce three changes you can make in Apex Code that will improve the performance of your Salesforce:


Enhance Validation and Workflow Rules:

One example of the errors your users might get is one that includes FIELD_CUSTOM_VALIDATION_EXCEPTION. This error usually means that you have a scheduled batch that inserts/updates records without fulfilling a Validation Rule(s). Here are a few areas you can improve performance:

  • If you have a batch class, make the class calls a DML operation and that opt_allOrNone is set to False. This will allow the rest of the operation to still succeed even if the record fails.
  • Workflow Rules are most helpful in preventing duplicate records. If you have a lot of Workflow Rules in your org, you can cut down unnecessary processes by deactivating the Workflow and improving the Trigger to do the Workflow Rule'sjob.
  • Review Validation Rules with management and make sure they are necessary to fulfill business logic. Having too many validation rules and required fields can cause unit tests to fail, which will then prevent you from deploying a change set.

Increase Field Length:

If, for example, you have a Trigger that populates values larger than the field length, then you'll most likely get a STRING_TOO_LONG error message. One thing you can do here is to increase the Field Length; it is better to have a large value than getting an error message.


Rectify API Limits:

If your org has a lot of integrations with third party apps and that those integrations are used frequently, your users might then see a "REQUEST_LIMIT_EXCEEDED" error. There's no ideal solution to reducing API usage, but one thing you can do is to evaluate the current API usage in your org (SetupAdministration SetupCompany ProfileCompany Information) and figure out a way to reduce and disperse the number of API calls.


It is always a good idea to periodically review the current custom Apex Code you have in your org with management and stakeholders. Making these changes will not only cut down the number of errors your users encounter, but will help improve the performance and the overall user-experience in your Salesforce.


Standardize Countries and States in Salesforce

Often times importing new data into your Salesforce org will result in having inconsistent data. This inconsistency is particularly found in Countries and States/Provinces, such as the values (US, Usa, us, usa) where they should be updated to United States, instead. Today, I'm going to give a tutorial on how to use Salesforce's Mass Update Addresses; to standardize your countries and states.

Standardizing Countries:

Initially, you want to update the countries first then the states. To get started:

  • Go to your name> Setup> and search for "Mass Update Addresses" or go to Administration Setup> Data Management> Mass Update Addresses
  • Select "Country", then click Next.

  • From the "Available Values" column, select the countries you want standardized (this view can display a maximum of 100 countries, if you can't find the countries you want updated, search for them in the above search box) and add them to the "Selected Values". Then, enter in the new name you want applied to the selected countries and click Next.

  • After that, you'll be shown a list of where records of those countries are found. Click "Replace" to replace all those countries with the new one.

Standardizing States:

Now that you've updated the countries in your org, standardize the states in those countries:

  • In Step 1, select "State/Province" and enter in the country in which the states belong to and click Next. 

  • Pick the states you want updated and enter in the new name and click Next. Then, review where those states are found in your data and click Replace to replace them with the new name.

Tips on Mass Updating Addresses in Salesforce:

  • Always standardize the countries before the states.
  • Keep in mind that once you've updated the values of countries and states, all associated filter conditions will be affected and that includes: assignment rules and web-to-lead, which then should be updated with the new values.

Salesforce: Create Your Own App Marketplace

Last month, Salesforce announced Private AppExchange-- a feature that lets managers create a private app marketplace with a specific selection of apps totally private to their organization.

Click to read more ...


Salesforce Page Layout

In SalesforcePage Layouts allow for customization of UI pages for users in your organization. And often times throughout, you have pages of objects that might contain items that are not used by many users. Today, I'm going to explain how you can customize a Salesforce page layout with the fields, related lists, and custom links that suit your organization's use.

In the example below, I'll change the layout of the page Contracts. Note that you can "Edit" the current page layout of that object, but it's best to create a new Contract Page Layout and then assign it the default one.

  • To get started, go to Your NameSetupCustomizeContractPage Layout> Click on New.

  • Fill in the appropriate information and click "Save".

  • Next, drag and drop the fields, buttons, and related lists you want on the Contract Page Layout.

  • Then, click "Save".

Assigning Page Layouts

Now that you've created the page layout, you can assign it to the appropriate user Profiles in your Salesforce org.

  • Go back to the "Contract Page Layout" and click "Page Layout Assignment".

  • Pick the profile and assign the appropriate Page Layout. Then, click "Save".


Bulk Update Records in Salesforce

Often times in Salesforce, you need to update data on a mass scale. And there are many ways to do that- perhaps the most efficient way to mass update records is DataLoader. Alternatively, you can choose to Mass Edit/Update data directly on interface. Today, I'm going to introduce a simple and intuitive way to Mass Update/Edit from List view in Salesforce.

This feature in Salesforce comes as an app that you can install from the AppExchange, part of the Salesforce Labs: a series of Free Salesforce apps made by Salesforce's employees and developers. To get started, find the "Mass Update and Mass Edit From List View" app on the AppExchange and follow instructions to install and deploy it.


Enable Buttons in Search Layout:

With this app, you're able to mass update records of virtually any object. But first, you have to enable it in the object Search Layout. For this tutorial, I'll be using Leads as my object. Go to: Setup> App Setup> Customize> Leads> Search Layouts. Click "Edit" next to "Leads List View". Scroll down under Custom Buttons, and add the two buttons: "Mass Edit" and "Mass Update". Don't forget to click Save.


Mass Update Your Records with a Few Clicks:

After you've enabled the buttons in Leads, you can see them right in the Search Layout of Leads. You have two ways mass changing data in your records:

  • Mass Edit: this button allows you to change values of fields already displayed in the list view: select the records you want to mass edit, and click "Mass Edit".


Fill in the new values and hit Save.



  • Alternatively, you can Mass Update your records: this allows you to change the value of any fields of the object. Select the records you want to update and click "Mass Update". You'll be walked through a quick wizard:



Confirm the records you want to update, then click "Next":



Then, pick the field you want to change, then hit "Next"


Enter the new value of the field and click "Next" then confirm the changes by clicking "Update" then "Done".



This app works great for mass updating a relatively large number of records. It is free and can be used in any object in Salesforce.


Mass Delete Records in Salesforce 

Up until recently, mass deletion of records wasn't even available in Salesforce, but thankfully the developers have finally added this feature. Today in this tutorial, we're going to learn about Mass Deletion in Salesforce.

Before we begin, I must point out that although it is helpful, this feature comes with some limitations. Here's a few limitations to consider:

  • A maximum of 250 records can be deleted at once
  • You must have the permission "Modify All Data"
  • If you're not the admin of your Salesforce org, then you may not be able to delete records with associated cases ; the same goes for Partner Accounts.

Step 1: Click Your Name> Setup> Data Management> Mass Delete Records 

Step 2: Pick the record type you wish to mass delete. Then examine the data you're looking to delete; remember that this is generally undo-able, so pick wisely.

Step 3: Specify conditions/filters for the times selected. For example, "State = Texas" or "City = Dallas"

Step 4: You may check the options to delete the appropriate records: archived records, accounts with other attached accounts.. etc

Step 5: Choose "Search" to find the matches based on the conditions 

Step 6: Select the boxed next to the record you want to delete; alternatively, you can click on the header to select all.

Step 7: If needed, you may select the "Permanently Delete" records.

Step 8: Click "Delete".

And that is it! Note that the mass delete option may not be visible to some profiles. In which case, admins can do the job.


Including Archived and Deleted Records in your SOQL Query

In some instances when performing a SOQL query, some records are omitted from appearing in the results. This includes deleted and archived records. For example, Tasks more than a year old (365 days) are automatically omitted unless the user specifies otherwise and that's where the  "ALL ROWS fuction" comes in handy.

If you add ALL ROWS at the end of a SOQL query, archived and deleted records will be included in the results. Here's an example below of query returning ALL tasks:


As you can probably imagine, pulling all deleted and archived records into the results could a be hard to using the "IsDeleted" and "IsArchived" after the "WHERE" clause can help filter the results:

SELECT Id FROM Task ALL ROWS WHERE IsDeleted = false AND IsArchived = false

Salesforce: Bypass Rules and Triggers

Having Validation Rules in your Salaesforce org is a great way to maintain your data. However, I can think of more than one scenario where Validation Rules, Triggers, and Workflow Rules can get in a way of things. I can tell you there have been times where I wished I could disable the required field validation rule when trying to upload new data into my salesforce.

What comes to our rescue here is the Hierarchical Custom Setting. There, you can basically set values at the User level, Profile level, and the Organization as a whole. When configured, the custom settings will first to look if there had been any values set at the User level, otherwise will move up the hierarchy to Profile, then Organization.


In the screenshot above, you can see the Custom Setting I set up (App Setup> Develop> Custom Settings):

  • Checkboxes to disable Workflows, Validation Rules, and Triggers
  • A text-field named "Trigger Objects" with "Lead;Account;Contact:Opportunity" as the value.
  • The Profiles the custom settings should effect


After the Custom Setting is set up, it can then be referenced in workflows, triggers, and validation rules:

For Validation and Worflow Rules, use the syntax below on if/when the triggers should fire off:

As for triggers, you can retrieve the Custom Setting details then use an IF statement for when it should fire off:
trigger LeadTrigger on Lead (after insert, after update) {
    disableReqField__c profileCustomSetting = disableReqField__c.getInstance(UserInfo.getUserId());
            && profileCustomSetting.DisableTriggers__c)){

With Hierarchical Custom Setting, Triggers and Validation/Workflow rules can now be overridden without having to temporarily disable them for the entire org.

APEX: Retrieve a Custom Object Field's History

Let's say you have the field "Address" in a custom object labeled "Customer" and you're looking to obtain that particular field's history and tracking details along with the dates on which changes were made. Today, I'll share a query that will help you do just that:


SELECT ParentId, OldValue, NewValue, Field, CreatedById, CreatedDate FROM Customer_History where parentId = :CustomerId and Field ='Address__c'