使用c#将sql查询中的值赋给CRM中的字段
本文关键字:CRM 字段 sql 查询 使用 | 更新日期: 2023-09-27 18:12:55
我试图为CRM创建一个插件,查询mysql数据库并从数据库中提取数据,并将其添加到CRM中的一个字段。到目前为止,我所拥有的是下面这一点,但我不确定如何在crm中分配一个表列的字段值?
`namespace Microsoft.Crm.Sdk.Samples
{
public class AssignSMSPlugin: IPlugin
{
/// <summary>
/// A plug-in that creates a follow-up task activity when a new account is created.
/// </summary>
/// <remarks>Register this plug-in on the Create message, account entity,
/// and asynchronous mode.
/// </remarks>
public void Execute(IServiceProvider serviceProvider)
{
//Conect to mySQl Database
String str ="";
MySqlConnection con = null;
MySqlDataReader reader = null;
try
{
con = new MySqlConnection(str);
con.Open();
//Select statement to query table to get certain columns
string cmdText = "SELECT col1, col2, col3 FROM table1";
MySqlCommand cmd = new MySqlCommand(cmdText, con);
reader = cmd.ExecuteReader();
// while (reader.Read())
// {
// Console.WriteLine(reader.GetString(0));
// }
//Extract the tracing service for use in debugging sandboxed plug-ins.
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity entity = (Entity)context.InputParameters["Target"];
// Verify that the target entity represents an account.
// If not, this plug-in was not registered correctly.
if (entity.LogicalName != "customer")
return;
try
{
// Create a sms activity.Assign table column to field in crm
Entity assign = new Entity("SMS Message");
assign["to"] = "";
assign["subject"] = "";
assign["contact"] = ;
assign["regardingobjectid"] = ;
// Refer to the account in the task activity.
if (context.OutputParameters.Contains("id"))
{
Guid regardingobjectid = new Guid(context.OutputParameters["id"].ToString());
string regardingobjectidType = "customer";
assign["regardingobjectid"] =
new EntityReference(regardingobjectidType, regardingobjectid);
}
// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// Create the task in Microsoft Dynamics CRM.
tracingService.Trace("AssignSMSPlugin: Creating the SMS Activity.");
service.Create(assign);
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in the AssignSMSPlugin plug-in.", ex);
}
catch (Exception ex)
{
tracingService.Trace("AssignSMSPlugin: {0}", ex.ToString());
throw;
}
}
}
}`
我看你的逻辑有问题。
1)改变这里的部分
assign["to"] = "";
assign["subject"] = "";
assign["contact"] = ;
assign["regardingobjectid"] = ;
你需要实际赋值,而不是空/空字符串。
2)在任何实体上通常都有某种类型的主字段(通常是名称/主题/标题);应该设置(我假设这是主题字段,目前是= "),否则它会回来咬你。
3)你可能应该使用Entity.Attributes.Contains("id")而不是"OutputParameters" context.OutputParameters.Contains("id"),因为我怀疑你注册的插件有输出参数(这需要你从一个单独的插件生成它们)
4)我不知道如何通过c#访问MySQL,但我假设你需要从MySQL中从"阅读器"获取数据,并使用它来填充CRM值。
5)在大多数CRM插件中,Org Service和ServiceFactory都是在跟踪开始时实例化的,所以我建议将其移到更高的位置(这是一个更好的可读性,所以你可以跳过它)。
6)你的评论一直提到账户,但开头的检查是针对"客户"的(同样,可选的,因为它只是糟糕的评论)
7)赋值的"Entity"不是一个有效的实体名称,不应该有任何空格——更可能的是实体名称类似于
var assign = new Entity("new_smsmessage")