on ‎2019 Sep 16 2:42 PM
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
Request clarification before answering.
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
{yourcoreextension}/import/emails/MyCronJobFinishNotificationTemplate_en.vm<html>
<head>
</head>
<body>
this is my custom field from cron job model: $ctx.catalogId
</body>
</html>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";
}
}
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 ;
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);
}
}
After the cron job finished you will receive the notification with your custom template / data you put into context.

hope this will help.
Igor.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you Igor for sharing the complete solution.It gave an understanding to proceed further. Thanks again
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 1 | |
| 1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.