简体   繁体   English

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

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

I want to pass to external web service some values of an entity (Case/incident) while new record is going to be created.我想在创建新记录时将实体(案例/事件)的某些值传递给外部 Web 服务。

I have a model for preparing data which have to be sent to web service as below:我有一个用于准备必须发送到 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; }
}

Here is my code inside Execute() method:这是我在 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. My first question is how to retrieve intended fields value on Create new record?我的第一个问题是如何在创建新记录时检索预期字段值? I have used entity["X"] to get value of "X" field but nothing returned.我已经使用 entity["X"] 来获取 "X" 字段的值,但没有返回任何内容。
  2. My second question is how to set value of a field on Update a record?我的第二个问题是如何在更新记录时设置字段的值? Using same expression ( entity["X"] = "NewValue" ) not worked for me.使用相同的表达式( entity["X"] = "NewValue" )对我不起作用。

Note: sample static data has sent to web service successfully and it returned true as result.注意:示例静态数据已成功发送到 Web 服务,结果返回 true。

EDIT:编辑:

I tried to get values as below but have error in CRM create record event.我试图获得如下值,但在 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();

Error:错误:

Unable to cast object of type Microsoft.Xrm.Sdk.OptionSetValue to type Microsoft.Xrm.Sdk.EntityReference无法将 Microsoft.Xrm.Sdk.OptionSetValue 类型的对象转换为 Microsoft.Xrm.Sdk.EntityReference

Thanks.谢谢。

First, You should register your plugin in Dynamics as Post operation (create).首先,您应该在 Dynamics 中将您的插件注册为 Post 操作(创建)。 Reason once the record is created in System, you will get it's Guid and so on.原因一旦在系统中创建记录,您将获得它的 Guid 等。 This is best way and in addition make your plugin Asynchronous (syn only if it is a real must for your use case).这是最好的方法,此外还使您的插件异步(仅在您的用例真正必须时才同步)。

Now when you create a recrod in crm plugin will get it's context as you are doing.现在,当您在 crm 插件中创建记录时,您将获得它的上下文。

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

now you can get particualr fileds value, you do something like below现在您可以获得特定的文件值,您可以执行以下操作

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

if you want optionset values you do something like below如果您想要选项集值,您可以执行以下操作

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


    }

To set up?建立? what type of data you want to set up, assuming you want to set up optionset value你想设置什么类型的数据,假设你想设置选项集值

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

The INDEX is an int you can look up in your optionset editor (default values are several digit long). INDEX 是一个整数,您可以在选项集编辑器中查找(默认值为几位长)。

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

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