Reference Apex Standard Classes and Methods
Methods
The following are static methods of the [Link] class.
Method Arguments Return Type Description
forName String [Link] Returns the type that corresponds to the specified
fullyQualifiedName fully qualified class name.
The fullyQualifiedName argument is the fully
qualified name of the class to get the type of. The
fully qualified class name contains the namespace
name, for example, [Link].
Note: When called from an installed
managed package to get the name of a
local type in an organization with no
defined namespace, the
forName(fullyQualifiedName)
method returns null. Instead, use the
forName(namespace, name) method
and specify an empty string or null for
the namespace argument.
forName String namespace [Link] Returns the type that corresponds to the specified
namespace and class name.
String name
The namespace argument is the namespace of
the class.
The name argument is the name of the class.
If the class doesn't have a namespace, set the
namespace argument to null or an empty string.
Note: Use this method instead of
forName(fullyQualifiedName) if
it will be called from a managed package
installed in an organization with no
defined namespace. To get the name of
a local type, set the namespace argument
to an empty string or null. For example,
Type t = [Link]('',
'ClassName');.
This example shows how to get the type that
corresponds to the ClassName class and the
MyNamespace namespace.
Type myType =
[Link]('MyNamespace',
'ClassName');
The following are instance methods of the [Link] class.
487
Reference Apex Standard Classes and Methods
Method Arguments Return Type Description
equals Object toCompare Boolean Returns true if the specified type is equal to the
current type; otherwise, returns false.
The toCompare argument is the type to compare
with the current type.
Example:
Type t1 = [Link];
Type t2 = [Link]('Account');
[Link]([Link](t2));
getName String Returns the name of the current type.
This example shows how to get a Type’s name. It
first obtains a Type by calling forName, then calls
getName on the Type object.
Type t =
[Link]('MyClassName');
String typeName =
[Link]();
[Link]('MyClassName',
typeName);
hashCode Integer Returns a hash code value for the current type.
The returned hash code value corresponds to the
type name hash code that [Link]
returns.
newInstance Object Creates an instance of the current type and returns
this new instance.
Because newInstance returns the generic object
type, you should cast the return value to the type
of the variable that will hold this value.
This method enables you to instantiate a Type
that implements an interface and call its methods
while letting someone else provide the methods’
implementation. For example, a package developer
can provide an interface that a subscriber who
installs the package can implement. The code in
the package calls the subscriber's implementation
of the interface methods by instantiating the
subscriber’s Type.
This example shows how to create an instance of
a Type. It first gets a Type by calling forName
with the name of a class (ShapeImpl), then calls
newInstance on this Type object. The newObj
instance is declared with the interface type
488
Reference Apex Standard Classes and Methods
Method Arguments Return Type Description
(Shape) that the ShapeImpl class implements.
The return value of the newInstance method
is cast to the Shape type.
Type t =
[Link]('ShapeImpl');
Shape newObj =
(Shape)[Link]();
toString String Returns a string representation of the current type,
which is the type name.
This method returns the same value as getName.
[Link] and [Link] use this
method to convert their Type argument into a
String.
This example calls toString on the Type
corresponding to a list of Integers.
Type t =
List<Integer>.class;
String s = [Link]();
[Link](
'LIST<Integer>', s);
Sample: Instantiating a Type Based on Its Name
The following sample shows how to use the Type methods to instantiate a Type based on its name. A typical application of
this scenario is when a package subscriber provides a custom implementation of an interface that is part of an installed package.
The package can get the name of the class that implements the interface through a custom setting in the subscriber’s org. The
package can then instantiate the type that corresponds to this class name and invoke the methods that the subscriber
implemented.
In this sample, Vehicle represents the interface that the VehicleImpl class implements. The last class contains the code
sample that invokes the methods implemented in VehicleImpl.
This is the Vehicle interface.
global interface Vehicle {
Long getMaxSpeed();
String getType();
}
This is the implementation of the Vehicle interface.
global class VehicleImpl implements Vehicle {
global Long getMaxSpeed() { return 100; }
global String getType() { return 'Sedan'; }
}
The method in this class gets the name of the class that implements the Vehicle interface through a custom setting value.
It then instantiates this class by getting the corresponding type and calling the newInstance method. Next, it invokes the
489
Reference Apex Standard Classes and Methods
methods implemented in VehicleImpl. This sample requires that you create a public list custom setting named
CustomImplementation with a text field named className. Create one record for this custom setting with a data set
name of Vehicle and a class name value of VehicleImpl.
public class CustomerImplInvocationClass {
public static void invokeCustomImpl() {
// Get the class name from a custom setting.
// This class implements the Vehicle interface.
CustomImplementation__c cs = CustomImplementation__c.getInstance('Vehicle');
// Get the Type corresponding to the class name
Type t = [Link](cs.className__c);
// Instantiate the type.
// The type of the instantiated object
// is the interface.
Vehicle v = (Vehicle)[Link]();
// Call the methods that have a custom implementation
[Link]('Max speed: ' + [Link]());
[Link]('Vehicle type: ' + [Link]());
}
}
Class Property
The class property returns the [Link] of the type it is called on. It is exposed on all Apex built-in types including
primitive data types and collections, sObject types, and user-defined classes. This property can be used instead of forName
methods.
Call this property on the type name. For example:
[Link] t = [Link];
You can use this property for the second argument of [Link], deserializeStrict,
[Link], and readValueAsStrict methods to get the type of the object to deserialize. For example:
Decimal n = (Decimal)[Link]('100.1', [Link]);
URL Methods
Represents a uniform resource locator (URL) and provides access to parts of the URL. Enables access to the Salesforce instance
URL.
Usage
Use the methods of the [Link] class to create links to objects in your organization. Such objects can be files, images,
logos, or records that you want to include in external emails, in activities, or in Chatter posts. For example, you can create a
link to a file uploaded as an attachment to a Chatter post by concatenating the Salesforce base URL with the file ID, as shown
in the following example:
// Get a file uploaded through Chatter.
ContentDocument doc = [SELECT Id FROM ContentDocument
WHERE Title = 'myfile'];
// Create a link to the file.
String fullFileURL = [Link]().toExternalForm() +
490
Reference Apex Standard Classes and Methods
'/' + [Link];
[Link](fullFileURL);
The following example creates a link to a Salesforce record. The full URL is created by concatenating the Salesforce base URL
with the record ID.
Account acct = [SELECT Id FROM Account WHERE Name = 'Acme' LIMIT 1];
String fullRecordURL = [Link]().toExternalForm() + '/' + [Link];
Constructors
Arguments Description
String protocol Creates a new instance of the [Link] class using the specified
protocol, host, port, and file on the host.
String host
Integer port
String file
String protocol Creates a new instance of the [Link] class using the specified
protocol, host, and file on the host. The default port for the specified
String host
protocol is used.
String file
URL context Creates a new instance of the [Link] class by parsing the specified
spec within the specified context.
String spec
For more information about the arguments of this constructor, see the
corresponding URL([Link], [Link]) constructor for Java.
String spec Creates a new instance of the [Link] class using the specified string
representation of the URL.
Methods
The following are static methods for the [Link] class.
Method Arguments Returns Description
getCurrentRequestUrl [Link] Returns the URL of an entire request on a
Salesforce instance.
For example,
[Link]
getFileFieldURL String entityId String Returns the download URL for a file
attachment.
String fieldName
The entityId argument specifies the ID
of the entity that holds the file data.
The fieldName argument specifies the API
name of a file field component, such as
AttachmentBody.
491