检查是否检索到数据- CRM

本文关键字:CRM 数据 是否 检索 检查 | 更新日期: 2023-09-27 18:04:48

我正在从CRM检索数据。我想从quotedetail中检索几个字段。其中一个是intad_discountpercent

该字段不需要有值

我需要检查是否有一些值从该字段检索,并给它一个默认值0,如果没有值。

下面是我的代码:
        string fetch1 = @"
                  <fetch count='50' >
                      <entity name='quotedetail' >
                        <attribute name='manualdiscountamount' />
                        <attribute name='priceperunit' />
                        <attribute name='ad_discountpercent' />
                        <attribute name='quantity' />
                        <attribute name='extendedamount' />
                      </entity>
                    </fetch>";
        EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetch1));
        foreach (var c in result.Entities)
        {
             if(...)
        }

我应该把什么代替(...),看看是否有任何数据检索并给出默认值?如果你知道这两件事中的一点,那将会很有帮助。

检查是否检索到数据- CRM

如果您需要其他信息请告诉我。

访问字段值可以使用GetAttributeValue<T>(string attribute logical name)

在你的例子中:

c.GetAttributeValue<Entity>("ad_discountpercent");

不清楚是要更新Entity对象(即将其保存回CRM)还是只需要处理该值。

if (!c.Attributes.Contains("ad_discountpercent"))
{
    var newEntity = new Entity(c.LogicalName, c.Id)
    newEntity["ad_discountpercent"] = 0; //replace 0 with your default value.   
}