Technical Articles
Customized Scheduling for integrations in SAP Cloud Integration
Start Timer is the mechanism for self-triggered integrations in SAP Cloud Integration. With its many inbuilt configurations, there still are scenarios that the present offers fails to provide. This blog utilizes the power of Scripts and Router in demonstrating how custom schedule configurations can be accomplished in a simple way.
How?
“Execute on last day of every month” is once such trigger scenario which we will achieve using a combination of JavaScript step and a Router step. The scenario is quite simple where a reminder mail should be sent to a user on last day of every month.
Let’s model this scenario step by step as follows:
- Drag and Drop a Start Timer step into the Integration Process.
- Configure it to run Daily (as our scenario is having date as the filtering parameter).
- Put a Script step in the Integration Process and connect Timer to Script (Here we are using JavaScript but either of the two i.e., JavaScript or GroovyScript can be used whichever the user is comfortable with).
importClass(com.sap.gateway.ip.core.customdev.util.Message); importClass(java.util.HashMap); /** * [Default Generated Function] * @param {[object]} message [The message being transferred] */ function processData(message) { var dateObj = new Date(); var currentDate = dateObj.getDate(); if (dateObj && currentDate === getLastDateOfTheMonth(dateObj.getMonth(), dateObj.getYear())) { return getMessage(message, true); } return getMessage(message, false); } /** * [Get message to be passed on from script step] * @param {[object]} message [The message being transferred] * @param {[boolean]} custom [Specifes if the day is the Last day or not] */ function getMessage(message, custom) { //body var body = message.getBody(); message.setBody(body + " modified from js"); //headers var map = message.getHeaders(); var value = map.get("oldHeader"); message.setHeader("oldHeader", value + "modified"); message.setHeader("newHeader", "newHeader"); //properties map = message.getProperties(); value = map.get("oldProperty"); message.setProperty("oldProperty", value + "modified"); message.setProperty("newProperty", "newProperty"); if(!custom) { message.setProperty("execute", custom); } return message; } function getLastDateOfTheMonth(month, year) { var d = new Date(year, (month + 1), 0); return d.getDate(); }
- Modify the existing script to add a header/property by the name “execute” (any name can be given here) with value “false” (again this can have any value that you would like to use as truth value) when the current day is not the last day of the month. Don not set any header/property in case the current day is last day of the month. The code implementation depends on the user. For reference purpose the following JavaScript snippet can be used.
- Save the Script and now add a Router step to the flow and connect the Script step to the Router.
- The Router will have two outgoing branches:
- First branch will be the default branch and name it some interpretable name (we are using “Execute”).
- For the Second branch provide some interpretable name (we are using “DoNotExecute”) and configure ‘Expression Type’ as ‘Non-XML’ and provide the condition as “${property.execute} = ‘false’” or “${header.execute} = ‘false’” (depending on whichever you have used in Script with correct name).
- First branch will be the default branch and name it some interpretable name (we are using “Execute”).
- Connect the “DoNotExecute” branch to an End Message step.
- Connect the “Execute” branch to the process you want to execute on Last Day of Every Month. (A process call can be used to call another Integration process containing the actual logic to be implemented). Here we connect it to and End Message which is connected to a Receiver via Mail Adapter as we need to send the mail on Last Day of Every Month. Sample overview of the scenario is as shown below.
- Save the scenario and deploy it.
The above example can be followed and modified by the users as per requirement to achieve new custom trigger configurations.
On similar lines, I tried a few more scenarios that came to my mind which involve and are listed below:
S. No. | Scenario | Code Snippet |
1. | Run every even week in a month. |
|
2. | Run every odd week in a month. |
|
3. | Run on 45th minute of every hour. |
|
4. | Run on Weekends. |
|
5. | Run on Weekdays. |
|
6. | Run on Last Weekday of the Month |
|
Therefore, by using a combination of Scheduler, a Script and a Router we can easily achieve trigger scenarios which are otherwise not possible to achieve via using Scheduler only.
awesome. thank you Apurva Sahay. Good Information
Hi Praveen,
Thank you for your kind words
Hi,
Please correct the logic for:
😉
Hi Maximiliano,
Thanks for the suggestion. The logic is rectified now.
Hi Apurva Apurva Sahay ,
Thanks a lot for sharing the information. I have a requirement same as this but there are times were we want to run the package manually like middle of the month based on user request. How that can be handled with this routing logic setup ?
thanks and regards,
Murugavel
Murugavel Singaravel - We can externalize the parameters used in the above scripts so that we can deploy the iFlow by changing these values according to ad-hoc request timestamp.
Thanks a lot Hemachandan, it worked ....
Hi Apurna, thank you for sharing.
My requirement is to run the iflow:
-every 10 min between 06:00 am - 08:00 am in time zone UTC+10
-every hour between 08:00 am - 06:am in time zone UTC+10
Hence 2 intervals within a day.
Is this achievable using the script? Otherwise I can only think of copying the iFlow for the second schedule.
Thank you for any suggestion.
Hi Sunny,
There are multiple ways to achieve this behavior.
You can put your main integration logic in a local integration process which will be called only when the condition in the scripts is satisfied.
Also could you please clarify if the second schedule "Execute every hour between 08:00 am - 06:am in time zone UTC+10" is every hour between 08:00 am and 06:00 pm or 06:00 am next day ?
Thanks
Hi Apurva,
Thanks much for sharing the information!
I have a requirement to schedule it once on quarterly basis on a particular day of every Quarter. How this can be handled.
Thanks,
Ezhil.
Hi Ezhil,
You can configure the scheduler to run Monthly and in the script you can check the current month and if it is the quarter month you can return "execute" as true else it will be false.
Hope this helps.
Hi Apurva,
We have requirement to run the iflow daily at 6 PM IST from Monday - Saturday, and at 5:15 PM IST only on Sundays.
Can you please suggest the best way to achieve this ?
Regards,
Debashis
Hi Debashis,
I see that you need two different schedules altogether for your case:
There are two possible approaches to achieve this:
Approach 1:
Approach 2:
Both approaches should solve your problem. You can pick whichever suits your case.
Regards,
Apurva
Hi Apurva Sahay,
Thank you for sharing. We have a requirement to run the iflow :
Monday – 7AM, 8AM, 9AM, 10AM, 11AM, 12PM and 4PM
Tuesday to Sunday – 7AM.
Can you please help how to handle this.
Thanks & Regards,
Poushali Bhandari
Hi Poushali,
Using the Script-Router combination you can achieve this as follow:
Regards,
Apurva
Hi Apurva,
Thanks for this useful info.
I have a requirement where the interface should execute at the 30th Minute. In CPI, currently we don't have 30 min interval option.
If we choose to run the interface every hour, the first execution happens at 00:00. Instead of this it should run 00:30, 01:30, 02:30 so on.. Could you please let me know if this can be achieved.
First Run of the interface should happen at 0030 instead of 0000 We do not have a standard option in CPI for this.
Hi Chethan,
From your question I understand that you want to configure the start time of the scheduler in your interface to 00:30 and then run every 1 Hr i.e., 00:30, 01:30, 02:30, 03:30 and so on.
It is possible to achieve this with Script-Router combination as follows:
This should give you the desired trigger pattern.
Regards,
Apurva
Hi Apurva,
We have a requirement where we need to trigger Purchase Order Details (Create, Update or Delete) to a thirdy party via REST APIs, they want CPI to initiate the call for triggering the data from the S/4 HANA system is this achievable using the Timer Functionality.
Appreciate your help.
Regards,
Sahaj
Hi Sahaj,
If I understand your use case correctly you want to use a timer triggered scenario which fetches data from S/4 HANA and sends it to a third party REST API. This can be easily done in SAP Cloud Integration.
For sending data to third party REST APIs you can use the HTTPS Receiver Adapter and for fetching data from S/4 HANA system you can use a request reply with the respective receiver adapter(based on the type of endpoint exposed by S/4 HANA system e.g., SOAP, HTTPS OData). You can configure and use the Timer step to schedule the triggering of this scenario. If you need a custom scheduling pattern you can follow the steps mentioned in this blog.
Regards,
Apurva
Hi Apurva,
We have a requirement to run the IFlow on 1st, 3rd , 5th Friday of the month.
For Example in the Month of Dec, 2021 - The interface should run on 3rd,17th,31st.
Can you please help on how to handle this scenario.
Appreciate your help,
Sunitha
Hi Apurva,
I have a requirement to run my flow on every 4th Monday of the month.
I'm using this script still i'm getting error. if there are any changes please let me know
I have a requirement where i want my interface to run once a year on the first day of the year.
Could you please share the groovy for the same ?
how i tested this iflow , i didn't get any endpoint url . but it is successfully deployed