SAP Hybris 电子商务:在 CronJob 中发送电子邮件

SAP Hybris E-commerce : Send Email in CronJob

我创建了一个完美运行的 CronJob。 但我想在此 Cronjob 内生成电子邮件发送。我在互联网上关注了 tutorial。 我从 ProductsApprovedEmailProcess.

的创建 itemType 开始

然后我创建了 productsApprovedEmailProcess 来定义流程引擎执行的步骤如下

然后我添加了一个 EmailContext 来保存要传递给电子邮件模板的数据,如下所示

public class ProductsApprovedEmailContext extends CustomerEmailContext
{

private String message;

@Override
public void init(final StoreFrontCustomerProcessModel processModel, final EmailPageModel emailPageModel)
{
    super.init(processModel, emailPageModel);
    if (processModel instanceof ProductsApprovedEmailProcessModel)
    {
        setMessage(((ProductsApprovedEmailProcessModel) processModel).getMessage());
    }
}


public String getMessage()
{
    return message;
}


public void setMessage(final String message)
{
    this.message = message;
}
}

并且我已经在 Spring 中将 ProductsApprovedEmailContext 注册为 bean,如下所示

    <bean id="productsApprovedEmailContext" class="com.hybris.training.facades.process.email.context.ProductsApprovedEmailContext"
        parent="abstractEmailContext"
        scope="prototype" >
</bean>

然后我创建了 2 个 Velocity 模板,一个用于电子邮件主题,另一个用于正文 email-productsapproved-subject.vmemail-productsapproved-body.vm

下面的 impex 允许您为主题和正文创建 RendererTemplates,并将它们附加到 EmailPageTemplate,如下所示

$contentCatalog=electronicsContentCatalog
$contentCV=catalogVersion(CatalogVersion.catalog(Catalog.id[default=$contentCatalog]),CatalogVersion.version[default=Online])[default=$contentCatalog:Online]
UPDATE GenericItem[processor=de.hybris.platform.commerceservices.impex.impl.ConfigPropertyImportProcessor];pk[unique=true]
$emailResource=$config-emailResourceValue
$emailPackageName=$config-emailContextPackageName
$lang=en
INSERT_UPDATE RendererTemplate  ;code[unique=true]                  ;contextClass                                   ;templateScript[lang=en,translator=de.hybris.platform.commerceservices.impex.impl.FileLoaderValueTranslator];rendererType(code)[default='velocity']
                            ;email-productsapproved-body        ;$emailPackageName.ProductsApprovedEmailContext ;$emailResource/email-productsapproved-body.vm
                            ;email-productsapproved-subject     ;$emailPackageName.ProductsApprovedEmailContext ;$emailResource/email-productsapproved-subject.vm
INSERT_UPDATE EmailPage ;$contentCV[unique=true];uid[unique=true]       ;masterTemplate(uid,$contentCV) ;approvalStatus(code)[default='approved']
                    ;                       ;ProductApprovedEmail   ;ProductApprovedEmailTemplate   ;

并且在 Cronjob 我添加了这段代码!

    final ProductsApprovedEmailProcessModel productsApprovedEmailProcessModel = (ProductsApprovedEmailProcessModel) businessProcessService
            .createProcess("productsApprovedEmailProcess" + "-" + System.currentTimeMillis(), "productsApprovedEmailProcess");
    productsApprovedEmailProcessModel.setMessage("Products approved in csv file");
    productsApprovedEmailProcessModel.setSite(baseSiteService.getBaseSiteForUID("electronics"));
    productsApprovedEmailProcessModel.setLanguage(CommerceCommonI18NService.getCurrentLanguage());
    modelService.save(productsApprovedEmailProcessModel);
    businessProcessService.startProcess(productsApprovedEmailProcessModel);

但是,当我使用 HMC 接口执行 CronJob 时,出现了这个错误:

 Error executing ActionNode with ID [generateProductsApprovedEmail]: HtmlTemplate associated with MasterTemplate of EmailPageModel cannot be null

更新:

这是我的业务流程:

<process xmlns="http://www.hybris.de/xsd/processdefinition"
     start="generateProductsApprovedEmail"
     name="productsApprovedEmailProcess"
     processClass="com.hybris.training.core.model.process.ProductsApprovedEmailProcessModel"
     onError="error">


    <action id="generateProductsApprovedEmail" bean="generateProductsApprovedEmail">
        <transition name="OK" to="sendEmail"/>
        <transition name="NOK" to="error"/>
    </action>        


    <action id="sendEmail" bean="sendEmail">
        <transition name="OK" to="removeSentEmail"/>
        <transition name="NOK" to="failed"/>
    </action>

    <action id="removeSentEmail" bean="removeSentEmail">
        <transition name="OK" to="success"/>
        <transition name="NOK" to="error"/>
    </action>


    <end id="error" state="ERROR">Something went wrong.</end>
    <end id="failed" state="FAILED">Could not send products approved in csv File email.</end>
    <end id="success" state="SUCCEEDED">Sent file in email.</end>

声明 ProductApprovedEmailTemplate (EmailPageTemplate) 后,我收到此警告并且未生成邮件:

WARN  [TaskExecutor-master-264-ProcessTask [8796715713462]] [GenerateEmailAction] Could not retrieve email page model for ProductApprovedEmail and Electronics Content Catalog:Online, cannot generate email content

看来,你关注的博客,每一步都说对了,但你可能漏掉了什么。

确保您已正确执行以下步骤。

例如

frontendTemplateName应该匹配EmailPageTemplate一个

<bean id="generateProductApprovedEmail" parent="abstractGenerateEmailAction">
    <property name="frontendTemplateName" value="ProductApprovedEmail"/>
</bean>

创建电子邮件页面模板

INSERT_UPDATE EmailPageTemplate ;$contentCV[unique=true];uid[unique=true]       ;active ;frontendTemplateName   ;subject(code)                          ;htmlTemplate(code)     ;restrictedPageTypes(code)
                                ;                       ;ProductApprovedEmailTemplate   ;true   ;ProductApprovedEmail   ;email-productsapproved-subject             ;email-productsapproved-body    ;EmailPage

创建电子邮件页面

   INSERT_UPDATE EmailPage  ;$contentCV[unique=true];uid[unique=true]   ;masterTemplate(uid,$contentCV);approvalStatus(code)[default='approved']
                            ;                       ;ProductApprovedEmail   ;ProductApprovedEmailTemplate   ;