【问题标题】:Passing values from Dynamics CRM entity form to external web service through plug-in execution method通过插件执行方法将值从 Dynamics CRM 实体表单传递到外部 Web 服务
【发布时间】:2020-02-09 08:32:44
【问题描述】:

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

谢谢。

【问题讨论】:

    标签: c# dynamics-crm-2015


    【解决方案1】:

    首先,您应该在 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 是一个 int,您可以在选项集编辑器中查找(默认值是几位长)。

    【讨论】:

    • 感谢@AnkUser,获取选项集字段的值已解决。您能否让我知道获取 Customer 和 lk_incidentbase_createdby 的属性值?当我使用 entity.GetAttributeValue("customerid") 时,出现语法错误,因为 Customer 是 IPlugin 执行方法中的未知类。与 entity.GetAttributeValue("createdby"). 相同
    • 获取 createdby 的值已解决为 GetAttributeValue("createdby").Name。但是对于 customerid 还没有。
    • 试试这个 entity.GetAttributeValue(“createdby”).Id 和 entity.GetAttribute(“createdby”).name
    • 它返回了客户的 ID,所以我必须通过实体 customer = service.Retrieve("customer entity logical name", customerid, columnset); 获取客户名称那么客户实体的逻辑名称是什么?非常感谢
    • 您必须通过进入您的定制和客户实体来找出答案。只需谷歌它,你会发现如何得到它google.com/amp/s/taagung.com/dynamics-365-understanding-name/…
    猜你喜欢
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    相关资源
    最近更新 更多