在“创建实体”上,从外部源设置属性值,使其保持不变

问题描述 投票:2回答:2

使用Dynamics 365,我正在做很多跳跃箍以获得我需要完成的任务。

基本上对于创建联系人,我有一个插件,它将Entity对象反序列化为XML,然后将该XML发布到webhook。 webhook为另一个系统做了一些事情并获取了一个ID。然后将此ID返回给插件。

我已经设置了我想在插件中设置此ID的属性:

 var response = webClient.UploadString(serviceUrl, serializedStr);
                            postMessageImage["po_ContactCRPID"] = response.ToString();

没有错误发生,联系人确实已创建,但我有兴趣更新的字段不显示CRM中的值。

我无法在后期创建D365中使用常规webhook功能,因为发布的JSON不允许您序列化回一个对象然后能够很好地拉出值然后插入到后端的其他系统中,所以我我试图尽可能多地使用强类型的类。

有关如何实现这一目标的任何想法?在post操作管道上,我希望能够根据从某个Web服务返回的值在联系人对象(具有自定义字段)上设置属性,以便CRM可以使用我设置的值创建联系人。

这是我的插件代码:

 if (context.PostEntityImages.Contains("CreateContactImage") && context.PostEntityImages["CreateContactImage"] is Entity)
                {
                    tracingService.Trace("AccountSync: CreateContactImage.");

                    Entity postMessageImage = (Entity)context.PostEntityImages["CreateContactImage"];
 using (var client = new WebClient())
                    {
                        var webClient = new WebClient();
                        webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";

                        var code = "CodeFromPlugin";
                        var serviceUrl = this.CRPSyncServiceUrl + "?code=" + code;

                        var entitySeri = new EntitySerializer();
                        var serializedStr = entitySeri.SerializeObject(postMessageImage);
try
                        {
                            // upload the data using Post mehtod
                             //var response = webClient.UploadData(serviceUrl, entityBytes);
                            var response = webClient.UploadString(serviceUrl, serializedStr);
                            postMessageImage["po_ContactCRPID"] = response.ToString();
                            postMessageImage.Attributes["po_ContactCRPID"] = response.ToString();
                            tracingService.Trace("Set postMessageImage po_ContactCRPID to: {0}", response.ToString());
                        }
                        catch (Exception ex)
                        {
                            tracingService.Trace("WebEX Error: {0}", ex.ToString());
                            throw;
                        }
                    }

                }
c# dynamics-crm dynamics-crm-online dynamics-365 dynamics-crm-365
2个回答
3
投票

在异步后创建插件中,您将在目标中拥有EntityId,它刚刚创建了Contact。

从webhook响应中获取ID,然后编写一个新的Contact对象,设置属性&service.Update将保存它。

Entity contact = new Entity(“contact”);
contact.Id = target.Id;
contact[“webhook_Idfield”] = ID;
service.Update(contact);

2
投票

image不是target行动的create

image只为您提供特定事件之前或之后记录值的只读快照。

target通过事件管道传递。 target表示可保存到数据库中的可编辑对象。

例如;如果你考虑一个CreateRequest,它有一个target财产。该请求基本上是用户保存记录时发生的情况。

Entity contact = new Entity("contact");
contact["firstname"] = "James";

CreateRequest cr = new CreateRequest
{
    Target = contact
};

您可以在插件中访问target,如下所示:

Entity target = (Entity)context.InputParameters["Target"];
target.GetAttributeValue<string>("firstname"); //James

您可以在target上设置值,如下所示:

target["lastname"] = "Wood";

如果您的插件已注册为同步和事件前,“Wood”将传递到事件管道并进入数据库。否则(例如事件后,异步)当你设置target时为时已晚 - 数据已经保存到数据库中。

如果你能满足这些条件,请切换到使用target。否则你需要发出一个单独的UpdateRequest

Entity update = new Entity("contact");
update.Id = target.Id;
update["lastname"] = "Wood";

Service.Update(update);
© www.soinside.com 2019 - 2024. All rights reserved.