Wednesday, February 1, 2012

Insert New Record In Micosoft CRM 2011 Using oData Call

              Need to insert new record from the JavaScript by using the oData call in Microsoft CRM 2011.
              The solution of the above problem is very simple and you can find the solution in the Microsoft CRM 2011 SDK as well. In Microsoft CRM 2011 SDK you can find separate file which contain all the function like Create, Update  and delete record using oData which you can reuse. But in this post I will use oData call without using the help JavaScript file. The JavaScript code is listed in the List 1. In the code you can see that I have created addAccountRecord object and then assigned appropriate value to some of the property of the object.

Note: You can use the Schema Name of the attribute as you can see in the Image 1, here you can see that PrimaryContactId is used to set the Primary Contact of the account.

Image 1

Similarly I have other attribute of the account entity like Name, Description, Revenue , DoNotPhone etc, you can use the name from the schema name from the attribute list of the entity. In the JavaScript code listed in List 1 you can see that Name and the Description attribute are of type Text mean you can assign simple string value to these two attribute.
The PrimaryContactId which is lookup type of contact entity , you have to provide the object which has the Id of the contact , LogicalName of the entity which in this case will be the contact entity and the name of the contact which "Susan Burk (Sample)" in this case. So in order to set the value for a lookup field of the account you have to provide Id , logical name and name.Here I have get the Id of the contact by using the developer toolbar to inspect the HTML of the active account grid so that I can assign value to the lookup field you may use different technique to get the Id and name of the contact.
Next is how to set the value to the picklist and the currency. If you have created plugin then you have idea how to set the value to the picklist and the currency attribute. You need to set the Value of the picklist and currency type and here in the code you can see that I have used that value to set the value to of picklist and the currency type. Last is how to set the value of the boolean type it is just simple either set true of false the only value for the boolean type.
List 1

Next point which I would like to mention here is the how the XMLHttpRequest object is open as I am sending data to the server mean to submit in the sever to I will use "POST", here you can notice that if we need to get data from the server we use "GET" but as you know we are inserting record in the database so we use "POST". Next is the URL which is appended with the "AccountSet" which is the name of the entity "Account". Last parameter for the open function is the boolean value to indicate whether we are making async or sync call by passing true we indicate that we are making async call if you pass false then call weill be sync.
In XMLHttpRequest object send function we pass the object by calling JSON.stringify function which is use to converting JavaScript data structures into JSON text. In the "onAccountCreateCallBack" event handler we check for the readyState of the passed object and status of the object. Here you can see that in this case it will return 201 if record is inserted successfully in the database.

It will return whole record of the account which is just added in the database. I have only used the Name and the Creation date to be shown that record is added in the database. As you can see in the above image Image 2. You can see the newly added record in the active account view which is shown in the Image 3 as below.
You just need one extra resource JSON2 which you can get from here. Just add the JSON2 as web resource and add that web resource in the form reference so that it can be accessible when you refere it. Just click the "From properties" by opening the form from customization and you will see the dialog as shown in the Image 4 and here you have to add the JSON2 web resource to access in the form.
Image 4

Hope you get some idea of how to add the record using JavaScript and odata call. I have call above JavaScript on load of form so that when form is loaded new record will be created and alert will be shown which display name of the account and created time of the record.
All and any comments / bugs / suggestions are welcomed!

1 comment:

mary ghazaryan said...

Hi, I've used your post and it helped me to create a new form.

In the code instead of


addAccountRecord.Revenue = { Value: "2000000.00" }; //Set Annual Revenue


I've written down

var a1=25 000;

addAccountRecord.Revenue = { Value:a1 };

And with this version I get an error. Please help me to solve this problem.