locked
How to convert crm 4.0 plugin c# code to crm 2013 RRS feed

  • Question

  •      
            public static DynamicEntity[] getSemProdukteFromDB(SqlConnection con, Guid termin, int menge, Guid[] teilnehmer, string tnname) {//string name) {
                SqlCommand Kommando = new SqlCommand();
                string Kommandotext = "";
                string sqlstring="sem_TNid like '" + teilnehmer[0] + "'";
                for(int i =1; i<teilnehmer.Length;i++){
                    sqlstring=sqlstring+" OR sem_TNid like '" + teilnehmer[i] + "'";
                }
                Kommandotext = "select * from sem_seminarterminprodukt where sem_kostenunderoelseId= '" + termin + "' AND DeletionStateCode!=2 AND "+sqlstring;
    
    
                DynamicEntity[] dea = new DynamicEntity[menge];
                if (menge > 0) {
                    Kommando.Connection = con;
                    Kommando.CommandText = Kommandotext;
                    con.Open();
                    SqlDataReader reader = Kommando.ExecuteReader();
                    int zähl = 0;
                    while (reader.Read()) {
                        Log("Reader readed: " + reader["Sem_name"]);
                        DynamicEntity tmp = new DynamicEntity();
                        tmp.Properties["tqu_titel"] = reader["Sem_name"];
                        Log("produktidname: " + reader["Sem_ProduktIdName"]);
                        tmp.Properties["Sem_ProduktIdName"] = reader["Sem_ProduktIdName"];
                        tmp.Properties["Sem_name"] = reader["Sem_name"];
                        tmp.Properties["Sem_ProduktId"] = reader["Sem_ProduktId"];
                        //   double sem_menge = (double)reader["MENGE"];
                        //   tmp.Properties["sem_menge"] = new CrmDecimal((decimal)sem_menge);
                        /*   try {
                               Log("@ getSemProdukteFromDB: check name auf teilnehmer");
                               if (((String)tmp.Properties["Sem_ProduktIdName"]).Equals("Teilnehmer")) {
                                   tmp.Properties["tqu_titel"] = "Teilnehmer: " + tnname;
                                   Log("Rechnung(Produkt) Titel: " + tmp.Properties["tqu_titel"]);
                               }
                           } catch (Exception ex) {
                               Log("ProduktIdName zickt! " + ex.Data + " " +ex.Message);
                           }*/
                        tmp.Properties["sem_ispriceoverridden"] = reader["sem_ispriceoverridden"];
                        Log("Preis überschrieben? " + tmp.Properties["sem_ispriceoverridden"]);
                        tmp.Properties["Sem_Menge"] = reader["Sem_Menge"];
                        tmp.Properties["Sem_einzelpreis"] = reader["Sem_einzelpreis"];
                        tmp.Properties["Sem_KostenOderErloes"] = reader["Sem_KostenOderErloes"];
                        tmp.Properties["Sem_seminarterminproduktid"] = reader["Sem_seminarterminproduktid"];
                        tmp.Properties["transactioncurrencyid"] = reader["transactioncurrencyid"];
                        tmp.Properties["tqu_mwst"] = reader["sem_mwst"];
                        tmp.Properties["sem_steuer"] = reader["sem_steuer"];
                        tmp.Properties["sem_manualdiscountamount"] = reader["sem_manualdiscountamount"];
                        //   tmp.Properties["tqu_manualdiscountpercent"] =new CrmNumber(0);
                        // MWST!
                        //        Log("Name:" + dea[zähl].Properties["Sem_name"]);
    
                        dea[zähl] = tmp;
                        zähl++;
                    }
                    con.Close();
                } else {
                    dea = null;
                }
    
                return dea;
            }
    


    • Edited by necsa Wednesday, June 10, 2015 3:41 PM
    Wednesday, June 10, 2015 3:39 PM

Answers

  • The dynamic entity of old is now just called Entity and EntityCollection.

    Here is an example of how it might work, you will most likely need to fix something. This assumes you are using late bound entities.

    Late bound (means they are dynamic)
    entity.Attributes.Add("tqu_titel", reader["Sem_name"]);

    Early bound (means they are strongly typed)
    entity.tqu_titel = reader["Sem_name"]);

    Here is an example of late bound, you will most likely have to fix some of the attribute names, if they are wrong you will get a generic error.

    public static EntityCollection getSemProdukteFromDB(SqlConnection con, Guid termin, int menge, Guid[] teilnehmer, string tnname) 
    {
    	SqlCommand Kommando = new SqlCommand();
    	string Kommandotext = "";
    	string sqlstring="sem_TNid like '" + teilnehmer[0] + "'";
    	for(int i =1; i<teilnehmer.Length;i++)
    	{
    		sqlstring=sqlstring+" OR sem_TNid like '" + teilnehmer[i] + "'";
    	}
    	Kommandotext = "select * from sem_seminarterminprodukt where sem_kostenunderoelseId= '" + termin + "' AND DeletionStateCode!=2 AND "+sqlstring;
    
    
    	EntityCollection entityCollection = new EntityCollection();
    	
    	if (menge > 0) 
    	{
    		Kommando.Connection = con;
    		Kommando.CommandText = Kommandotext;
    		con.Open();
    		SqlDataReader reader = Kommando.ExecuteReader();
    		
    		while (reader.Read()) 
    		{
    			Log("Reader readed: " + reader["Sem_name"]);
    			Entity entity = new Entity("NAMEOFENTITY"); //Here you need to insert the name of the entity you are working with.
    			
    			//Entity attribute names will have to be correct, they are case sensitive and can be found on the entity form.
    			entity.Attributes.Add("tqu_titel", reader["Sem_name"]);
    			Log("produktidname: " + reader["Sem_ProduktIdName"]);
    			entity.Attributes.Add("Sem_ProduktIdName", reader["Sem_ProduktIdName"]);
    			entity.Attributes.Add("Sem_name", reader["Sem_name"]);
    			entity.Attributes.Add("Sem_ProduktId", reader["Sem_ProduktId"]);
    			entity.Attributes.Add("sem_ispriceoverridden", reader["sem_ispriceoverridden"]);
    			Log("Preis überschrieben? " + entity.Attributes["sem_ispriceoverridden"]);
    			entity.Attributes.Add("Sem_Menge", reader["Sem_Menge"]);
    			entity.Attributes.Add("Sem_einzelpreis", reader["Sem_einzelpreis"]);
    			entity.Attributes.Add("Sem_KostenOderErloes", reader["Sem_KostenOderErloes"]);
    			entity.Attributes.Add("Sem_seminarterminproduktid", reader["Sem_seminarterminproduktid"]);
    			entity.Attributes.Add("transactioncurrencyid", reader["transactioncurrencyid"]);
    			entity.Attributes.Add("tqu_mwst", reader["sem_mwst"]);
    			entity.Attributes.Add("sem_steuer", reader["sem_steuer"]);
    			entity.Attributes.Add("sem_manualdiscountamount", reader["sem_manualdiscountamount"]);
    
    
    			entityCollection.Entites.Add(entity);
    		}
    		con.Close();
    	} else 
    	{
    		return null;
    	}
    
    	return entityCollection;
    }


    Halldór Jóhannsson

    • Marked as answer by necsa Thursday, June 11, 2015 1:17 PM
    Thursday, June 11, 2015 9:43 AM

All replies

  • I would download the CRM 2013 SDK and check out the samples under "\SDK\SampleCode\CS\Plug-ins\" there should be information there to help you update your plugin.

    Halldór Jóhannsson

    Wednesday, June 10, 2015 4:21 PM
  • Thanks for advice. I have already check it but I did not see any information. That why i please helping in forum. My problem is not to convert all code just how to convert data type of Dynamic[] as EntityCollection or using other way. So, thanks again and wellcome all other advice, solution..
    Thursday, June 11, 2015 5:54 AM
  • The dynamic entity of old is now just called Entity and EntityCollection.

    Here is an example of how it might work, you will most likely need to fix something. This assumes you are using late bound entities.

    Late bound (means they are dynamic)
    entity.Attributes.Add("tqu_titel", reader["Sem_name"]);

    Early bound (means they are strongly typed)
    entity.tqu_titel = reader["Sem_name"]);

    Here is an example of late bound, you will most likely have to fix some of the attribute names, if they are wrong you will get a generic error.

    public static EntityCollection getSemProdukteFromDB(SqlConnection con, Guid termin, int menge, Guid[] teilnehmer, string tnname) 
    {
    	SqlCommand Kommando = new SqlCommand();
    	string Kommandotext = "";
    	string sqlstring="sem_TNid like '" + teilnehmer[0] + "'";
    	for(int i =1; i<teilnehmer.Length;i++)
    	{
    		sqlstring=sqlstring+" OR sem_TNid like '" + teilnehmer[i] + "'";
    	}
    	Kommandotext = "select * from sem_seminarterminprodukt where sem_kostenunderoelseId= '" + termin + "' AND DeletionStateCode!=2 AND "+sqlstring;
    
    
    	EntityCollection entityCollection = new EntityCollection();
    	
    	if (menge > 0) 
    	{
    		Kommando.Connection = con;
    		Kommando.CommandText = Kommandotext;
    		con.Open();
    		SqlDataReader reader = Kommando.ExecuteReader();
    		
    		while (reader.Read()) 
    		{
    			Log("Reader readed: " + reader["Sem_name"]);
    			Entity entity = new Entity("NAMEOFENTITY"); //Here you need to insert the name of the entity you are working with.
    			
    			//Entity attribute names will have to be correct, they are case sensitive and can be found on the entity form.
    			entity.Attributes.Add("tqu_titel", reader["Sem_name"]);
    			Log("produktidname: " + reader["Sem_ProduktIdName"]);
    			entity.Attributes.Add("Sem_ProduktIdName", reader["Sem_ProduktIdName"]);
    			entity.Attributes.Add("Sem_name", reader["Sem_name"]);
    			entity.Attributes.Add("Sem_ProduktId", reader["Sem_ProduktId"]);
    			entity.Attributes.Add("sem_ispriceoverridden", reader["sem_ispriceoverridden"]);
    			Log("Preis überschrieben? " + entity.Attributes["sem_ispriceoverridden"]);
    			entity.Attributes.Add("Sem_Menge", reader["Sem_Menge"]);
    			entity.Attributes.Add("Sem_einzelpreis", reader["Sem_einzelpreis"]);
    			entity.Attributes.Add("Sem_KostenOderErloes", reader["Sem_KostenOderErloes"]);
    			entity.Attributes.Add("Sem_seminarterminproduktid", reader["Sem_seminarterminproduktid"]);
    			entity.Attributes.Add("transactioncurrencyid", reader["transactioncurrencyid"]);
    			entity.Attributes.Add("tqu_mwst", reader["sem_mwst"]);
    			entity.Attributes.Add("sem_steuer", reader["sem_steuer"]);
    			entity.Attributes.Add("sem_manualdiscountamount", reader["sem_manualdiscountamount"]);
    
    
    			entityCollection.Entites.Add(entity);
    		}
    		con.Close();
    	} else 
    	{
    		return null;
    	}
    
    	return entityCollection;
    }


    Halldór Jóhannsson

    • Marked as answer by necsa Thursday, June 11, 2015 1:17 PM
    Thursday, June 11, 2015 9:43 AM
  • Thanks a lot Johansssson,

    I customize to my plug in and wooow!!! You are super hero. Thanks again you save me a lot of time.

    Thursday, June 11, 2015 1:19 PM
  • That is good to hear, I am glad this worked out 8)

    Halldór Jóhannsson

    Thursday, June 11, 2015 1:26 PM