locked
Error in Plugin RRS feed

  • Question

  • hello all,

    i have created one plugin that execute when new lead is saved.

    Here is a code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;
    using System.Data.SqlClient;
    
    
    namespace LeadOpportunityConversionRatio
    {
        public class Plugin : IPlugin
        {
            int count;
            int count1;
            public void Execute(IPluginExecutionContext context)
            {
                string constr = "Data Source=SVT-SRV-02;Initial Catalog=SynovergeTest_MSCRM;Integrated Security=True";
    
                using (SqlConnection con = new SqlConnection(constr))
                {
                    SqlCommand cmd = new SqlCommand("SELECT count(*) FROM dbo.LeadBase WHERE DeletionStateCode = '0'");
                    SqlCommand cmd1 = new SqlCommand("SELECT count(*) FROM dbo.OpportunityBase WHERE OriginatingLeadId is not null");
                    cmd.Connection = con;
                    cmd1.Connection = con;
    
                    try
                    {
                        con.Open();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
    
    
    
                    try
                    {
                        count = (int)cmd.ExecuteScalar();
                        count1 = (int)cmd1.ExecuteScalar();
                    }
    
                    catch (Exception ex)
                    {
                        throw ex;
                    }
    
    
                    con.Close();
    
                }           
          
                if (context.InputParameters.Properties.Contains("Target") &&
                    context.InputParameters.Properties["Target"] is DynamicEntity)
                {
                    DynamicEntity entity = (DynamicEntity)context.InputParameters.Properties["Target"];
    
                    if (entity.Name == EntityName.lead.ToString())
                    {
                        if (entity.Properties.Contains("new_totallead") == false)
                        {
    
    
                            StringProperty totallead = new StringProperty("new_totallead", count.ToString());
                            entity.Properties.Add(totallead);
                        }
                        else
                        {
                            // Throw an error, because Account Numbers must be system generated
                            throw new InvalidPluginExecutionException("The total lead can only be set by the system");
                        }
                        if (entity.Properties.Contains("new_convertedlead") == false)
                        {
    
    
                            StringProperty convertedlead = new StringProperty("new_convertedlead", count1.ToString());
                            entity.Properties.Add(convertedlead);
                        }
                        else
                        {
                            // Throw an error, because Account Numbers must be system generated
                            throw new InvalidPluginExecutionException("The converted lead can only be set by the system");
                        }	
                    }
                }
            }
           
           
        
        
        }
    }
    

    I have used 4.0 framework.

    but when i save any newly created lead i got this error.

    And my assembly step is like this

    So where is my mistake???

    Please give me suggestion.


    Kartik Associative Software engineer, Synoverge Tech Pvt. Ltd. Ahmedabad

    Friday, June 1, 2012 8:43 AM

Answers

All replies

  • Hello,

    It seems that you deveploed plugin for CRM 4.0. This mean that you should use framework 3.5, 3.0, 2.0. With Framework 4.0 it would not work.

    And small reminder in case you have already got answer - close threads please. http://social.microsoft.com/Forums/en-US/crm/thread/9043fb1f-6eaf-44ac-8c46-e38911b29551/#edc1dbc4-0773-4c1f-bcf2-f79b990b1575


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter

    Friday, June 1, 2012 8:50 AM
    Moderator
  • Hi Andrii,

    Thanks for reply and reminder about other thread.

    i have changed framework to 3.5 but i still get below common error.

    I think Problem is in coding.


    Kartik Associative Software engineer, Synoverge Tech Pvt. Ltd. Ahmedabad


    Friday, June 1, 2012 9:00 AM
  • hello all,

    i have created one plugin that execute when new lead is saved.

    Here is a code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;
    using System.Data.SqlClient;
    
    
    namespace LeadOpportunityConversionRatio
    {
        public class Plugin : IPlugin
        {
            int count;
            int count1;
            public void Execute(IPluginExecutionContext context)
            {
                string constr = "Data Source=SVT-SRV-02;Initial Catalog=SynovergeTest_MSCRM;Integrated Security=True";
    
                using (SqlConnection con = new SqlConnection(constr))
                {
                    SqlCommand cmd = new SqlCommand("SELECT count(*) FROM dbo.LeadBase WHERE DeletionStateCode = '0'");
                    SqlCommand cmd1 = new SqlCommand("SELECT count(*) FROM dbo.OpportunityBase WHERE OriginatingLeadId is not null");
                    cmd.Connection = con;
                    cmd1.Connection = con;
    
                    try
                    {
                        con.Open();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
    
    
    
                    try
                    {
                        count = (int)cmd.ExecuteScalar();
                        count1 = (int)cmd1.ExecuteScalar();
                    }
    
                    catch (Exception ex)
                    {
                        throw ex;
                    }
    
    
                    con.Close();
    
                }           
          
                if (context.InputParameters.Properties.Contains("Target") &&
                    context.InputParameters.Properties["Target"] is DynamicEntity)
                {
                    DynamicEntity entity = (DynamicEntity)context.InputParameters.Properties["Target"];
    
                    if (entity.Name == EntityName.lead.ToString())
                    {
                        if (entity.Properties.Contains("new_totallead") == false)
                        {
    
    
                            StringProperty totallead = new StringProperty("new_totallead", count.ToString());
                            entity.Properties.Add(totallead);
                        }
                        else
                        {
                            // Throw an error, because Account Numbers must be system generated
                            throw new InvalidPluginExecutionException("The total lead can only be set by the system");
                        }
                        if (entity.Properties.Contains("new_convertedlead") == false)
                        {
    
    
                            StringProperty convertedlead = new StringProperty("new_convertedlead", count1.ToString());
                            entity.Properties.Add(convertedlead);
                        }
                        else
                        {
                            // Throw an error, because Account Numbers must be system generated
                            throw new InvalidPluginExecutionException("The converted lead can only be set by the system");
                        }	
                    }
                }
            }
           
           
        
        
        }
    }

    I have used 3.5 framework.

    but when i save any newly created lead i got this error.

    And my assembly step is like this

    So where is my mistake???

    Please give me suggestion.



    Kartik Associative Software engineer, Synoverge Tech Pvt. Ltd. Ahmedabad

    Friday, June 1, 2012 2:13 PM
  • 1. Please don't create duplicated threads.

    2. Use debugger to debug your plugin. This will give you answer faster.


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter

    Friday, June 1, 2012 3:23 PM
    Moderator
  • hi andrii

    sorry for creating duplicate thread

    actually a don't know right forum to put my question.

    I'll try using debugger and let u know if I'll get any problem 


    Kartik Associative Software engineer, Synoverge Tech Pvt. Ltd. Ahmedabad

    Monday, June 4, 2012 4:58 AM
  • hi andrii,

    thanks for giving me idea of debugger.it worked now.

    problem is with string property.the crm field are of integer type and i have use stringproperty in plugin.


    Kartik Associative Software engineer, Synoverge Tech Pvt. Ltd. Ahmedabad

    Monday, June 4, 2012 5:03 AM