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.

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

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

ParameterTypeDescription
classNameStringThe class name to resolve.

Returns Type — The resolved Type, or null if not found.

Example

apex
Type classType = UTIL_System.findTypeForClassName('SCHED_DeactivateUsers');
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

ParameterTypeDescription
classNameStringThe class name to resolve.
expectedTypeTypeThe type the resolved class must be assignable to.

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

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

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

ParameterTypeDescription
classNameStringThe name of the class

Returns String — Empty String or the namespace

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

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

ParameterTypeDescription
delimiterStringThe delimiter for the item

Returns String — Empty String or the namespace with prefix

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

ParameterTypeDescription
namespaceStringThe provided namespace
delimiterStringThe delimiter for the item

Returns String — Empty String or the namespace with prefix

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"

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

ParameterTypeDescription
anObjectObjectThe object to evaluate

Returns String — Name of Class of the Object provided

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

ParameterTypeDescription
classNameStringThe class name to find a type for (please ensure that you prefix namespace if required)

Returns Type — The type of the object

Throws

ExceptionDescription
IllegalArgumentExceptionIf no type if found for the given class name

Example

apex
Type classType = UTIL_System.getTypeForClassName('UTIL_System');
apex
global static Type getTypeForClassName(String className, Type expectedType)

Will return the Type for a given object class name

Parameters

ParameterTypeDescription
classNameStringThe class name to find a type for (please ensure that you prefix namespace if required)
expectedTypeTypeThe expected type that the type for the class is assignable from

Returns Type — The type of the object

Throws

ExceptionDescription
IllegalArgumentExceptionIf no type if found for the given class name
TypeExceptionif the type is not compatible with the expected type

Example

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