Cloud Kicks management is very happy with the development of subscriptions. However, the team is very busy with the manual management of subscriptions.Jose asks Linda if there is a way to automate the subscription lifecycle management.

Linda, who has already automated quite a few processes, immediately checks to see if there are any automations planned in this regard.
She quickly finds this information in the Frisbii knowledge base:
Automation - Articles | Apex Classes | Invocable Method |
Automation - Subscription Renewal | SubscriptionAutoRenewal | Renew_Subscriptions |
Activate subscriptions (in progress) | SubscriptionAutoActivate | n/a |
Cancel subscriptions (in progress) | SubscriptionAutoTermination | n/a |
End subscriptions (in progress) | SubscriptionAutoEnd | n/a |
Generation of invoice schedules for subscriptions (in progress) | n/a | InvoicePlan_Subscriptions |
Elle starts with the configuration of the automatic renewal of subscriptions and chooses between the two configuration options.
Scheduling an Apex job
Either Linda schedules the Apex job using the SubscriptionAutoRenewal class.
Using this configuration, the job will renew the subscriptions that fit the conditions at a specific time and day regularly.
Trigger conditions on the subscription
In order to create a change order on a subscription, you must first check if :
The subscription is activated.
The subscription should be renewed (conditions contracted with the customer, for example: current date = 1 month before the end of the subscription).
The customer has authorized the renewal.
Beware: Adjust these conditions to your needs or to the specific needs of your customer.
Configure the required field for the scheduled job
If you want to schedule an Apex job, you have to create a formula field taking these conditions into account.
Steps
1. Go to Setup > Object Manager > Subscription.
2. Click on Fields & Relationships.
3. Click on the New button.
4. In Step 1, choose the field type Formula.
5. In Step 2, choose the Formula Return Type Checkbox and fill in Auto renewal condition.
6. In Step 3, switch to Advanced Formula and compose a formula adapted to your needs.
Example:
AND (ADDMONTHS(sofactoapp__Fin_periode_facturation__c,-1)<=TODAY(),
sofactoapp__Ne_pas_renouveler__c = FALSE,
sofactoapp__Tacite_reconduction__c = true,
ISPICKVAL(sofactoapp__Statut__c, "Activé"))Beware: If you copy the formula specified in this article, first copy it into a text editor that removes all formatting. The formula editor will probably not accept a copy and paste directly from this article.
7. Check the syntax.
8. Provide a meaningful description and help text.
9. In Step 4, configure the security of this field by checking the boxes corresponding to the profiles, according to your company rules. Alternatively, you can set these rights to permission sets.
10. Uncheck the box to add the field automatically on the page layout, to better place the created field manually later.
11. Don't forget to translate this field and its help text if multiple languages are used in this Salesforce environment.
Here is Linda's result:
Condition to trigger auto-renewal of the subscription : initial end date = Today , Do not renew = False , no next subscription
Control field on the Administration panel
In order to schedule the Apex job later, we will select the condition to be taken into account by the Apex class in the Administration panel.
Steps
1. Open to the app launcher.
2. Type Administration in the search bar.
3. Open the Administration panel.
4. Select Auto renewal condition as the Control field for auto renewal of subscriptions.

5. Save by clicking on the Confirm button.
Planning the Apex class
If you want the invoice generation process to start at a certain time, you have to use a scheduled Apex Class.
Here is the Salesforce procedure for Apex Task Scheduling.
Steps
1. Go to Setup > Custom Code > Apex Class.
2. Click the Schedule Apex button.
3. Enter a Job Name.
4. Fill in the Apex Class SubscriptionAutoRenewal.

5. Set the Frequency and Start as well as the Preferred Start Time.
6. For the End date, enter a date far in the future to prevent the process from stopping too early.
7. Then click on the Save button.
Note: The scheduled task is then visible in Configuration > Environments > Jobs > Scheduled Jobs.
To modify the job, you have to delete and reschedule it.
Testing approach
After the configuration, let's not forget to test our process in a sandbox.
Preparation
First, ensure you have a subscription available to you that meets the criteria.
For example, in the case of Cloud Kicks:
Subscription is enabled > Status = Enabled.
The subscription should be renewed > Do not renew is not checked.
End date is in one month or less > End date - 1 month ≤ today.
The customer agrees with automatic renewal > Tacit Renewal is checked.
...and that you have added the Auto renewal condition field in your page layout.
Steps
1. Open the Developer Console.

2. In the Debug menu > Open Execute Anonymous Window.
3. Paste this expression into the Enter Apex Code window :
// String CRON_EXP = ‘ss mm hh dd MM ? AAAA’;
String CRON_EXP = '00 18 10 14 07 ? 2021';
String lv_jobId = System.schedule(
'SubscriptionAutoRenewal'+Datetime.now(),
CRON_EXP,
new sofactoapp.SubscriptionAutoRenewal()
);4. Put the date and time in a minute (the format is explained on the first line, it starts with seconds, then minutes, hours, day, month and year).
Example: if it is 10:17 at the moment, put 00 18 10. This leaves you 1 minute to trigger the automation. Beware: the format is reversed (minutes and hours).
5. Click on the Execute in order to trigger the script.
Was the test successful?
The test is passed if:
A change order is created and linked to your subscription.
The "end of billing period" date has been pushed back into the future (by default by one month)
This is not the case ?
Please verify:
Conditions of your field:
Auto renewal condition
On your test subscription:
The box Auto renewal condition is checked ?
No ? > Compare the subscription with the conditions defined before.
In the Administration panel, did you set the condition ?
If you are not sure, please check the procedure again.

Testing the automation through the Apex class worked well.