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

Posted

技术标签:

【中文标题】SAP Hybris 电子商务:在 CronJob 中发送电子邮件【英文标题】:SAP Hybris E-commerce : Send Email in CronJob 【发布时间】:2018-09-02 11:58:18 【问题描述】:

我创建了一个完美运行的 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

【问题讨论】:

您是否声明了 ProductApprovedEmailTemplate (EmailPageTemplate)? 【参考方案1】:

看起来,你关注的博客,它正确地提到了每一步,但你可能会错过一些东西。

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

例如

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   ;

【讨论】:

helo,感谢您的反馈。首先这是问题,但在更改我的代码后,我总是收到此警告 WARN [TaskExecutor-master-255-ProcessTask [8796715647926]] [GenerateEmailAction] 无法检索 ProductApprovedEmail 和电子内容目录的电子邮件页面模型:在线,不能生成电子邮件内容 在帖子中添加业务流程和其他剩余的 Impex 确保您已同步内容目录。您可以从HMCHMC 交叉检查您的 EmailPage 是否在在线版本中可用 当我在选项卡 System-> Types 中搜索时,搜索条件标识符为 EmailPage,结果是 EmailPage 和 EmailPageTemplate,extensionNameAcceleratorServices 检查 WCMS 选项卡 > EmailPage,如果无法获取请联系我。

以上是关于SAP Hybris 电子商务:在 CronJob 中发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

SAP Hybris Commerce - 初始化参考属性的默认值

#私藏项目实操分享# SAP Commerce Cloud 里的 Solr 架构简介

如何在特定时间触发hybris中的cronjob

解释如何在 Hybris 中创建 Cron Job

Hybris电商方案介绍(企业全渠道) B2B B2C O2O建设

SAP Hybris的build callback和SAP ABAP的SGEN事务码