-
Notifications
You must be signed in to change notification settings - Fork 1
Home
This is a simple framework for constructing data in Apex tests. This framework allows you to avoid copy/pasting and set default values once, in a custom metadata table.
There's a Test_Code_Default__mdt table, where enter a value for each default. For example, if you have a custom field Resolution__c on Case that is a picklist, you can default it value "Resolved" by creating a metadata record as follows:
| Field | Value |
|---|---|
| sObject__c | Case |
| sObjectField__c | Resolution__c |
| Picklist_Value__c | Resolved |
| Type__c | Picklist |
You can create as many records in the metadata table as you want. The class will only pull the table once, and it won't count against limits.
Then, in your test class, to create a Case, you would:
Case c = (Case) TestUtil.newObject('Case');
This will return the Case object. Optionally you can pass it a boolean parameter to indicate whether the object should be inserted.
Case c = (Case) TestUtil.newObject('Case',true); // Inserts the Case as well
Name, LastName, Title, Subject Fields - If the field is Name, LastName, Title, or Subject, the process will append a number to the end. So if you have Title = 'Test Account' the first record will be 'Test Account 1' and the second will be 'Test Account 2', etc.
Record Types - if the field you are populating is RecordTypeId the system will look up the ID of the Record Type and insert it. So for Record Types, use Text Value and use the display value; the system will swap out the ID of the RT for you.
Lookups - If you are confident there is data, you can specify a Lookup. This is helpful for things like parent Account of Contacts. To use a Lookup default, specify the Lookup Field, Lookup Value, and Lookup Type. For example, to set up a lookup to a parent Account on a Contact, set up a default record as follows:
| Field | Value |
|---|---|
| sObject__c | Case |
| sObjectField__c | AccountId |
| Type__c | Lookup |
| Lookup_Field__c | Name |
| Lookup_Value__c | Sample Account Name |
| Lookup_Type__c | Account |
This would set the default AccountId on Contact to the Id of the Account with Name = 'Sample Account Name'.
If you don't want a particular value to be used you can suppress it:
TestUtil.suppressDefault(Account.Name);
Account a = (Account) TestUtil.newObject('Account'); // Will not populate 'Name' attribute
You can also use the DML wrappers:
TestUtil.updateRecord(c);
This is useful to adhere to since you can expand on the test factory to insert performance tracking/tracing, etc. They are all static so the updated values will be returned to your variable.
This package contains some other items such as my MyException class, which creates an Error__c object when there is an error, so you can create a Case against it and/or report on it.
It also includes a Util class which includes two things: a Global Configuration, where you can set flags in the Global_Configuration__mdt metadata table that are swapped out at runtime. This is helpful to turn features on and off, or if you have values that you want to be configurable without updating code.
Examples:
String x = Util.getConfig('sales_vp_name'); // You know this is going to change at some point, right?
if (Util.getBoolConfig('enable_opp_insert_trigger'){
// Opp trigger logic here
}
// Use the Boolean version to enable toggling the feature on/off
// Use a comma-delimited String in the String value to store a list of items to enable the feature for
public with sharing class ContentDocumentLinkTriggerHandler {
public static void setVisibility(List<ContentDocumentLink> links) {
if (TMUtil.getBoolConfig('apply_default_visibility_to_files')){
String types = TMUtil.getConfig('apply_default_visibility_to_relatedto');
Set<String> typesToDefault = new Set<String>(types.split(','));
for (ContentDocumentLink cdl : links) {
if (cdl.LinkedEntityId != null){
String sobjectType = cdl.LinkedEntityId.getSObjectType().getDescribe().getName();
if (types == 'All' || typesToDefault.contains(sobjectType)){
cdl.Visibility = TMUtil.getConfig('default_file_visibility');
}
}
}
}
}
public static void ContentDocumentLinkBeforeInsert(List<ContentDocumentLink> cdLinks) {
setVisibility(cdLinks);
}
}