Skip to content

UTIL_System

Class · Group: Utilities

apex
global inherited sharing class UTIL_System

Namespace, type resolution, and platform utility methods. Provides runtime introspection for managed package namespace detection, class type resolution, API version retrieval, and session management.

Since: 1.0

Example:

apex
String namespace = UTIL_System.getManagedPackageNamespace();
Type handlerType = UTIL_System.getTypeForClassName('TRG_SetFoobarDefaults');
Type validated = UTIL_System.getTypeForClassName('TRG_SetFoobarDefaults', IF_Trigger.BeforeInsert.class);

Methods

MethodDescription
global static Type findTypeForClassName(String className)Resolves a class name to a Type via the UTIL_TypeResolver chain without throwing an exception.
global static Type findTypeForClassName(String className, Type expectedType)Resolves a class name to a Type via the UTIL_TypeResolver chain and verifies it is assignable to the expected type.
global static String getApiEnabledSessionId()Will render a VF page to retrieve an API Enabled Session Id for current user To improve performance, session will be cached
global static String getClassNamespace(String className)Will extract the namespace out of a class name, expects the class to be a top level class
global static String getManagedPackageNamespace()Will determine the namespace for the current execution context; ie.
global static String getManagedPackageNamespacePrefix(String delimiter)Will determine the namespace for the current execution context; ie.
global static String getNamespacePrefix(String namespace, String delimiter)Will calculate namespace prefix as follows: if the namespace exists - namespace + delimiter else Empty String
global static String getOrgApiVersion()Will determine the current Salesforce Org Api Version
global static String getRuntimeTypeName(Object anObject)This method serves to retrieve the type name of the provided object
global static Type getTypeForClassName(String className)Will return the Type for a given object class name
global static Type getTypeForClassName(String className, Type expectedType)Will return the Type for a given object class name

Method Details

findTypeForClassName

apex
global static Type findTypeForClassName(String className)

Resolves a class name to a Type via the UTIL_TypeResolver chain without throwing an exception. Returns null when the class cannot be found or has compilation errors.

Parameters:

  • className (String) - The class name to resolve.

Returns: Type - The resolved Type, or null if not found.

Since: 1.0

Example:

apex
Type classType = UTIL_System.findTypeForClassName('SCHED_DeactivateUsers');

findTypeForClassName

apex
global static Type findTypeForClassName(String className, Type expectedType)

Resolves a class name to a Type via the UTIL_TypeResolver chain and verifies it is assignable to the expected type. Returns null when the class cannot be found or does not implement the expected type.

Parameters:

  • className (String) - The class name to resolve.
  • expectedType (Type) - The type the resolved class must be assignable to.

Returns: Type - The resolved Type, or null if not found or not assignable.

Since: 1.0

Example:

apex
Type classType = UTIL_System.findTypeForClassName('SCHED_DeactivateUsers', Schedulable.class);

getApiEnabledSessionId

apex
global static String getApiEnabledSessionId()

Will render a VF page to retrieve an API Enabled Session Id for current user To improve performance, session will be cached

Returns: String - API enabled Session Id

Since: 1.0

Example:

apex
String sessionId = UTIL_System.getApiEnabledSessionId();

getClassNamespace

apex
global static String getClassNamespace(String className)

Will extract the namespace out of a class name, expects the class to be a top level class

Parameters:

  • className (String) - The name of the class

Returns: String - Empty String or the namespace

Since: 1.0

Example:

apex
String namespace = UTIL_System.getClassNamespace('kern.UTIL_System'); // 'kern'
String empty = UTIL_System.getClassNamespace('MyClass'); // ''

getManagedPackageNamespace

apex
global static String getManagedPackageNamespace()

Will determine the namespace for the current execution context; ie. Is code currently running sitting in a package and the namespace of the package

Returns: String - Empty String or the package namespace

Since: 1.0

Example:

apex
String namespace = UTIL_System.getManagedPackageNamespace(); // e.g. 'kern' or ''

getManagedPackageNamespacePrefix

apex
global static String getManagedPackageNamespacePrefix(String delimiter)

Will determine the namespace for the current execution context; ie. Is code currently running sitting in a package and the namespace of the package and add the provided delimiter (if a namespace exists)

Parameters:

  • delimiter (String) - The delimiter for the item

Returns: String - Empty String or the namespace with prefix

Since: 1.0

Example:

apex
// Returns 'Sfdc_Surveys.' when namespace exists with '.' delimiter
String namespaceWithDot = UTIL_System.getManagedPackageNamespacePrefix('.');
// Returns 'Sfdc_Surveys__' when namespace exists with '__' delimiter
String namespaceWithUnderscores = UTIL_System.getManagedPackageNamespacePrefix('__');
// Returns '' when namespace does not exist

getNamespacePrefix

apex
global static String getNamespacePrefix(String namespace, String delimiter)

Will calculate namespace prefix as follows: if the namespace exists - namespace + delimiter else Empty String

Parameters:

  • namespace (String) - The provided namespace
  • delimiter (String) - The delimiter for the item

Returns: String - Empty String or the namespace with prefix

Since: 1.0

Example:

apex
String prefix = UTIL_System.getNamespacePrefix('kern', '.'); // 'kern.'

getOrgApiVersion

apex
global static String getOrgApiVersion()

Will determine the current Salesforce Org Api Version

Returns: String - the version in the form of "59.0"

Since: 1.0

Example:

apex
String version = UTIL_System.getOrgApiVersion(); // e.g. '65.0'

getRuntimeTypeName

apex
global static String getRuntimeTypeName(Object anObject)

This method serves to retrieve the type name of the provided object

Parameters:

  • anObject (Object) - The object to evaluate

Returns: String - Name of Class of the Object provided

Since: 1.0

Example:

apex
String typeName = UTIL_System.getRuntimeTypeName((Object)'Hello'); // 'String'

getTypeForClassName

apex
global static Type getTypeForClassName(String className)

Will return the Type for a given object class name

Parameters:

  • className (String) - The class name to find a type for (please ensure that you prefix namespace if required)

Returns: Type - The type of the object

Throws:

Since: 1.0

Example:

apex
Type classType = UTIL_System.getTypeForClassName('UTIL_System');

getTypeForClassName

apex
global static Type getTypeForClassName(String className, Type expectedType)

Will return the Type for a given object class name

Parameters:

  • className (String) - The class name to find a type for (please ensure that you prefix namespace if required)
  • expectedType (Type) - The expected type that the type for the class is assignable from

Returns: Type - The type of the object

Throws:

Since: 1.0

Example:

apex
Type classType = UTIL_System.getTypeForClassName('UTIL_System', Object.class);