joi, 15 aprilie 2010

CRM, OnSave, Activate/Deactivate and changing form fields

A little thing i learned today while cursing and swearing the hell out of CRM is how to change the value of a field when activating/deactivating an entity in CRM 4.0 (The cursing and swearing is, mostly, due to my being a true CRM newb, but CRM has it's faults too. Oh yeah...:)
The scenario is this: When deactivating or reactivating and entity in CRM 4.0, change the value of a field in your form and save it to the db.
Long story short to achieve this goal you must do the following:
1. Customize the OnSave event of the form
2. Check the event.Mode value (remember that 5 is deactivate, while 6 is reactivate)
3. Change the DataValue property of your desired field to whatever you want
5. Throw in some ForceSubmit = true for good measure

So, your code for the OnSave event might look like this
Code:
         if(event.Mode == 5/*Deactivate*/)
           {
                crmForm.all.yourField.DataValue = xxxxxx;
                crmForm.all.yourFiled.ForceSubmit = true;
          }
        if(event.Mode == 6/*Reactivate*/)
           {
                crmForm.all.yourField.DataValue = yyyy;
                crmForm.all.yourFiled.ForceSubmit = true;
          }


Enjoy:)

LINQ, InsertOnSubmit And SubmitChanges

I have been trying to learn LINQ (actually, trying is really an overstatement) and ASP.NET MVC and i came across a little problem that baffled me.
How do you insert a record in a database using LINQ?
The answer is very, very simple and one scenario to go is using InsertOnSubmit and then SubmitChanges.

Quick example:
First, create a database and then a table called Movie with the following structure:
 Ok, now create some LINQ to SQL classes based on the previously created table.
So you should have by now a .dbml that looks like this (actual class definitions in the dbml.cs file in your project):
The next thing is to insert the following code in your desired method:

Code:
          MovieDbDataContext dbContext = new MovieDbDataContext();
         dbContext.Movies.InsertOnSubmit(movie);
         dbContext.SubmitChanges();
where MovieDbDataContext is the name of the DataContext class generated by LINQ and movie holds the data you want to save in the database.
Easy, right? Just don't forget to fire up that SubmitOnChanges method after InsertOnSubmit
Cheers:)