gpt4 book ai didi

c# - 使用 C# 将 sql 查询的值分配给 CRM 中的字段

转载 作者:行者123 更新时间:2023-11-28 23:50:59 27 4
gpt4 key购买 nike

我正在尝试为 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) 你可能应该为 context.OutputParameters.Contains("id") 使用 Entity.Attributes.Contains("id") 而不是 "OutputParameters"因为我怀疑你注册的插件有输出参数(这需要你将从一个单独的插件中生成它们)

4) 我不知道如何通过 C# 访问 MySQL,但我假设您需要从那个“阅读器”获取 MySQL 的数据,并使用它来填充 CRM 值。

5) 在大多数 CRM 插件中,Org Service 和 ServiceFactory 在开始时与跟踪一起被实例化,因此我建议将其向上移动得多(这一个更多地提高了可读性,因此您可以跳过它)。

6) 你的评论一直提到账户,但一开始的支票是针对“客户”的(同样,可选的,因为它只是糟糕的评论)

7) 分配的“实体”不是有效的实体名称,不应有任何空格 - 实体名称更可能类似于

var assign = new Entity("new_smsmessage")

关于c# - 使用 C# 将 sql 查询的值分配给 CRM 中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32609659/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com