通过插件执行方法将值从Dynamics CRM实体表单传递到外部Web服务

问题描述 投票:0回答:1

我要在创建新记录时将某些实体的值(大小写/事件)传递给外部Web服务。

我有一个准备要发送到Web服务的数据的模型,如下所示:

public class TicketViewModel
{
    public string CaseID { get; set; }
    public string Subject { get; set; }
    public string Description { get; set; }
    public string CreateTime { get; set; }
    public string Owner { get; set; }
    public string States { get; set; }
    public string Assigned { get; set; }
}

这是我的Execute()方法中的代码:

IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

            IOrganizationService service = factory.CreateOrganizationService(context.UserId);

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                try
                {
                    var entity = (Entity)context.InputParameters["Target"];
                    if (entity.LogicalName != "incident") // The logical name for Case entity
                        return;

                    Guid recordID = entity.Id;

                    var ticket = new CaseViewModel
                    {
                        // Retrieving Intended Fields Value
                    };

                    BasicHttpBinding httpBinding = new BasicHttpBinding();
                    httpBinding.Name = "HttpBinding_Service";
                    httpBinding.Security.Mode = BasicHttpSecurityMode.None;
                    httpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
                    httpBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
                    httpBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;

                    EndpointAddress epa = new EndpointAddress(@"webservice/url/address");
                    CallChamberPortalSoapClient tcClient = new CallChamberPortalSoapClient(httpBinding, epa);

                    var res = tcClient.addTicket(//Passing Intended Fields Value);

                    entity["X"] = res.ToString();
                }
                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException("Failed to register ticket by this error: " + ex.Message);
                }
  1. 我的第一个问题是如何在创建新记录时检索预期的字段值?我已经使用entity [“ X”]来获取“ X”字段的值,但未返回任何内容。
  2. 我的第二个问题是如何在更新记录上设置字段的值?使用相同的表达式(entity [“ X”] =“ NewValue”)对我不起作用。

注意:示例静态数据已成功发送到Web服务,并且结果返回true。

EDIT:] >>

我尝试获取以下值,但在CRM创建记录事件中出错。

ColumnSet cs = new ColumnSet(new string[] {
   "ticketnumber", "title", "description", "createdon", "customerid", "new_peygiriii", "createdby" });
    Entity wholeCase = service.Retrieve("incident", recordID, cs);

Owner = wholeCase.GetAttributeValue<EntityReference>("customerid").ToString();

错误:

无法将类型为Microsoft.Xrm.Sdk.OptionSetValue的对象转换为类型Microsoft.Xrm.Sdk.EntityReference

谢谢。

我想在创建新记录时将某个实体的某些值(案例/事件)传递给外部Web服务。我有一个模型,用于准备必须以...

c# dynamics-crm-2015
1个回答
0
投票

[首先,您应该在Dynamics中将您的插件注册为Post操作(创建)。原因一旦在System中创建了记录,您将得到它的Guid,依此类推。这是最好的方法,此外还可以使您的插件异步(仅在您的用例确实是必须的情况下才同步)。

© www.soinside.com 2019 - 2024. All rights reserved.