DEV Community

Cover image for How create a  custom Job which execute a script  in SFRA πŸ”¨

How create a custom Job which execute a script in SFRA πŸ”¨

21yo, I write what i have learned and sometimes i drink coffee β˜•
・2 min read

Intro πŸ’‘

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!

Let’s Create File to execute! πŸ› οΈ

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.


Let’s Declare Job! βš™οΈ

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>.


In <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.

  • In name <parameter name="ExecuteScriptModule.Module">app_project/cartridge/scripts/jobs/scriptFile.js
    You have to pass ExecuteScriptModule.Module which accept as input parameters Script File.

  • In <parameter name="ExecuteScriptModule.FunctionName">execute</parameter> You have to pass as input parameters function’s name exported in script file.

  • In <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.

With <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.

Discussion (0)