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

时间:2018-03-23 18:05:30

标签: cron sap sendmail hybris

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

然后我创建productsApprovedEmailProcess来定义Process Engine执行的步骤,如下所示

然后我添加了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允许您为Subject和Body创建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

1 个答案:

答案 0 :(得分:4)

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

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

e.g。

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   ;