[英]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);
}
注意:示例静态数据已成功发送到 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.