Newer Version Available
System Class
Namespace
System Methods
The following are methods for System. All methods are static.
abortJob(jobId)
Signature
public static Void abortJob(String jobId)
Parameters
- jobId
- Type: String
- The jobId is the ID associated with either AsyncApexJob or CronTrigger.
Return Value
Type: Void
Usage
The following methods return the job ID that can be passed to abortJob.
- System.schedule method—returns the CronTrigger object ID associated with the scheduled job as a string.
- SchedulableContext.getTriggerId method—returns the CronTrigger object ID associated with the scheduled job as a string.
- getJobId method—returns the AsyncApexJob object ID associated with the batch job as a string.
- Using Batch ApexDatabase.executeBatch method—returns the AsyncApexJob object ID associated with the batch job as a string.
assert(condition, msg)
Signature
public static Void assert(Boolean condition, Object msg)
Parameters
- condition
- Type: Boolean
- msg
- Type: Object
- (Optional) Custom message returned as part of the error message.
Return Value
Type: Void
assertEquals(expected, actual, msg)
Signature
public static Void assertEquals(Object expected, Object actual, Object msg)
Parameters
- expected
- Type: Object
- Specifies the expected value.
- actual
- Type: Object
- Specifies the actual value.
- msg
- Type: Object
- (Optional) Custom message returned as part of the error message.
Return Value
Type: Void
Usage
You can’t catch an assertion failure using a try/catch block even though it’s logged as an exception.
assertNotEquals(expected, actual, msg)
Signature
public static Void assertNotEquals(Object expected, Object actual, Object msg)
Parameters
- expected
- Type: Object
- Specifies the expected value.
- actual
- Type: Object
- Specifies the actual value.
- msg
- Type: Object
- (Optional) Custom message returned as part of the error message.
Return Value
Type: Void
Usage
You can’t catch an assertion failure using a try/catch block even though it’s logged as an exception.
currentPageReference()
Signature
public static System.PageReference currentPageReference()
Return Value
Type: System.PageReference
Usage
For more information, see PageReference Class.
currentTimeMillis()
Signature
public static Long currentTimeMillis()
Return Value
Type: Long
debug(msg)
Signature
public static Void debug(Object msg)
Parameters
- msg
- Type: Object
Return Value
Type: Void
Usage
If the msg argument is not a string, the debug method calls String.valueOf to convert it into a string. The String.valueOf method calls the toString method on the argument, if available, or any overridden toString method if the argument is a user-defined type. Otherwise, if no toString method is available, it returns a string representation of the argument.
If the log level for Apex Code is set to DEBUG or higher, the message of this debug statement will be written to the debug log.
For more information on log levels, see Debug Log Levels in the Salesforce online help.
debug(logLevel, msg)
Signature
public static Void debug(LoggingLevel logLevel, Object msg)
Parameters
- logLevel
- Type: LoggingLevel Enum
- The logging level to set for this method.
- msg
- Type: Object
- The message or object to write in string format to the execution debug log.
Return Value
Type: Void
Usage
If the msg argument is not a string, the debug method calls String.valueOf to convert it into a string. The String.valueOf method calls the toString method on the argument, if available, or any overridden toString method if the argument is a user-defined type. Otherwise, if no toString method is available, it returns a string representation of the argument.
For more information on log levels, see Debug Log Levels in the Salesforce online help.
enqueueJob(queueableObj)
Signature
public static ID enqueueJob(Object queueableObj)
Parameters
- queueableObj
- Type: Object
- An instance of the class that implements the Queueable Interface.
Usage
To add a job for asynchronous execution, call System.enqueueJob by passing in an instance of your class implementation of the Queueable interface for execution as follows:
For more information about Queueable Apex, including information about limits, see Queueable Apex.
enqueueJob(queueable, delay)
Signature
public static Id enqueueJob(Object queueable, Integer delay)
Parameters
- queueable
- Type: Object
- An instance of the class that implements the Queueable Interface.
- delay
- Type: Integer
- The minimum delay (0–10 minutes) before the queueable job is scheduled for execution.
- The delay is ignored during Apex testing.
Example
This example adds a job for delayed asynchronous execution by passing in an instance of your class implementation of the Queueable interface for execution. There’s a minimum delay of 5 minutes before the job is executed.
For more information about Queueable Apex, including information about limits, see Queueable Apex.
enqueueJob(queueable, asyncOptions)
Signature
public static Id enqueueJob(Object queueable, Object asyncoptions)
Parameters
- queueable
- Type: Object
- An instance of the class that implements the Queueable Interface.
- asyncoptions
- Type: AsyncOptions
- Specify a unique signature for your queueable job, the maximum stack depth, or a minimum queue delay in the AsyncOptions class properties.
Usage
The System.AsyncInfo class methods help you determine if maximum stack depth is set in your Queueable request and get the stack depths and queue delay for queueables that are currently running. Use information about the current queueable execution to make decisions on adjusting delays on subsequent calls.
These are methods in the System.AsyncInfo class.
- hasMaxStackDepth()
- getCurrentQueueableStackDepth()
- getMaximumQueueableStackDepth()
- getMinimumQueueableDelayInMinutes()
For more information about Queueable Apex, including information about limits, see Queueable Apex.
equals(obj1, obj2)
Signature
public static Boolean equals(Object obj1, Object obj2)
Parameters
- obj1
- Type: Object
- Object being compared.
- obj2
- Type: Object
- Object to compare with the first argument.
Return Value
Type: Boolean
Usage
obj1 and obj2 can be of any type. They can be values, or object references, such as sObjects and user-defined types.
The comparison rules for System.equals are identical to the ones for the == operator. For example, string comparison is case insensitive. For information about the comparison rules, see the == operator.
getApplicationReadWriteMode()
Signature
public static System.ApplicationReadWriteMode getApplicationReadWriteMode()
Using the System.ApplicationReadWriteMode Enum
Use the System.ApplicationReadWriteMode enum returned by the getApplicationReadWriteMode to programmatically determine if the application is in read-only mode during Salesforce upgrades and downtimes.
- DEFAULT
- READ_ONLY
getQuiddityShortCode(QuiddityValue)
Signature
public String getQuiddityShortCode(System.Quiddity QuiddityValue)
Parameters
- QuiddityValue
- Type: System.Quiddity
- The Quiddity enum value that has an associated short code. This short code is used in Event Monitoring logs. For more information, see Apex Execution Event Type.
Return Value
Type: String
hashCode(obj)
Signature
public static Integer hashCode(Object obj)
Parameters
- obj
- Type: Object
- The object to get the hash code for. This parameter can be of any type, including values or object references, such as sObjects or user-defined types.
Return Value
Type: Integer
Versioned Behavior Changes
In API version 51.0 and later, the hashCode() method returns the same hashCode for identical Id values. In API version 50.0 and earlier, identical Id values didn’t always generate the same hashCode value.
isBatch()
Signature
public static Boolean isBatch()
Return Value
Type: Boolean
isFunctionCallback()
Signature
public static Boolean isFunctionCallback()
Return Value
Type: Boolean
Usage
Use this method to determine if the Apex code is being invoked as part of a callback from an asynchronous Salesforce Functions invocation. For more details on invoking Salesforce Functions from Apex, see Functions Namespace
isFuture()
Signature
public static Boolean isFuture()
Return Value
Type: Boolean
isQueueable()
Signature
public static Boolean isQueueable()
Return Value
Type: Boolean
Usage
isRunningElasticCompute()
Signature
public static Boolean isRunningElasticCompute()
Return Value
Type: Boolean
isScheduled()
Signature
public static Boolean isScheduled()
Return Value
Type: Boolean
movePassword(targetUserId,sourceUserId)
Signature
public static Void movePassword(ID targetUserId, ID sourceUserId)
Parameters
Return Value
Type: Void
Usage
Moving a password simplifies converting a user to another type of user, such as when converting an external user to a user with less restrictive access. If you require access to the movePassword method, contact Salesforce.
- The targetUserId, sourceUserId, and user performing the move operation must all belong to the same Salesforce org.
- The targetUserId and the sourceUserId cannot be the same as the user performing the move operation.
- A user without a password can’t be specified as the sourceUserId. For example, a source user who has already had their password moved is left without a password. That user can’t be a source user again.
- The target user can log in with the password.
- The source user no longer has a password. To enable logins for this user, a password reset is required.
now()
Signature
public static Datetime now()
Return Value
Type: Datetime
purgeOldAsyncJobs(dt)
Signature
public static Integer purgeOldAsyncJobs(Date dt)
Parameters
- dt
- Type: Date
- Specifies the date up to which old records are deleted. The date comparison is based on the CompletedDate field of AsyncApexJob, which is in the GMT time zone.
Return Value
Type: Integer
Usage
Asynchronous Apex job records are records in AsyncApexJob.
The system cleans up asynchronous job records for jobs that have finished execution and are older than seven days. You can use this method to further reduce the size of AsyncApexJob by cleaning up more records.
Each execution of this method counts as a single row against the governor limit for DML statements.
requestVersion()
Signature
public static System.Version requestVersion()
Return Value
Type: System.Version
Usage
Using this method, you can determine the version of an installed instance of your package from which the calling code is referencing your package. Based on the version that the calling code has, you can customize the behavior of your package code.
The requestVersion method isn’t supported for unmanaged packages. If you call it from an unmanaged package, an exception will be thrown.
resetPassword(userId, sendUserEmail)
Signature
public static System.ResetPasswordResult resetPassword(ID userId, Boolean sendUserEmail)
Return Value
Type: System.ResetPasswordResult
Usage
When the user logs in with the new password, they are prompted to enter a new password, and to select a security question and answer if they haven't already. If you specify true for sendUserEmail, the user is sent an email notifying them that their password was reset. A link to sign onto Salesforce using the new password is included in the email. Use setPassword(userId, password) if you don't want the user to be prompted to enter a new password when they log in.
resetPasswordWithEmailTemplate(userId, sendUserEmail, emailTemplateName)
Signature
public static System.ResetPasswordResult resetPasswordWithEmailTemplate(Id userId, Boolean sendUserEmail, String emailTemplateName)
Parameters
Return Value
Usage
If you specify true for sendUserEmail, specify the email template that is sent to the user notifying them that their password was reset. When the user logs in with the new password in the email, they are prompted to enter a new password. A link to sign onto Salesforce using the new password is included in the email. Use setPassword(userId, password) if you don't want the user to be prompted to enter a new password when they log in.
runAs(version)
Signature
public static Void runAs(System.Version version)
Parameters
- version
- Type: System.Version
Return Value
Type: Void
Usage
A package developer can use Version methods to continue to support existing behavior in classes and triggers in previous package versions while continuing to evolve the code. Apex classes and triggers are saved with the version settings for each installed managed package that the class or trigger references.
This method is used for testing your component behavior in different package versions that you upload to the AppExchange. This method effectively sets a two-part version consisting of major and minor numbers in a test method so that you can test the behavior for different package versions.
You can only use runAs in a test method. There is no limitation to the number of calls to this method in a transaction. For sample usage of this method, see Testing Behavior in Package Versions.
runAs(userSObject)
Signature
public static Void runAs(User userSObject)
Parameters
- userSObject
- Type: User
Return Value
Type: Void
Usage
All of the specified user's record sharing is enforced during the execution of runAs. You can only use runAs in a test method. For more information, see Using the runAs() Method.
The runAs method implicitly inserts the user that is passed in as parameter if the user has been instantiated, but not inserted yet.
You can also use runAs to perform mixed DML operations in your test by enclosing the DML operations within the runAs block. In this way, you bypass the mixed DML error that is otherwise returned when inserting or updating setup objects together with other sObjects. See sObjects That Cannot Be Used Together in DML Operations.
schedule(jobName, cronExpression, schedulableClass)
Signature
public static String schedule(String jobName, String cronExpression, Object schedulableClass)
Usage
Use extreme care if you’re planning to schedule a class from a trigger. You must be able to guarantee that the trigger won’t add more scheduled classes than the limit. In particular, consider API bulk updates, import wizards, mass record changes through the user interface, and all cases where more than one record can be updated at a time. Use the abortJob method to stop the job after it has been scheduled.
Using the System.Schedule Method
The following are the values for the expression:
| Name | Values | Special Characters |
|---|---|---|
| Seconds | 0–59 | None |
| Minutes | 0–59 | None |
| Hours | 0–23 | , - * / |
| Day_of_month | 1–31 | , - * ? / L W |
| Month | 1–12 or the following:
|
, - * / |
| Day_of_week | 1–7 or the following:
|
, - * ? / L # |
| optional_year | null or 1970–2099 | , - * / |
| Special Character | Description |
|---|---|
| , | Delimits values. For example, use JAN, MAR, APR to specify more than one month. |
| - | Specifies a range. For example, use JAN-MAR to specify more than one month. |
| * | Specifies all values. For example, if Month is specified as *, the job is scheduled for every month. |
| ? | Specifies no specific value. This is only available for Day_of_month and Day_of_week, and is generally used when specifying a value for one and not the other. |
| / | Specifies increments. The number before the slash specifies when the intervals will begin, and the number after the slash is the interval amount. For example, if you specify 1/5 for Day_of_month, the Apex class runs every fifth day of the month, starting on the first of the month. |
| L | Specifies the end of a range (last). This is only available for Day_of_month and Day_of_week. When used with Day of month, L always means the last day of the month, such as January 31, February 29 for leap years, and so on. When used with Day_of_week by itself, it always means 7 or SAT. When used with a Day_of_week value, it means the last of that type of day in the month. For example, if you specify 2L, you are specifying the last Monday of the month. Do not use a range of values with L as the results might be unexpected. |
| W | Specifies the nearest weekday (Monday-Friday) of the given day. This is only available for Day_of_month. For example, if you specify 20W, and the 20th is a Saturday, the class runs on the 19th. If you specify 1W, and the first is a Saturday, the class does not run in the previous month, but on the third, which is the following Monday. |
| # | Specifies the nth day of the month, in the format weekday#day_of_month. This is only available for Day_of_week. The number before the # specifies weekday (SUN-SAT). The number after the # specifies the day of the month. For example, specifying 2#1 means the class runs on the first Monday of every month. |
The following are some examples of how to use the expression.
| Expression | Description |
|---|---|
| 0 0 13 * * ? | Class runs every day at 1 PM. |
| 0 0 22 ? * 6L | Class runs the last Friday of every month at 10 PM. |
| 0 0 10 ? * MON-FRI | Class runs Monday through Friday at 10 AM. |
| 0 0 20 * * ? 2010 | Class runs every day at 8 PM during the year 2010. |
In the following example, the class proschedule implements the Schedulable interface. The class is scheduled to run at 8 AM, on the 13 February.
scheduleBatch(batchable, jobName, minutesFromNow)
Signature
public static String scheduleBatch(Database.Batchable batchable, String jobName, Integer minutesFromNow)
Parameters
- batchable
- Type: Database.Batchable
- An instance of a class that implements the Database.Batchable interface.
- jobName
- Type: String
- The name if the job that this method will start.
- minutesFromNow
- Type: Integer
- The time interval in minutes after which the job should start executing. This argument must be greater than zero.
Usage
For an example, see Using Batch Apex.
scheduleBatch(batchable, jobName, minutesFromNow, scopeSize)
Signature
public static String scheduleBatch(Database.Batchable batchable, String jobName, Integer minutesFromNow, Integer scopeSize)
Parameters
- batchable
- Type: Database.Batchable
- The batch class that implements the Database.Batchable interface.
- jobName
- Type: String
- The name of the job that this method will start.
- minutesFromNow
- Type: Integer
- The time interval in minutes after which the job should start executing.
- scopeSize
- Type: Integer
- The number of records that should be passed to the batch execute method.
Return Value
Type: String
Usage
For an example, see Using the System.scheduleBatch Method.
setPassword(userId, password)
Signature
public static Void setPassword(ID userId, String password)
Return Value
Type: Void
Usage
- If a security question hasn't been previously configured, a user who logs in with a new password that was set using setPassword() is redirected to the "Change Your Password" page.
- Use resetPassword(userId, sendUserEmail) if you want the user to go through the reset process and create their own password.
submit(workItemIds, comments, nextApprover)
Signature
public static List<ID> submit(List<ID> workItemIds, String comments, String nextApprover)
Usage
For enhanced submit and evaluation features, see the ProcessSubmitRequest class.
today()
Signature
public static Date today()
Return Value
Type: Date