cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

how to write custom cronjob email

Jyothi_Remalli
Explorer
0 Likes
3,332

Hi,

I have a requirement of modifying the custom cronjob email template with some additional parameters in the cronjob.

If anyone have sample code, please provide.

Thanks

Jyothi

Accepted Solutions (0)

Answers (3)

Answers (3)

aimprosoft
Participant

Hi Jyothi,

Just for example - I've added one of the custom fields (catalog id) of custom cronjob model into the custom email template.

Custom cron job model:

<itemtype code="MyTestCronJob" extends="CronJob"
   generate="true" autocreate="true">
   <attributes>
      <attribute qualifier="catalog" type="Catalog">
         <description>Some catalog</description>
         <persistence type="property" />
         <modifiers optional="true" />
      </attribute>
   </attributes>
</itemtype>

please check steps / sample code below

  • create the custom template.vm
{yourcoreextension}/import/emails/MyCronJobFinishNotificationTemplate_en.vm
  • fill your custom template file with data
<html>
   <head>
   </head>
   <body>
      this is my custom field from cron job model: $ctx.catalogId
   </body>
</html>
  • create the custom context class
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 && 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";
    }
}
  • create the impex of custom renderer template & media and import it
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                           ;
  • override getRendererNotificationContext() method in Jalo class of your custom cronjob model
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);
    }
}
  • rebuild your project using ant
  • go to the backoffice/hmc, find your CronJob
  • in the Task tab select the "Send notification after processing" checkbox, put emailTo address, and select your custom RendererTemplate

  • hit "save" button to save CronJob changes

After the cron job finished you will receive the notification with your custom template / data you put into context.

hope this will help.

Igor.

0 Likes

Thank you Igor for sharing the complete solution.It gave an understanding to proceed further. Thanks again

0 Likes

Hi aimprosoft,

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?

For example: In vm I have done
ListOfCronJobs: $ctx.jobNames<br/>

but this line itself is not executed.

Can you please help me out with above query?

Thanks,

Abhishek Singh