Code the Job Function
Now let’s code the job function that will be triggered when a Function Job is executed from the Function Job Pool. This function will be used to send out the birthday greeting to the required person via their email. This function will be employing the Mail SDK.
The job function present in the functions directory, BirthdayGreetings/functions/dynamic_cron/ contains:
- The index.js main function file
- The catalyst-config.json configuration file
- Node modules
- package.json and package-lock.json dependency files.
You will be adding code in the index.js file.
Copy the code given below and paste it in the index.js file.
Note: Please go through the code in this section to make sure you fully understand it.
index.jscopy'use strict'; const catalyst = require('zcatalyst-sdk-node'); module.exports = async (jobRequest, context) => { const catalystApp = catalyst.initialize(context); try { console.log("Jobs", jobRequest.getAllJobParams()); const { id, name, email, message, birthday } = jobRequest.getAllJobParams(); await catalystApp.email().sendMail({ from_email: 'emmy@zylker.com', // Add sender's email address to_email: [email], html_mode: true, subject: `Birthday Wishes for ${name}`, content: `Hello ${name},
${message}`, }); console.log(`Email sent successfully to ${email}`); context.closeWithSuccess('Email sent successfully.'); } catch (error) { console.error('Error:', error); context.closeWithFailure('Failed to send email.'); } };Note: Ensure that you add the sender email address you configured using the Mail component in line 12.All of the required functions have now been configured for your application.
Last Updated 2025-04-01 11:10:20 +0530 +0530