Many times you had think how you can automate a script, but you don't know how jobs works in SFRA or how you can create a custom jobs? Don't worry, first time can be difficult for all. I am here for explain to you whats is a job and how you can customize it and automate routine tasks or long-running processes.
Note: you can create jobs through business manager but I don’t explain how do it in this article.
I know, you can't wait to automate your processes, so what are we waiting for, let's automate!
Go create a js file under the path your_directory/cartridge/scripts/jobs/scriptFile.js
I created and exported a simple function which log a string, but you can do everything you want.
Remember to export function you want execute.
Go create a xml file under the path your_directory/sites/sites_template/file_name.xml
Here we declare job which execute a script, all jobs are included in tag
<jobs> </jobs> you can declare all jobs, every job is unique and it is identified by
job-id. In job you must start a
<flow> </flow> which include
<step> </step>. Every step is unique and it is identified by
step-id. Step include a
<description> </description> and a
<parameters> </parameters>. In
<parameters> you can include how many
<parameter> </parameter> you want. Every parameter have a name, In
name=“” you must specify input Parameters, you can find all input Parameters with description in official documentation.
<parameter name="ExecuteScriptModule.Module">app_project/cartridge/scripts/jobs/scriptFile.jsYou have to pass ExecuteScriptModule.Module which accept as input parameters Script File.
<parameter name="ExecuteScriptModule.FunctionName">execute</parameter>You have to pass as input parameters function’s name exported in script file.
<parameter name="ExecuteScriptModule.Transactional">false</parameter>You have to pass a boolean value, but It must be true if in function you are using class Transaction.
<triggers> </triggers> you can specify when run job.
Upload and import your job file .xml in Business Manager in Administration -> Operations -> Import & Export.
Now you can import your job immediately or wait the day and the time specified in the xml file.