簡體   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