Id Class
Contains methods for the ID primitive data type.
This sample shows how to use the getSObjectType method to obtain an sObject token from an ID. The updateOwner method in this sample accepts a list of IDs of the sObjects to update the ownerId field of. This list contains IDs of sObjects of the same type. The second parameter is the new owner ID. Note that since it is a future method, it doesn’t accept sObject types as parameters; this is why it accepts IDs of sObjects. This method gets the sObject token from the first ID in the list, then does a describe to obtain the object name and constructs a query dynamicallly. It then queries for all sObjects and updates their owner ID fields to the new owner ID.
The following are methods for Id.
-
Marks a trigger record with a custom error message and prevents any DML operation from occurring.
-
Marks a trigger record with a custom error message, specifies if the error message should be escaped, and prevents any DML operation from occurring.
-
Marks a trigger record with a custom error message and prevents any DML operation from occurring.
-
addError(exceptionError, escape)
Marks a trigger record with a custom error message and prevents any DML operation from occurring.
-
Returns the token for the sObject corresponding to this ID. This method is primarily used with describe information.
-
Converts an 18-character Id value to a 15-character case-sensitive string.
-
Converts the specified String into an ID and returns the ID.
-
Converts the specified string into an ID and returns the ID. If
restoreCasingistrue, and the string represents an 18-character ID that has incorrect casing, the method returns an 18-character ID that is correctly aligned with its encoded casing.
Marks a trigger record with a custom error message and prevents any DML operation from occurring.
public Void addError(String errorMsg)
- errorMsg: Type: String The error message to mark the record with.
Type: Void
This method is similar to the addError(errorMsg) sObject method.
This method escapes any HTML markup in the specified error message. The escaped characters are: \n, <, >, &, ", \, \u2028, \u2029, and \u00a9. As a result, HTML markup is not rendered; instead, it is displayed as text in the Salesforce user interface.
Marks a trigger record with a custom error message, specifies if the error message should be escaped, and prevents any DML operation from occurring.
public Void addError(String errorMsg, Boolean escape)
-
errorMsg: Type: String
The error message to mark the record with.
-
escape: Type: Boolean
Indicates whether any HTML markup in the custom error message should be escaped (
true) or not (false). This parameter is ignored in both Lightning Experience and the Salesforce mobile app, and the HTML is always escaped. The escape parameter only applies in Salesforce Classic.
Type: Void
The escaped characters are: \n, <, >, &, ", \, \u2028, \u2029, and \u00a9. As a result, HTML markup is not rendered; instead, it is displayed as text in the Salesforce user interface.
Be cautious if you specify false for the escape argument. Unescaped strings displayed in the Salesforce user interface can represent a vulnerability in the system because these strings might contain harmful code. If you want to include HTML markup in the error message, call this method with a false escape argument. Make sure that you escape any dynamic content, such as input field values. Otherwise, specify true for the escape argument or call addError(String errorMsg) instead.
Marks a trigger record with a custom error message and prevents any DML operation from occurring.
public Void addError(Exception exceptionError)
- exceptionError: Type: System.Exception An Exception object or a custom exception object that contains the error message to mark the record with.
Type: Void
This method is similar to the addError(exceptionError) sObject method.
This method escapes any HTML markup in the specified error message. The escaped characters are: \n, <, >, &, ", \, \u2028, \u2029, and \u00a9. As a result, HTML markup is not rendered; instead, it is displayed as text in the Salesforce user interface.
This trigger marks the first record in Trigger.new with the exception.
Marks a trigger record with a custom error message and prevents any DML operation from occurring.
public Void addError(Exception exceptionError, Boolean escape)
-
exceptionError: Type: System.Exception
An Exception object or a custom exception object that contains the error message to mark the record with.
-
escape: Type: Boolean
Indicates whether any HTML markup in the custom error message should be escaped (
true) or not (false). This parameter is ignored in both Lightning Experience and the Salesforce mobile app, and the HTML is always escaped. The escape parameter only applies in Salesforce Classic.
Type: Void
The escaped characters are: \n, <, >, &, ", \, \u2028, \u2029, and \u00a9. As a result, HTML markup is not rendered; instead, it is displayed as text in the Salesforce user interface.
Be cautious if you specify false for the escape argument. Unescaped strings displayed in the Salesforce user interface can represent a vulnerability in the system because these strings might contain harmful code. If you want to include HTML markup in the error message, call this method with a false escape argument. Make sure that you escape any dynamic content, such as input field values. Otherwise, specify true for the escape argument or call addError(Exception e) instead.
This trigger marks the first record in Trigger.new with an unescaped exception.
Returns the token for the sObject corresponding to this ID. This method is primarily used with describe information.
public Schema.SObjectType getSObjectType()
Type: Schema.SObjectType
For more information about describes, see Understanding Apex Describe Information.
Converts an 18-character Id value to a 15-character case-sensitive string.
public static string to15()
Type: String
Converts the specified String into an ID and returns the ID.
public static ID valueOf(String toID)
- toID: Type: String
Type: ID
In API version 54.0 and later, assignment of an invalid 15 or 18 character ID to a variable results in a System.StringException exception.
Converts the specified string into an ID and returns the ID. If restoreCasing is true, and the string represents an 18-character ID that has incorrect casing, the method returns an 18-character ID that is correctly aligned with its encoded casing.
public static Id valueOf(String str, Boolean restoreCasing)
- str: Type: String String to be converted to an ID
- restoreCasing: Type: Boolean If set to
true, and str represents an 18-character ID, the method returns an 18-character ID that is correctly aligned with its casing.
Type: Id
The return value depends on both the str and the restoreCasing parameter values.
If the str is invalid, the method throws a System.StringException exception.
| Parameters | restoreCasing=true | restoreCasing=false |
|---|---|---|
| Valid 15-character str value | Because the 15-character input value can’t be encoded for casing, the method throws a System.StringException. | The method returns a 15-character ID. |
| Valid 18-character str value | The method returns an 18-character ID that is correctly aligned with its encoded casing. | The method doesn’t attempt to correctly align the casing of the 18-character ID and returns an 18-character ID. |