繁体   English   中英

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

[英]Passing values from Dynamics CRM entity form to external web service through plug-in execution method

我想在创建新记录时将实体(案例/事件)的某些值传递给外部 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。

编辑:

我试图获得如下值,但在 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

谢谢。

首先,您应该在 Dynamics 中将您的插件注册为 Post 操作(创建)。 原因一旦在系统中创建记录,您将获得它的 Guid 等。 这是最好的方法,此外还使您的插件异步(仅在您的用例真正必须时才同步)。

现在,当您在 crm 插件中创建记录时,您将获得它的上下文。

 var entity = (Entity)context.InputParameters["Target"];

现在您可以获得特定的文件值,您可以执行以下操作

  if(entity.contains("field name")){
    var recordName=entity.GetAttributeValue<string>("field name");
    }

如果您想要选项集值,您可以执行以下操作

if(entity.contains("optionset field name")){
    int selectedTopic = entity.GetAttributeValue<OptionSetValue>("optionset field name").Value
String text = entity.FormattedValues["optionset field name"].ToString();


    }

建立? 你想设置什么类型的数据,假设你想设置选项集值

entity["X"] = new OptionSetValue(INDEX)

INDEX 是一个整数,您可以在选项集编辑器中查找(默认值为几位长)。

暂无
暂无

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

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