<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: how to write custom cronjob email in CRM and CX Q&amp;A</title>
    <link>https://community.sap.com/t5/crm-and-cx-q-a/how-to-write-custom-cronjob-email/qaa-p/12118139#M416680</link>
    <description>&lt;P&gt;Thank you Igor for sharing the complete solution.It gave an understanding to proceed further. Thanks again&lt;/P&gt;</description>
    <pubDate>Fri, 25 Mar 2022 11:55:39 GMT</pubDate>
    <dc:creator>former_member648175</dc:creator>
    <dc:date>2022-03-25T11:55:39Z</dc:date>
    <item>
      <title>how to write custom cronjob email</title>
      <link>https://community.sap.com/t5/crm-and-cx-q-a/how-to-write-custom-cronjob-email/qaq-p/12118136</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
  &lt;P&gt;I have a requirement of modifying the custom cronjob email template with some additional parameters in the cronjob.&lt;/P&gt;
  &lt;P&gt;If anyone have sample code, please provide.&lt;/P&gt;
  &lt;P&gt;Thanks&lt;/P&gt;
  &lt;P&gt;Jyothi&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2019 13:42:27 GMT</pubDate>
      <guid>https://community.sap.com/t5/crm-and-cx-q-a/how-to-write-custom-cronjob-email/qaq-p/12118136</guid>
      <dc:creator>Jyothi_Remalli</dc:creator>
      <dc:date>2019-09-16T13:42:27Z</dc:date>
    </item>
    <item>
      <title>Re: how to write custom cronjob email</title>
      <link>https://community.sap.com/t5/crm-and-cx-q-a/how-to-write-custom-cronjob-email/qaa-p/12118137#M416678</link>
      <description>&lt;P&gt;Hi Jyothi,&lt;/P&gt;&lt;P&gt;Just for example - I've added one of the custom fields (catalog id) of custom cronjob model into the custom email template.&lt;/P&gt;&lt;P&gt;Custom cron job model:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;&amp;lt;itemtype code="MyTestCronJob" extends="CronJob"
   generate="true" autocreate="true"&amp;gt;
   &amp;lt;attributes&amp;gt;
      &amp;lt;attribute qualifier="catalog" type="Catalog"&amp;gt;
         &amp;lt;description&amp;gt;Some catalog&amp;lt;/description&amp;gt;
         &amp;lt;persistence type="property" /&amp;gt;
         &amp;lt;modifiers optional="true" /&amp;gt;
      &amp;lt;/attribute&amp;gt;
   &amp;lt;/attributes&amp;gt;
&amp;lt;/itemtype&amp;gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;please check steps / sample code below&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;create the custom template.vm&lt;/LI&gt;&lt;/UL&gt;&lt;PRE&gt;&lt;CODE&gt;{yourcoreextension}/import/emails/MyCronJobFinishNotificationTemplate_en.vm&lt;/CODE&gt;&lt;/PRE&gt;&lt;UL&gt;&lt;LI&gt;fill your custom template file with data&lt;/LI&gt;&lt;/UL&gt;&lt;PRE&gt;&lt;CODE&gt;&amp;lt;html&amp;gt;
   &amp;lt;head&amp;gt;
   &amp;lt;/head&amp;gt;
   &amp;lt;body&amp;gt;
      this is my custom field from cron job model: $ctx.catalogId
   &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;UL&gt;&lt;LI&gt;create the custom context class&lt;/LI&gt;&lt;/UL&gt;&lt;PRE&gt;&lt;CODE&gt;package com.test.core.jalo;

import de.hybris.platform.cronjob.jalo.CronJobNotificationTemplateContext;
import de.hybris.platform.util.Utilities;

import java.util.Date;

public class MyCronJobNotificationTemplateContext implements CronJobNotificationTemplateContext {

    private final MyCustomCronJob cronJob;

    public MyCronJobNotificationTemplateContext(MyCustomCronJob cronJob) {
        this.cronJob = cronJob;
    }

    public String getCronJobName() {
        return this.cronJob.getCode();
    }

    public String getEndDate() {
        return this.cronJob.getEndTime() != null ? Utilities.getDateTimeInstance().format(this.cronJob.getEndTime()) : "n/a";
    }

    public String getDuration() {
        Date start = this.cronJob.getStartTime();
        Date end = this.cronJob.getEndTime();
        return start != null &amp;amp;&amp;amp; end != null ? Utilities.formatTime(end.getTime() - start.getTime()) : "n/a";
    }

    public String getResult() {
        return this.cronJob.getResult() != null ? this.cronJob.getResult().getCode() : "n/a";
    }

    public String getStartDate() {
        Date start = this.cronJob.getStartTime();
        return start != null ? Utilities.getDateTimeInstance().format(start) : null;
    }

    public String getStatus() {
        return this.cronJob.getStatus() != null ? this.cronJob.getStatus().getCode() : "n/a";
    }

    public String getCatalogId() {
        return this.cronJob.getCatalog() != null ? this.cronJob.getCatalog().getId() : "no luck";
    }
}
&lt;/CODE&gt;&lt;/PRE&gt;&lt;UL&gt;&lt;LI&gt;create the impex of custom renderer template &amp;amp; media and import it&lt;/LI&gt;&lt;/UL&gt;&lt;PRE&gt;&lt;CODE&gt;UPDATE GenericItem[processor = de.hybris.platform.commerceservices.impex.impl.ConfigPropertyImportProcessor]; pk[unique = true]
$emailResource = {path to your .vm files}

INSERT_UPDATE  CatalogUnawareMedia; code[unique = true]                    ; mime       ; realfilename                              ; @media[translator = de.hybris.platform.impex.jalo.media.MediaDataTranslator];
                                  ; MyCronJobFinishNotificationTemplate_en ; text/plain ; MyCronJobFinishNotificationTemplate_en.vm ; $emailResource/MyCronJobFinishNotificationTemplate_en.vm                    ;

INSERT_UPDATE RendererTemplate; code[unique = true, allownull = true]; contextClass                                            ; description[lang = en]                                                     ; content(code)[lang = en]               ; outputMimeType; rendererType(code, itemtype(code))[allownull = true];
                              ; MyCronJobFinishNotificationTemplate  ; com.test.core.jalo.MyCronJobNotificationTemplateContext ; My Custom notification template for rendering email after finished CronJob ; MyCronJobFinishNotificationTemplate_en ; text/plain    ; velocity:RendererTypeEnum                           ;
&lt;/CODE&gt;&lt;/PRE&gt;&lt;UL&gt;&lt;LI&gt;override getRendererNotificationContext() method in Jalo class of your custom cronjob model&lt;/LI&gt;&lt;/UL&gt;&lt;PRE&gt;&lt;CODE&gt;package com.test.core.jalo;

import de.hybris.platform.cronjob.jalo.CronJobNotificationTemplateContext;
import de.hybris.platform.jalo.Item;
import de.hybris.platform.jalo.JaloBusinessException;
import de.hybris.platform.jalo.SessionContext;
import de.hybris.platform.jalo.type.ComposedType;

public class MyTestCronJob extends GeneratedMyTestCronJob {

    @Override
    protected Item createItem(final SessionContext ctx, final ComposedType type, final ItemAttributeMap allAttributes) throws JaloBusinessException {
        // business code placed here will be executed before the item is created
        // then create the item
        final Item item = super.createItem(ctx, type, allAttributes);
        // business code placed here will be executed after the item was created
        // and return the item
        return item;
    }

    @Override
    protected CronJobNotificationTemplateContext getRendererNotificationContext() {
        return new MyCronJobNotificationTemplateContext(this);
    }
}&lt;/CODE&gt;&lt;/PRE&gt;&lt;UL&gt;&lt;LI&gt;rebuild your project using ant&lt;/LI&gt;&lt;LI&gt;go to the backoffice/hmc, find your CronJob&lt;/LI&gt;&lt;LI&gt;in the Task tab select the "Send notification after processing" checkbox, put emailTo address, and select your custom RendererTemplate&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/1731665-1.png" /&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;hit "save" button to save CronJob changes&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;After the cron job finished you will receive the notification with your custom template / data you put into context.&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/1731666-2.png" /&gt;&lt;/P&gt;&lt;P&gt;hope this will help.&lt;/P&gt;&lt;P&gt;Igor.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2019 11:41:02 GMT</pubDate>
      <guid>https://community.sap.com/t5/crm-and-cx-q-a/how-to-write-custom-cronjob-email/qaa-p/12118137#M416678</guid>
      <dc:creator>aimprosoft</dc:creator>
      <dc:date>2019-09-19T11:41:02Z</dc:date>
    </item>
    <item>
      <title>Re: how to write custom cronjob email</title>
      <link>https://community.sap.com/t5/crm-and-cx-q-a/how-to-write-custom-cronjob-email/qaa-p/12118138#M416679</link>
      <description>&lt;P&gt;Hi &lt;SPAN class="mention-scrubbed"&gt;aimprosoft&lt;/SPAN&gt;,&lt;/P&gt;&lt;P&gt;Thanks for the above suggestion, I have one question here, if I want to access new field in vm file, then how should I add it into to the context?&lt;/P&gt;&lt;P&gt;For example: In vm I have done&lt;BR /&gt;ListOfCronJobs: $ctx.jobNames&amp;lt;br/&amp;gt;&lt;/P&gt;&lt;P&gt;but this line itself is not executed.&lt;/P&gt;&lt;P&gt;Can you please help me out with above query?&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Abhishek Singh&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2020 07:24:43 GMT</pubDate>
      <guid>https://community.sap.com/t5/crm-and-cx-q-a/how-to-write-custom-cronjob-email/qaa-p/12118138#M416679</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2020-01-03T07:24:43Z</dc:date>
    </item>
    <item>
      <title>Re: how to write custom cronjob email</title>
      <link>https://community.sap.com/t5/crm-and-cx-q-a/how-to-write-custom-cronjob-email/qaa-p/12118139#M416680</link>
      <description>&lt;P&gt;Thank you Igor for sharing the complete solution.It gave an understanding to proceed further. Thanks again&lt;/P&gt;</description>
      <pubDate>Fri, 25 Mar 2022 11:55:39 GMT</pubDate>
      <guid>https://community.sap.com/t5/crm-and-cx-q-a/how-to-write-custom-cronjob-email/qaa-p/12118139#M416680</guid>
      <dc:creator>former_member648175</dc:creator>
      <dc:date>2022-03-25T11:55:39Z</dc:date>
    </item>
  </channel>
</rss>

