Overview
This article outlines the scheduling of the Purge Processes (Purge Cost Transactions, Purge System Transactions, Purge Work Orders, Purge Purchase Orders, and Purge Sales Orders) via a scheduled job.
NOTE Setting up the Scheduled Job function is technical in nature and requires System Administrator privileges.
Prerequisites
-
System Administrator privileges
-
Understanding of Salesforce Developer Console
-
Understanding of Salesforce Scheduled Jobs
-
Understanding of Apex Code
Application
This is used to schedule the Purge Processes to run on a pre-determined schedule instead of manually initiating the routine from the UI.
The following are the Scheduled Jobs for Purge Processes:
-
ScheduledPurgeCostTxnsJob
-
ScheduledPurgeSystemTxnsJob
-
ScheduledPurgeWorkOrdersJob
-
ScheduledPurgePurchaseOrdersJob
-
ScheduledPurgeSalesOrdersJob
Processing
Step 1: Click on the Developer Console option under the User Menu in the upper-right corner of the User Interface.
Step 2: In the Developer Console window, click on Debug, then Open Execute Anonymous Window.
Step 3: In the Execute Anonymous Window - enter the example command, then click the Execute button.
To execute the Job instantly, the user can use the below function
To execute the Job at a scheduled time, the user can use the below function
Cron Expression: A CRON expression is a string of five or six fields separated by white spaces representing a set of times, generally as a schedule to execute some routine.
-
Seconds - Second Start
-
Values: 0-59
-
Special Characters: None
-
Minutes - Minute Start
-
Values: 0-59
-
Special Characters: None
-
Hours - Hour Start (Uses 24-hour clock)
-
Day_of_month - Day of the month Start
-
Month - Month Start
-
Values: 1-12 or JAN/FEB/MAR/APR/MAY/JUN/JUL/AUG/SEP/OCT/NOV/DEC
-
Special Characters (see below): , - * /
-
Day_of_week - Day of the week Start
-
Optional_year - Start year
The special characters are defined below,
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 the 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, not the other.
|
/
|
Specifies increments. The number before the slash specifies when the interval will begin, and the number after the slash is the interval amount. For example, if you specify 1/5 for Day_of_month, the Analysis and Repair job 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 the 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 specify 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 Analysis and Repair runs on the 19th. If you specify 1W, and the first is a Saturday, the Analysis and Repair job does not run in the previous month, but on the third, which is the following Monday. Tip Use the L and W together to specify the last weekday of the month.
|
#
|
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 Analysis and Repair job runs on the first Monday of every month.
|
Examples:
Expression
|
Description
|
---|
0 0 10 ? * * *
|
Perform the process at 10:00 AM every day.
|
0 0-5 15 * * ?
|
Perform the process every minute starting at 3:00 PM and ending at 3:05 PM every day.
|
0 0 20 * * ? 2021
|
Perform the process every day at 8 PM during the year 2021.
|
0 0 18 ? * MON-FRI
|
Perform the process from Monday through Friday at 6:00 PM.
|
NOTE To schedule a purge job to run simultaneously in multiple Divisions, the user has to run the same code/script (by changing the Division Id) for different Divisions each time.
NOTE For a division, the user can create multiple schedule jobs for a purge process by running the same code/script with different CRON expressions each time.
1. ScheduledPurgeCostTxnsJob
This job performs the Purge Cost Transactions process.
Input Parameter for paramsMap:
S No
|
Map Field Name
|
Type
|
Description
|
Mandatory (Y/N)
|
---|
1
|
division
|
Id
|
record Id of the Division Master.
|
Y
|
2
|
thruDateForPurge
|
Date
|
Date to compare with the 'Accounting Date' of the Cost Transaction records OR The Dynamic Date (e.g., TODAY-4) to purge the records within the given date range.
|
Y
|
3
|
purgeSYTXNCST
|
Boolean
|
If true, Inventory Cost Transactions through the given date will be purged.
|
Y
|
4
|
purgeGLJEH
|
Boolean
|
If true, General Journal Headers and Details through the given date will be purged.
|
Y
|
NOTE At least one of the boolean field values must be set to true.
EXAMPLE 1 To perform the Purge Cost Transactions for the Division 'KLH Central (1401)', which is scheduled to run at 1:00 PM on weekdays (Monday to Friday), the following needs to be executed in the 'Open Execute Anonymous Window' in Developer Console in the Org.
Map<String, Object> paramsMap = new Map<String,Object> {
'division' => 'a5Sd0000000XZCm',
'thruDateForPurge' => Date.valueOf('2021-01-11'),
'purgeSYTXNCST' => true,
'purgeGLJEH' => false};
String cronExp = '0 0 13 ? * MON-FRI';
rstk.ScheduledPurgeCostTxnsJob.schedule(paramsMap, cronExp);
EXAMPLE 2 To perform the Purge Cost Transactions for the Division 'KLH Central (1401)' instantly, the following needs to be executed in the 'Open Execute Anonymous Window' in Developer Console in the Org.
Map<String, Object> paramsMap = new Map<String,Object> {
'division' => 'a5Sd0000000XZCm',
'thruDateForPurge' => Date.valueOf('2021-01-11'),
'purgeSYTXNCST' => true,
'purgeGLJEH' => false};
rstk.ScheduledPurgeCostTxnsJob.runNow(paramsMap);
EXAMPLE 3 To purge Cost Transaction records every Monday at 9:00 AM (records created within the last 7 days) in the Division 'KLH Central (1401)', the following needs to be executed in the 'Open Execute Anonymous Window' in Developer Console in the Org.
Map<String, Object> paramsMap = new Map<String,Object> {
'division' => 'a5Sd0000000XZCm',
'thruDateForPurge' => 'TODAY-7',
'purgeSYTXNCST' => true,
'purgeGLJEH' => false};
String cronExp = '0 0 9 ? * MON';
rstk.ScheduledPurgeCostTxnsJob.schedule(paramsMap, cronExp);
2. ScheduledPurgeSystemTxnsJob
This job performs the Purge System Transactions process.
Input Parameter for paramsMap:
S No
|
Map Field Name
|
Type
|
Description
|
Mandatory (Y/N)
|
---|
1
|
thruDateForPurge
|
Datetime
|
Date through which the selected records to be purged OR The Dynamic Date (e.g., TODAY-4) to purge the records within the given date range.
|
Y
|
2
|
ignoreStatus
|
Boolean
|
If true, the selected records (SYDATA, SYDATAT, SOAPI, POLOADER, PLMIMPORT) will be purged irrespective of the status. If false, only the processed records will be purged.
|
Y
|
3
|
purgeSYDATA
|
Boolean
|
If true, the SYDATA and SYDATAT records having the 'Last Modified Date' before the given date will be purged.
|
Y
|
4
|
purgeSOAPI
|
Boolean
|
If true, the SOAPI records having the 'Last Modified Date' before the given date will be purged.
|
Y
|
5
|
purgePOLOADER
|
Boolean
|
If true, the POLOADER records having the 'Last Modified Date' before the given date will be purged.
|
Y
|
6
|
purgePROCESSLOG
|
Boolean
|
If true, the Process Log records having the 'Created Date' before the given date will be purged.
|
Y
|
7
|
purgeLABORBOOK
|
Boolean
|
If true, the Process Labor Booking records having the 'Created Date' before the given date will be purged.
|
Y
|
8
|
purgeSYRESULT
|
Boolean
|
If true, the SYRESULT and SYRESULTD records having the 'Created Date' before the given date will be purged.
|
Y
|
9
|
purgePLMIMPORT
|
Boolean
|
If true, the PLM Import records (PLMIMPORT, PLMITEM, PLMBOM, PLMAPPMFG) having the 'Last Modified Date' before the given date will be purged.
|
Y
|
10
|
purgePOPRINTHDR
|
Boolean
|
If true, the PO Print records (POPRINTHDR, POPRINTLINE) having the 'Created Date' before the given date will be purged.
|
Y
|
11
|
purgeFSOACKH
|
Boolean
|
If true, the Sales Order Print records (FSOACKH, FSOACKD, FSOACKSCHED, FSOACKCOMP) having the 'Created Date' before the given date will be purged.
|
Y
|
12
|
purgeFSOINVH
|
Boolean
|
If true, the Sales Invoice Print records (FSOINVH, FSOINVLINE, FSOINVDTL) having the 'Created Date' before the given date will be purged.
|
Y
|
13
|
purgeICDMDPICKLOC
|
Boolean
|
If true, the Picklist records (ICDMDPICKLOC, SOPICKH, SOPICKD, FSOPICKH) having the 'Created Date' before the given date will be purged.
|
Y
|
14
|
purgeICIXRSOORDDMD
|
Boolean
|
If true, the irrelevant ICIXR records of Sales order Demand.
|
Y
|
NOTE At least one of the boolean field values must be set to true.
EXAMPLE 1 To perform the Purge System Transactions which is scheduled to run at 7:00 PM on Friday, the following needs to be executed in the 'Open Execute Anonymous Window' in Developer Console in the Org.
Map<String, Object> paramsMap = new Map<String,Object> {
'thruDateForPurge' => Datetime.newInstance(2022, 06, 08, 12, 01, 59),
'ignoreStatus' => true,
'purgeSYDATA' => false,
'purgeSOAPI' => false,
'purgePOLOADER' => true,
'purgePROCESSLOG' => true,
'purgeLABORBOOK' => false,
'purgeSYRESULT' => false,
'purgePLMIMPORT' => true,
'purgePOPRINTHDR' => true,
'purgeFSOACKH' => false,
'purgeFSOINVH' => false,
'purgeICDMDPICKLOC' => true,
'purgeICIXRSOORDDMD' => false};
String cronExp = '0 0 19 ? * FRI';
rstk.ScheduledPurgeSystemTxnsJob.schedule(paramsMap, cronExp);
EXAMPLE 2 To instantly perform the Purge System Transactions, the following needs to be executed in the 'Open Execute Anonymous Window' in Developer Console in the Org.
Map<String, Object> paramsMap = new Map<String,Object> {
'thruDateForPurge' => Datetime.newInstance(2022, 06, 09, 12, 29, 59),
'ignoreStatus' => true,
'purgeSYDATA' => false,
'purgeSOAPI' => false,
'purgePOLOADER' => true,
'purgePROCESSLOG' => true,
'purgeLABORBOOK' => false,
'purgeSYRESULT' => false,
'purgePLMIMPORT' => true,
'purgePOPRINTHDR' => true,
'purgeFSOACKH' => true,
'purgeFSOINVH' => false,
'purgeICDMDPICKLOC' => true,
'purgeICIXRSOORDDMD' => false};
rstk.ScheduledPurgeSystemTxnsJob.runNow(paramsMap);
EXAMPLE 3 To purge the System Transactions every Friday at 7:00 PM (records created within the last 3 days), the following needs to be executed in the 'Open Execute Anonymous Window' in Developer Console in the Org.
Map<String, Object> paramsMap = new Map<String,Object> {
'thruDateForPurge' => 'TODAY-3',
'ignoreStatus' => true,
'purgeSYDATA' => false,
'purgeSOAPI' => false,
'purgePOLOADER' => true,
'purgePROCESSLOG' => true,
'purgeLABORBOOK' => false,
'purgeSYRESULT' => false,
'purgePLMIMPORT' => true,
'purgePOPRINTHDR' => true,
'purgeFSOACKH' => false,
'purgeFSOINVH' => false,
'purgeICDMDPICKLOC' => true,
'purgeICIXRSOORDDMD' => false};
String cronExp = '0 0 19 ? * FRI';
rstk.ScheduledPurgeSystemTxnsJob.schedule(paramsMap, cronExp);
3. ScheduledPurgeWorkOrdersJob
This job performs the Work Order Purge process.
Input parameter for paramsMap:
S No
|
Map Field Name
|
Type
|
Description
|
Mandatory (Y/N)
|
---|
1
|
division
|
Id
|
record Id of the Division Master
|
Y
|
2
|
cutoffDateForPurge
|
Date
|
The cut-off date to compare with the 'Actual Close Date' of Work Order Header records OR The Dynamic Date (e.g., TODAY-4) to purge the records within the given date range.
|
Y
|
3
|
archive
|
String
|
If True, the closed Work Orders and all subordinate data will be archived based on the given cut-off date.
|
N
|
4
|
purgearch
|
String
|
If True, the archived Work Orders will be purged in addition to the qualifying non-archived Work orders based on the given cut-off date.
|
N
|
EXAMPLE 1 To perform the Work Order Purge for the Division 'KLH Central (1401)' which is scheduled to run at 8:00 PM every day in 2022, the following needs to be executed in the 'Open Execute Anonymous Window' in Developer Console in the Org.
Map<String, Object> paramsMap = new Map<String,Object> {
'division' => 'a5Sd0000000XZCm',
'cutoffDateForPurge' => Date.valueOf('2022-03-11'),
'archive' => 'true',
'purgearch' => 'false'};
String cronExp = '0 0 20 * * ? 2022';
rstk.ScheduledPurgeWorkOrdersJob.schedule(paramsMap, cronExp);
EXAMPLE 2 To instantly perform the Work Order Purge for the Division 'KLH Central (1401)' , the following needs to be executed in the 'Open Execute Anonymous Window' in Developer Console in the Org.
Map<String, Object> paramsMap = new Map<String,Object> {
'division' => 'a5Sd0000000XZCm',
'cutoffDateForPurge' => Date.valueOf('2022-04-20'),
'archive' => 'false',
'purgearch' => 'true'};
rstk.ScheduledPurgeWorkOrdersJob.runNow(paramsMap);
EXAMPLE 3 To purge the Work Order records every day in 2023 at 8:00 PM (records created within a day) in the Division 'KLH Central (1401)', the following needs to be executed in the 'Open Execute Anonymous Window' in Developer Console in the Org.
Map<String, Object> paramsMap = new Map<String,Object> {
'division' => 'a5Sd0000000XZCm',
'cutoffDateForPurge' => 'TODAY-1',
'archive' => 'true',
'purgearch' => 'false'};
String cronExp = '0 0 20 * * ? 2023';
rstk.ScheduledPurgeWorkOrdersJob.schedule(paramsMap, cronExp);
4. ScheduledPurgePurchaseOrdersJob
This job performs the Purge Purchase Orders process.
Input parameter for customext:
S No
|
Map Field Name
|
Type
|
Description
|
Mandatory (Y/N)
|
---|
1
|
popurge_sydiv__c
|
Id
|
record Id of the Division Master.
|
Y
|
2
|
popurge_cutoffdate__c
|
Date
|
The cut-off date to compare with the 'Close Date' of Purchase Order Header records.
|
Y
|
3
|
cutOffDateExpression
|
Date
|
The Dynamic Date (e.g., TODAY-4) to purge the records within the given date range.
|
Y
|
NOTE The parameters 'popurge_cutoffdate__c' and 'cutOffDateExpression' are mutually exclusive, only one can be specified at a time.
Input parameter for paramsMap:
S No
|
Map Field Name
|
Type
|
Description
|
Mandatory (Y/N)
|
---|
1
|
customext
|
Object Name
|
This is the variable for the object 'customext__c' which has the values mentioned in the above table.
|
Y
|
EXAMPLE 1 To perform the Purge Purchase Orders for the Division 'KLH Central (1401)' which is scheduled to run at 10:00 AM every day, the following needs to be executed in the 'Open Execute Anonymous Window' in Developer Console in the Org.
rstk__customext__c customext = new rstk__customext__c();
customext.rstk__popurge_sydiv__c = 'a5Sd0000000XZCm';
customext.rstk__popurge_cutoffdate__c = Date.valueOf('2022-02-18');
Map<String, Object> paramsMap = new Map<String,Object> {
'customext' => customext};
String cronExp = '0 0 10 ? * * *';
rstk.ScheduledPurgePurchaseOrdersJob.schedule(paramsMap, cronExp);
EXAMPLE 2 To perform the Purge Purchase Orders for the Division 'KLH Central (1401)' instantly, the following needs to be executed in the 'Open Execute Anonymous Window' in Developer Console in the Org.
rstk__customext__c customext = new rstk__customext__c();
customext.rstk__popurge_sydiv__c = 'a5Sd0000000XZCm';
customext.rstk__popurge_cutoffdate__c = Date.valueOf('2022-02-18');
Map<String, Object> paramsMap = new Map<String,Object> {
'customext' => customext};
rstk.ScheduledPurgePurchaseOrdersJob.runNow(paramsMap);
EXAMPLE 3 To purge the Purchase Orders every day at 10:00 AM (records created within a day) in the Division 'KLH Central (1401)', the following needs to be executed in the 'Open Execute Anonymous Window' in Developer Console in the Org.
rstk__customext__c customext = new rstk__customext__c();
customext.rstk__popurge_sydiv__c = 'a5Sd0000000XZCm';
Map<String, Object> paramsMap = new Map<String,Object> {
'customext' => customext,
'cutOffDateExpression' => 'TODAY-1'};
String cronExp = '0 0 10 ? * * *';
rstk.ScheduledPurgePurchaseOrdersJob.schedule(paramsMap, cronExp);
5. ScheduledPurgeSalesOrdersJob
This job performs the Purge Sales Orders process.
Input parameter for customext:
S No
|
Map Field Name
|
Type
|
Description
|
Mandatory (Y/N)
|
---|
1
|
sopurge_sydiv__c
|
Id
|
record Id of the Division Master.
|
Y
|
2
|
sopurge_cutoffdate__c
|
Date
|
The cut-off date to compare with the 'Close Date' of Sales Order Header records and to the 'Date Transferred to AR' of Sales Invoice records.
|
Y
|
3
|
cutOffDateExpression
|
Date
|
The Dynamic Date (e.g., TODAY-4) to purge the records within the given date range.
|
Y
|
4
|
sopurge_purgeopt__c
|
String
|
The values are 'Purge Sales Orders + Purge Sales Order Invoices' and 'Purge Sales Invoice Distributions (only)'.
|
N
|
NOTE The parameters 'sopurge_cutoffdate__c' and 'cutOffDateExpression' are mutually exclusive, only one can be specified at a time.
Input parameter for paramsMap:
S No
|
Field API Name
|
Type
|
Description
|
Mandatory (Y/N)
|
---|
1
|
customext
|
Object Name
|
This is the variable for the object 'customext__c' which has the values mentioned in the above table.
|
Y
|
NOTE When the value of 'sopurge_purgeopt__c' is null, the value 'Purge Sales Orders + Purge Sales Order Invoices’ will be considered. And if it is not null, the value 'Purge Sales Invoice Distributions (only)' will be considered.
EXAMPLE 1 To perform the purge of Sales Order Headers and Sales Order Invoice for the Division 'KLH Central (1401)' which is scheduled to run at 1:00 PM on weekdays (Monday to Friday), the following needs to be executed in the 'Open Execute Anonymous Window' in Developer Console in the Org.
rstk__customext__c customext = new rstk__customext__c();
customext.rstk__sopurge_sydiv__c = 'a5Sd0000000XZCm';
customext.rstk__sopurge_cutoffdate__c = Date.valueOf('2022-03-18');
customext.rstk__sopurge_purgeopt__c = 'Purge Sales Orders + Purge Sales Order Invoices';
//sopurge_purgeopt is optional:
//If(sopurge_purgeopt == null) == 'Purge Sales Orders + Purge Sales Order Invoices'
//If(sopurge_purgeopt NOT null) == 'Purge Sales Invoice Distributions (only)'
Map<String, Object> paramsMap = new Map<String,Object> {
'customext' => customext};
String cronExp = '0 0 13 ? * MON-FRI';
rstk.ScheduledPurgeSalesOrdersJob.schedule(paramsMap, cronExp);
EXAMPLE 2 To instantly perform the purge of the records related to Sales Order Invoices for the Division 'KLH Central (1401)' , the following needs to be executed in the 'Open Execute Anonymous Window' in Developer Console in the Org.
rstk__customext__c customext = new rstk__customext__c();
customext.rstk__sopurge_sydiv__c = 'a5Sd0000000XZCm';
customext.rstk__sopurge_cutoffdate__c = Date.valueOf('2022-03-18');
customext.rstk__sopurge_purgeopt__c = 'Purge Sales Invoice Distributions (only)';
//sopurge_purgeopt is optional:
//If(sopurge_purgeopt == null) == ‘Purge Sales Orders + Purge Sales Order Invoices’
//If(sopurge_purgeopt NOT null) == 'Purge Sales Invoice Distributions (only)'
Map<String, Object> paramsMap = new Map<String,Object> {
'customext' => customext};
rstk.ScheduledPurgeSalesOrdersJob.runNow(paramsMap);
EXAMPLE 3 To purge the Sales Order Header and Sales Order Invoice records on weekdays at 1:00 PM (records created within a day) in the Division 'KLH Central (1401)' the following needs to be executed in the 'Open Execute Anonymous Window' in Developer Console in the Org.
rstk__customext__c customext = new rstk__customext__c();
customext.rstk__sopurge_sydiv__c = 'a5Sd0000000XZCm';
customext.rstk__sopurge_purgeopt__c = 'Purge Sales Orders + Purge Sales Order Invoices';
//sopurge_purgeopt is optional:
//If(sopurge_purgeopt == null) == 'Purge Sales Orders + Purge Sales Order Invoices'
//If(sopurge_purgeopt NOT null) == 'Purge Sales Invoice Distributions (only)'
Map<String, Object> paramsMap = new Map<String,Object> {
'customext' => customext,
'cutOffDateExpression' => 'TODAY-1'};
String cronExp = '0 0 13 ? * MON-FRI';
rstk.ScheduledPurgeSalesOrdersJob.schedule(paramsMap, cronExp);
Deleting an existing Scheduled Job:
SEE ALSO
Purge Cost Transactions
Purge System Transactions
Work Order Purge
Purchase Order Purge
Sales Order Purge
Purge Financial Transactions