简体   繁体   English

使用 C# 更新 CRM 中的实体字段

[英]Update an Entity field in CRM using C#

I am trying to update a field value of an entity in CRM using C#, but I am not sure how I should do it.我正在尝试使用 C# 更新 CRM 中实体的字段值,但我不知道该怎么做。

Below are my current code:以下是我当前的代码:

var helper = new CRMConnection();
var service = helper.OpenConnection();

var entiyRef = new Entity("financialaid");
string fetchXML = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                      <entity name='financialaid'>
                        <attribute name='financialaidid' />
                        <attribute name='emailcontent' />
                      </entity>
                    </fetch>";

var retrieved = service.RetrieveMultiple(new FetchExpression(fetchXML));
if (retrieved != null && retrieved.Entities.Count > 0){
    for (var k = 0; k < retrieved .Entities.Count; i++){
        var emailContent = retrieved .Entities[i].GetAttributeValue<string>("emailcontent");
        entiyRef["emailcontent"] = "test";
        service.Update(entiyRef);
    }
}

The code above returned over 5000 records, which I suppose that it is tied to every record I have in CRM.上面的代码返回了超过5000条记录,我想它与我在 CRM 中的每条记录相关联。 But I am just trying to update a standalone field in the entity.但我只是想更新实体中的独立字段。

After which, it hit the following error and exited.之后,它遇到以下错误并退出。

Exception thrown: 'System.ServiceModel.FaultException`1' in Microsoft.Xrm.Sdk.dll抛出异常:Microsoft.Xrm.Sdk.dll 中的“System.ServiceModel.FaultException`1”

May I know how can I correctly update a field in CRM using FetchXML?我可以知道如何使用 FetchXML 正确更新 CRM 中的字段吗?

attributeId = 0AB038CE-E57E-EA11-A811-000D3AA3E77C
entityId = b6abe6f6-a876-4a80-87d6-2f1b179d437f

First problem is that you are instantiating the entityRef as new Entity("financialaid") but you never assign the Id property of that object, so the service.Update(entityRef) call will try to update a record of the financialaid entity that has financialaidid = Guid.Empty , which should not exist.第一个问题是您将entityRef实例化为new Entity("financialaid")但您从未分配该 object 的Id属性,因此service.Update(entityRef)调用将尝试更新具有 Financialaidid 的financialaidid = Guid.Empty援助实体的记录financialaidid = Guid.Empty ,它不应该存在。

The looping of results could be greatly simplified using some Linq expressions or simply a foreach.使用一些 Linq 表达式或简单的 foreach 可以大大简化结果的循环。 The looping also does not really make sense, since you are updating the same record every time (the financialaid record with empty id).循环也没有真正意义,因为您每次都在更新相同的记录(id 为空的金融援助记录)。

If what you are trying to accomplish is to update every record of entity financialaid and set the emailcontent attribute to "test", put this in your loop:如果您要完成的是更新实体财务援助的每条记录并将 emailcontent 属性设置为“测试”,请将其放入循环中:

var entityRef = new Entity(retrieved.Entities.LogicalName, retrieved.Entities.Id);
entityRef["emailcontent"] = "test";
service.Update(entityRef);

Note that FetchXML is only a query language , you cannot use FetchXML to update records.请注意,FetchXML 只是一种查询语言,您不能使用 FetchXML 来更新记录。 Think of it as the select statement of SQL.将其视为 SQL 的 select 语句。

Finally, if you just want to bulk update all financialaid records to set the value of a field, I would recommend using an existing tool for that instead of writing a one-off app to do it.最后,如果您只想批量更新所有财务援助记录以设置字段的值,我建议您使用现有工具而不是编写一次性应用程序来完成。 In this case the Bulk Data Updater tool in the XrmToolBox would do the job.在这种情况下, XrmToolBox中的Bulk Data Updater工具可以完成这项工作。

You're creating the emailContent field in a wrong entity.您在错误的实体中创建 emailContent 字段。 If you have over 5,000 records in that entity, that entity is likely to store transactional data.如果您在该实体中有超过 5,000 条记录,则该实体可能会存储交易数据。

I would suggest you to use a custom configuration entity (if there is, else create a configuration entity) that have a key field, and a value field.我建议您使用具有键字段和值字段的自定义配置实体(如果有,则创建配置实体)。

Give a unique name to the key, and then store html table into that value field.为键指定一个唯一名称,然后将 html 表存储到该值字段中。

Retrieve that record by the key, you can then update/read the html table from the value field.通过键检索该记录,然后您可以从值字段更新/读取 html 表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM