You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AlexaCRM Toolkit获取Dynamics 365中特定联系人的全部发票

刚看了你的问题,现在的脚本确实是拉取了全系统最新10张发票,要改成只获取特定联系人的关联发票很简单,咱们可以通过添加过滤条件来实现,给你两种常用的方法:

方法一:使用QueryExpression(推荐)

这种方式类型安全,代码可读性更高,是AlexaCRM Toolkit推荐的查询方式:

// 替换成你需要查询的目标联系人GUID
$targetContactId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

// 初始化发票实体的查询表达式
$query = new AlexaCRM\CRMToolkit\Query\QueryExpression("invoice");

// 添加核心过滤条件:
// 1. 发票的关联客户ID等于目标联系人ID
$query->addCondition("customerid", AlexaCRM\CRMToolkit\Query\ConditionOperator::EQUAL, $targetContactId);
// 2. 确保关联的客户类型是联系人(避免匹配到同名账户)
$query->addCondition("customeridtype", AlexaCRM\CRMToolkit\Query\ConditionOperator::EQUAL, "contact");

// 可选:如果需要限制返回数量,取消下面这行注释
// $query->setTopCount(10);

// 执行查询
$invoices = $service->retrieveMultiple($query);

// 遍历输出结果
foreach ($invoices->Entities as $invoice) {
    echo 'ID : ' . $invoice->ID . '<br>';
    echo 'Name : ' . $invoice->name . '<br>';
}
方法二:使用FetchXML

如果你更习惯用FetchXML(比如从Dynamics 365高级查找里导出的查询语句),可以这样修改:

// 替换成目标联系人的GUID
$targetContactId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

// 定义FetchXML模板,替换占位符为实际联系人ID
$fetchXml = str_replace(
    "TARGET_CONTACT_ID",
    $targetContactId,
    '<fetch top="10">
        <entity name="invoice">
            <attribute name="invoiceid"/>
            <attribute name="name"/>
            <filter>
                <condition attribute="customerid" operator="eq" value="TARGET_CONTACT_ID"/>
                <condition attribute="customeridtype" operator="eq" value="contact"/>
            </filter>
        </entity>
    </fetch>'
);

// 传入FetchXML执行查询
$invoices = $service->retrieveMultipleEntities(
    "invoice",
    $allPages = false,
    $pagingCookie = null,
    $limitCount = 10,
    $pageNumber = 1,
    $simpleMode = false,
    $fetchXml
);

// 遍历输出结果
foreach ($invoices->Entities as $invoice) {
    echo 'ID : ' . $invoice->ID . '<br>';
    echo 'Name : ' . $invoice->name . '<br>';
}

几个关键注意点:

  • 一定要加上customeridtype的过滤,因为Dynamics 365里的customerid是多实体 lookup,可能关联联系人或账户,避免拿到不属于目标联系人的发票。
  • 如果需要获取全部关联发票,记得去掉top="10"或者setTopCount(10),同时把$allPages参数设为true,这样会自动分页拉取所有结果。

内容的提问来源于stack exchange,提问作者Mssef

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:09:19