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

时间:2015-09-16 13:14:31

标签: c# mysql crm dynamics-crm-2015

我试图为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 个答案:

答案 0 :(得分:1)

你的逻辑对我来说很糟糕。
1)在此处更改零件

assign["to"] = "";
assign["subject"] = "";
assign["contact"] = ;
assign["regardingobjectid"] = ;

您需要实际分配值而不是空值/空字符串。

2)任何实体通常都有某种类型的主要字段(通常是名称/主题/标题);应该设置(我假设这是主题字段,当前=&#34;&#34;)或它会回来咬你。

3)您可能应该使用Entity.Attributes.Contains(&#34; id&#34;)而不是&#34; OutputParameters&#34; for context.OutputParameters.Contains(&#34; id&#34;)因为我怀疑你注册了插件有输出参数(这需要你从一个单独的插件生成它们)

4)我不知道如何通过C#访问MySQL,但我认为你需要从那个&#34;读者&#34;中获取MySQL的数据,并使用它来填充CRM值。

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

6)您的评论一直是指帐户,但是开头的检查是针对&#34;客户&#34; (再次,可选,因为它只是糟糕的评论)

7)分配&#34;实体&#34;不是有效的实体名称,不应该有任何空格 - 实体名称更像是

var assign = new Entity("new_smsmessage")