Capture the name of the App where a record was created

Background

One of my clients asked me if there is a way to track which application the user was in when they created an Account record. The client did not have Event Monitoring. 

The thought was to store the application name where the user was at the time they created the account, on the account itself. But this storage can also be done in a more centralized location such as a custom object and be extended to more than just account object records. Whether we store the information on the record, or in a centralized location, the approach to get the application information would be the same using the two objects mentioned below.

The queries can be run in Apex trigger/LWC, or using Flow.

Use the following objects, in this order:

  1. UserAppInfo (Label: Last Used App)
  2. AppDefinition (Label: App Definition)

Sample Queries:

Query UserApInfo filtering for User ID first:

SELECT AppDefinitionId, UserId, Id FROM UserAppInfo where UserId='0052g0000023kgaAAA'

Take the AppDefinitionId and filter AppDefinition where DurableId=AppDefinitionId:

SELECT Id, DurableId, Label, MasterLabel, DeveloperName FROM AppDefinition Where DurableId='06md0000000bp06AAA'

Sample Flow

Get Data From Custom Metadata Types

What are Custom Metadata Types?

Essentially, Custom Metadata Types are a replacement for List Custom Settings. But they are more than that, you can:

  • Deploy Custom Metadata Types along with their records.
  • Control the visibility or the metadata type, as well as individual records.
  • Query them using SOQL and not count against governor limits.
  • Access them in Process Builder and Validation Rules
  • For more information, see official Salesforce Documentation

Sample Code to read Custom Metadata Type records:

String queueExt = '69381';

// you can query custom metadata type using SOQL. The advantage is that this query does not count against governor limits.
CTI_Case_Queue__mdt[] queueMapping = [SELECT 
                MasterLabel, Label, DeveloperName, 
                Queue_Extension__c, Queue_Name__c 
                  FROM CTI_Case_Queue__mdt
                  WHERE Queue_Extension__c = :queueExt];

System.debug('==== queueExt: ' + queueExt);
System.debug('==== num of records: ' + queueMapping.size());

for(CTI_Case_Queue__mdt q : queueMapping)
{
 System.debug('==== MasterLabel: ' + q.MasterLabel);
 System.debug('==== Label: ' + q.Label);
 System.debug('==== DeveloperName: ' + q.DeveloperName);
 System.debug('==== Queue_Extension__c: ' + q.Queue_Extension__c);
 System.debug('==== Queue_Name__c: ' + q.Queue_Name__c);
}