繁体   English   中英

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

[英]Assign value from sql query to a field in CRM using C#

我试图为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)assign“ Entity”不是有效的实体名称,不应有任何空格-实体名称更像是

var assign = new Entity("new_smsmessage")

暂无
暂无

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

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