Creating Records into Microsoft Dynamics CRM 2013 programmatically unveiled
Hi Folks,
This is an extension of the post which I have written to connect to Microsoft Dynamics CRM 2013 programmatically from the following link: https://blogs.technet.com/b/tvishnun1/archive/2014/07/09/kick-start-with-connecting-to-microsoft-dynamics-crm-2013-programmatically.aspx.
In this example we will see how we can create a simple contact record which includes the following fields:
Schema Name |
Display Name |
Field Type |
firstname |
First Name |
Single Line of Text |
lastname |
Last Name |
Single Line of Text |
gendercode |
Gender Code |
Option Set |
birthdate |
Birth Date |
Date & Time |
annualincome |
Annual Income |
Currency |
preferredsystemuserid |
Preferred User |
Lookup |
exchangerate |
Exchange Rate |
Decimal Number |
In the above table all types of fields are given so that you can replicate that according to your business needs for your field.
Since we have already connected to CRM in our previous post and now have to create records into CRM with the above fields. This example shows some few fields with almost all types now if you have to create other fields of same type you can use similar approach.
Let us go back to the project we have created earlier and add a new button for creating contact in CRM.
Now let us get into some simple coding stuff.
On the button click event we are going to place the following code:
//Here we are getting the details of the connection string and placing in the context variable.
var context = new
CrmOrganizationServiceContext(CrmConnection.Parse(connectionDialog.ConnectionString));
//We are instantiating object of Entity class. This is available in Microsot.Xrm.Sdk assembly.
Entity contact = new
Entity("contact");
//Since First Name and last name are single line of texts we are passing string variable.
contact["firstname"] = "Vishnu";
contact["lastname"] = "Turlapati";
//Here we are giving the value of Male value avaialable in Microsoft CRM. So based on the option set that needs to be selected place the respective optionset value.
contact["gendercode"] = new
OptionSetValue(1);
//Here we are placing the date in date time format.
contact["birthdate"] = new
DateTime(1988, 01, 02);
//Annual income is a currency so we are giving it directly as money variable.
contact["annualincome"] = new
Money((decimal)10000);
//Here we are setting the preferred System user to a specific user record. GUID of a record can be obtained from an individual record.
contact["preferredsystemuserid"] = new
EntityReference("systemuser", new
Guid("CF955D17-310B-E411-AB97-D89D6765A2D0"));
//This is another decimal value which we are placing.
contact["exchangerate"] = new
decimal(60);
//Finally we are creating the contact record into CRM.
context.Create(contact);
Congratulations you have successfully created a record in CRM with different fields. In the next post we will see how this can be better streamlined for better operations.
Comments
- Anonymous
January 01, 2003
thanks - Anonymous
February 25, 2015
contact["preferredsystemuserid"] = new EntityReference("systemuser", new Guid("CF955D17-310B-E411-AB97-D89D6765A2D0"));
In this any other way? i want to insert it without Gud.
i have related fields data. so every time i call to get Guig.
Thanks