Understanding the custom field features

Custom fields carry many more features than you might think; they are much more than simple field definitions you find on other platforms. Having a good understanding of a custom field is the key to reducing the amount of code you have to write and improving the user experience and reporting of your application's data.

Default field values

Adding default values to your fields improves the usability of your application and can reduce the number of fields needed on the screen, as users can remove fields with acceptable defaults from the layouts.

Default values defined on custom fields apply in the native user interfaces and Visualforce UIs (providing the apex:inputField component is used) and in some cases, through the APIs. You can define a default value based on a formula using either literal values and/or variables such as $User, $Organization, and $Setup.

Let's try this out. Create a Year text field on the Season object as per the following screenshot. Make sure that you select the External ID checkbox as this cannot be changed on the field once the package is uploaded at the end of this chapter. This will become important in the next chapter.

Default field values

Tip

The default value you define cannot be overridden through a customization in the subscriber org, however, you can reference a Custom Setting (hierarchy type only) in a default value formula using a $Setup reference. This allows for some customization through editing the Custom Setting at an organization, profile, or individual user level.

To create a record in the Season object, we need to first create a tab for it. Perform the following steps in the package org to try out this process:

  1. Create a tab for the Season object.
  2. Go to the Season tab and click on New.
  3. This results in the current year being displayed in the Year field as shown in the following screenshot:
    Default field values

Note

The preceding screenshot shows the user interface shown by Salesforce when the user is using Salesforce Classic. Salesforce Classic is the predecessor to their latest user interface technology known as Lightning Experience. Both are options for users and will likely remain so for some time until users and applications are migrated over. This book also includes information on Lightning throughout its chapters, as well as a dedicated Chapter 10, Lightning.

Unfortunately, by default, the Apex code does not apply default values when you construct an SObject in the direct manner via new Season(); however, if you utilize the SObjectType.newSObject method, you can request for defaults to be applied as follows:

Season__c season = (Season__c)
     Season__c.SObjectType.newSObject(null, true);
System.assertEquals('2016', season.Year__c);

Encrypted fields

However unlikely you feel data theft might be, there are some markets and government regulations that require encrypted fields. Salesforce offers two ways to help ensure your application data is stored in their data centers in an encrypted form. There are two options to consider for encrypting field values at rest, that is records physically stored on permanent storage.

  • Classic Encryption: Encrypted fields leverage 128-bit master keys and use the Advanced Encryption Standard (AES) algorithm to store values; they are displayed using a character mask (currently not developer definable). Such fields are packagable, though this only applies to Text fields.
  • Platform Encryption: Customers that have purchased the Salesforce Shield add-on can enable 256-bit AES encryption for certain Standard fields and Custom fields of their choosing. It can be applied to E-mail, Phone, Text, Text Area, URL, Date and Date/Time fields. While the encryption level is higher this facility does come with some significant restrictions, these are covered in the next section.

Encrypted field values are visible to those who have the View Encrypted Data permission, which is a facility that may not be something your requirements tolerate. This also includes users whom you grant the login access to, such as through subscriber support. Apex, Visualforce Page, Lightning Components and Validation Rule logic you package can see the unencrypted values, so extra care is needed by developers if you want to mask field values. If your only concern is compliance with encryption at rest, this may not be such an issue for you.

Special considerations for Platform Encryption

Salesforce recommend that you only consider this feature if government regulations for your market require it. When developing a packaged solution, your main consideration is what happens if a subscriber wishes to enable it on a Standard field you reference in your solution or on one of your packaged fields.

Tip

For those that do want to promote support for this feature through their AppExchange listing, there is an indicator that can be applied to your listing by Salesforce to show prospective customers that you support Platform Encryption.

When implementing the 256-bit encryption used by Platform Encryption, Salesforce opted for probabilistic encryption over deterministic encryption. Since the encryption is not deterministic (encrypted value is the same each time), it is not possible to index fields that have this level of encryption applied. This results in the most significant of restrictions to developers and platform functionality around querying the field.

Without an index, the platform cannot provide a performant way to allow code to filter, group or order by encrypted fields in queries. If code attempts to do so at runtime, an exception will occur. Your package installation may be blocked if it detects queries inside Apex code that would not be compatible with this restriction. Conversely once your package is installed and the user attempts to enable encryption, this action may fail. Salesforce provides a report in both cases to inform the user which fields are problematic.

It is possible to work around these restrictions conditionally, by using Dynamic SOQL to allow runtime formation and the execution of effected queries where encryption is not enabled. In cases where encryption is enabled you can perform filtering, grouping and ordering in Apex and/or via Salesforce Object Search Language (SOSL). Though both these options should be considered carefully as they have their own restrictions and may not be the most performant. You may need to accept that you have to selectively hide functionality in your solution in some cases. The decision is something you have to make in conjunction with your users as to how acceptable a trade-off this would be.

Tip

Regardless of if you plan to support Platform Encryption or not, it may still be an important need for your enterprise customers where your application must co-exists with other Salesforce products and other Partner solutions. I would recommend that you prepare a statement to set expectations early on what level of support you provide, such as which standard fields and package fields are supported. Make sure your prospects can make informed decisions, otherwise they may dismiss your application from their shortlists unnecessarily.

I highly recommend that you leverage the Education section in the Partner Success Community. Under the MORE RESOURCES section you will find a dedicated page with videos and other resources dedicated to ISVs.

Lookup options, filters, and layouts

The standard lookup UI from Salesforce can be configured to make it easier for users to recognize the correct record to select, filter out those that are not relevant, and if necessary, prevent them from deleting records that are in use elsewhere in your application, all without writing any code.

Perform the following steps in the package org to try out this process:

  1. Create Tabs for the Driver and Race objects.
  2. Add an Active checkbox field to the Driver Custom Object; ensure that the default is checked.
  3. Create a driver record, for example, Lewis Hamilton.
  4. From the Season record created in the previous section, create a Race record, for example, Spa.
  5. Associate Lewis Hamilton with the Spa race via the Contestants relationship.

First, let's ensure that drivers cannot be deleted if they are associated with a Contestant record (meaning they have or are taking part in a race). Edit the Driver field on the Contestant object and enable Don't allow deletion of the lookup record that's part of a lookup relationship. This is similar to expressing a referential integrity rule in other database platforms.

Lookup options, filters, and layouts

Now if you try to delete Lewis Hamilton, you should receive a message as shown in the following screenshot. This also applies if you attempt to delete via Apex DML or the Salesforce API.

Lookup options, filters, and layouts

You've just saved yourself some Apex Trigger code and used a platform feature.

Let's take a look at another lookup feature, known as lookup filters. Formula 1 drivers come and go all the time; let's ensure when selecting drivers to be in races via the Driver lookup on the Contestant record, that only the active drivers are shown and are permitted when entered directly.

Perform the following steps in the packaging org to try out the process:

  1. Create a Driver record, for example, Rubens Barrichello and ensure that the Active field is unchecked.
  2. Edit the Driver field on the Contestant object and complete the Lookup Filter section as shown in the following screenshot, entering the suggested messages in the If it doesn't, display this error message on save and Add this informational message to the lookup window fields, and click on Save:
    Lookup options, filters, and layouts

You can have up to five active lookup filters per object. This is not a limit that is set by your package namespace. So, use them sparingly to give your end users room to create their own lookup filters. If you don't have a strong need for filtering, consider implementing it as an Apex Trigger validation.

Test the filter by attempting to create a Contestant child record for the Spa record. First, by using the lookup dialog, you will see the informational message entered previously and will be unable to select Rubens Barrichello, as shown in the following screenshot:

Lookup options, filters, and layouts

If you attempt to enter an inactive driver's name directly, this is also blocked as shown in the following screenshot:

Lookup options, filters, and layouts

This validation is also enforced through Apex DML and Salesforce APIs. Unlike Validation Rules, it cannot be disabled by the end user. However, changes related to records, such as making the Driver subsequently inactive, are permitted by the platform. In this case, the next time the Contestant record is edited, the error given in the preceding screenshot will occur.

Finally, you are also able to display additional columns on the lookup dialogs, which help the user identify the correct record based on other fields. The following steps describe how to do this:

  1. Add a Nationality field to the Driver object of the Picklist type, and enter the following values: British, Spanish, Brazilian, Finnish, and Mexican.
  2. Edit the existing Driver records and set an appropriate value in the Nationality field.
  3. Locate the Search Layouts section of the Driver object, edit the Lookup Dialogs layout, add the Nationality field created in step 1, and save the layout. When selecting a driver, the lookup should now show this additional field, as follows:
    Lookup options, filters, and layouts

Lookups are a hugely powerful declarative feature of the platform; they are not just used to express the application data model relationships, but they actually enforce the integrity of the data as well, thereby making the user's job of entering data much easier.

Rollup summaries and limits

When it comes to performing calculations on your data, the platform provides a number of options: reports, Analytics API, dashboards, Apex Triggers, Visualforce, and Rollup summaries. The Rollup summary option provides a code-free solution with the ability to apply some condition filtering. Rollup summary fields can then also be referenced and combined with other features such as formula fields and Validation Rules.

The key requirement is that there is a Master-Detail relationship between the detail records being summarized on the field being added to the master records, such as that between Race and Contestant. The following example will total the number of competitors that did not finish a given race (or DNF in Formula 1 terms).

Perform the following steps in the package org to try out this process:

  1. Create a DNF field on the Contestant object using the Checkbox field type.
  2. Create a Driver record for Charles Pic and add him to the Spa race via the Contestant object, checking the DNF field.
  3. Create a Total DNFs field on the Race object using a Rollup Summary field type. Select the Contestants object from the Summarized Object dropdown and COUNT as Rollup-Up Type and apply the Filter Criteria field as shown in the following screenshot, to select only Contestant records with the DNF field checked:
    Rollup summaries and limits

Navigate to the Spa record and note that the Total DNFs shows the value 1. Experiment by changing the status of the DNF field on the Driver records to see the value recalculate.

Rollup summaries provide a real-time calculation once configured. However, when creating or changing rollups, Salesforce states that it can take up to 30 minutes. You can read more about them by searching for About Roll-Up Summary Fields in the documentation and by reviewing the Roll Up Summary Field Technology Overview knowledge base article at http://help.salesforce.com/apex/HTViewSolution?id=000004281.

Tip

You can combine the use of Rollup summary fields in your Apex Trigger logic, for example, you can add the Apex logic to check the number of DNFs in the after phase of the trigger on the Race object. Your users can also apply Validation Rules on the Race object that references its Rollup summary fields. Any changes to Contestant detail records that cause this Validation Rule to be invalid will prevent updates to the related Contestant record.

As you can imagine, calculating Rollup summaries can be quite an intensive job for the Salesforce servers. As such, Salesforce limits the total number of Rollup summaries to 10 per object, though this can be increased to 25 by contacting Salesforce Support with a valid business case. Keep in mind that as a packaged application provider, this limit is not scoped by your namespace, unlike other limits. This means that if you use all 10, your end users will not be able to add their own post-installation. Similarly, if they have created their own post-installation and an upgrade causes the limit to be breached, the install will fail.

Tip

While this can be a powerful feature, carefully consider the other summarization options listed in the previous paragraph and if your users really need this information to be real-time or accessible from formulas and Validation Rules. If not, a good compromise is a Visualforce page that can be added inline into the Page Layout objects. The Visualforce page can utilize the readonly=true attribute in which the Apex Controller logic can utilize aggregate SOQL queries to aggregate up to 5 million records (subject to appropriate indexes and within standard timeout tolerance). This page can then display when the user reviews the record in the UI and is thus calculated on demand. Enhancing the Salesforce standard pages with Visualforce is discussed in more detail later in this chapter

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.7.208