CRM4 and LINQ example


I have been working with CRM import lately. This import is implemented using CRM proxy classes for the account entity that enables use of LINQ statements when I’m going to perform lookups, insert and updates against the CRM4 object model. Here is a link for several LINQ examples.

var crm = new CRMDataContext(CrmConnection.Parse(ConnectionStringCrmService));
var acc = (from c in crm.accounts
           where c.accountnumber == accountNumber
           select c).FirstOrDefault();

The from…where…select statement gives my the CRM account object that has equal the account number accountNumber given as parameter to my method. The ConnectionStringCrmService property retrieves the connection string to the CRM4 web service that is used to construct the CRM data context object.

When you have found the CRM account, this can be modiefied by setting the properties for the appropriate values and calling the UpdateObject method, as shown below.

acc.name = "xxx";
crm.UpdateObject(acc);
crm.SaveChanges();

To add a new account to CRM4, you will create a new account object, assign the propper values through the account properties, call the AddToaccount method and finally call the SaveChanges method on the CRM4 data context object.

var tmp = new account();
tmp.accountnumber = "1234";
tmp.name = "MyComapny";
crm.AddToaccounts(tmp);
crm.SaveChanges();

Happy CRM and LINQ coding! 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s