[AX]AX2012 AIF(六):为FORM添加发送电子文档功能

对于一些交易日记账,我们可能需要生成XML格式的电子档,比如财务的General journal ,比较方便的做法是在“General journal”窗口中添加按钮,点击时调用文档服务生成XML文件,这里不关注文档服务如何实现,而是着重于如何使用X++调用文档服务。

以Ledger general journal为例,第一步我们需要配置服务端口,用到的是LedgerGeneralJournalService.read服务读取操作,我们是从程序中导出文档,不需要接收消息,因此我们只需要一个出站(Outband)端口,使用文件系统适配器创建一个出站端口,设置输出文件目录,选择LedgerGeneralJournalService.read服务操作,激活端口。

在程序中调用这个端口服务前,我们可能要检查服务端口是否真正存在并激活,编写一个X++函数来判断,把这个函数放到表LedgerTable上方便共享使用:

boolean canXMLBeSent()
{
    boolean ret;
    AifActionId actionId;
    AifEndpointList endpointList;
    AifConstraint aifConstraint = new AifConstraint();
    AifConstraintList aifConstraintList = new AifConstraintList();
    ;

    actionId = AifSendService::getDefaultSendAction(classnum(LedgerGeneralJournalService), 
        AifSendActionType::SendByKey);//得到LedgerGeneralJournalService.read操作

    if(actionId)
    {
        aifConstraint.parmType(AifConstraintType::NoConstraint);
        aifConstraintList.addConstraint(aifConstraint);

        endpointList = AifSendService::getEligibleEndpoints(actionId, 
            aifConstraintList);//寻找包含LedgerGeneralJournalService.read操作的AIF端口
        if(endpointList.getEndpointCount()>0)
        {
            ret = true;
        }
    }
    return ret;
}

在LedgerJournalTable表上再创建一个方法调用服务:

void sendElectronically(XMLDocPurpose _xMLDocPurpose, 
                        AifSendMode _aifSendMode = AifSendMode::Async)
{
    AxdSendContext axdSendContext = AxdSendContext::construct();
    AifEntityKey aifEntityKey = AifEntityKey::construct();
    Map keyData;
    AifConstraintList aifConstraintList = new AifConstraintList();
    AifConstraint aifConstraint = new AifConstraint();
    ;
    keyData = SysDictTable::getKeyData(this);

    aifEntityKey.parmTableId(this.TableId);
    aifEntityKey.parmRecId(this.RecId);//直接使用记录RecId
    aifEntityKey.parmKeyDataMap(keyData);

    axdSendContext.parmXMLDocPurpose(_xMLDocPurpose);
    axdSendContext.parmSecurity(false);

    aifConstraint.parmType(AifConstraintType::NoConstraint) ;
    aifConstraintList.addConstraint(aifConstraint) ;

    AifSendService::submitDefault(
        classnum(LedgerGeneralJournalService),
        aifEntityKey,
        aifConstraintList,
        _aifSendMode,
        axdSendContext.pack());
}


可以在AifSendService::submitDefault()的参数中指定是否并行处理以及消息的会话ID。

剩下要做的就是在LedgerJournalTable form上新建工具栏来调用这两个方法:

void clicked()
{
    LedgerJournalTable  ledgerJournalTableLocal;
    ;

    for (ledgerJournalTableLocal = (LedgerJournalTable_ds.getFirst(true) ?
            LedgerJournalTable_ds.getFirst(true) : LedgerJournalTable);
            ledgerJournalTableLocal;
            ledgerJournalTableLocal = LedgerJournalTable_ds.getNext())

    {
        // 检查是否可以发送
        if (ledgerJournalTableLocal.canXMLBeSent())
        {
            //发送文档
            ledgerJournalTableLocal.sendElectronically(XMLDocPurpose::Original);
         }
        else
        {
            warning (strfmt("@SYS72175"));
        }
    }

}

在LedgerJournalTable窗口中我们可以选择多个Journal记录发送,如果你点击按钮却没有在输出目录找到文档,检查一下是否有Batch job来运行AifGatewayReceiveService, AifGatewaySendService, AifInboundProcessingService, 和AifOutboundProcessingService这个几个服务。

原文地址:https://www.cnblogs.com/duanshuiliu/p/2886340.html