Please read the last blog before starting this one. Here are the steps.
- Open the CRM Explorer. Right Click on the Entities node and select “Generate Wrapper”. It will generate the strongly type classes and add it to the Plugins project. The name of the generated file will be Entities.cs.
- Now expand the Entities node. Right Click on Account Entity and select “Create Plug-in”. It will display a following dialog. This time we are creating a plugin on update event. Enter the values as shown the screen shot. You can change the class name if you like and press OK. It will create a new file “PostAccountUpdate.cs” in the plugins project.
- Open the “PostAccountUpdate.cs” file. Add the reference to Entites.cs file to use the strongly typed classes.
- Copy the ExecutePostAccountCreate() function code from PostAccountCreate.cs created in last blog and paste it in ExecutePostAccountUpdate() function of “PostAccountUpdate.cs”.
- Replace the following lines in copied code of ExecutePostAccountUpdate() function
//create a task Entity task = new Entity("task"); task["subject"] = "Account number is missing"; task["regardingobjectid"] = new EntityReference("account",entity.Id); //adding attribute using the add function // task["description"] = "Account number is missng for the following account. Please enter the account number"; task.Attributes.Add("description", "Account number is missng for the following account. Please enter the account number"); // Create the task in Microsoft Dynamics CRM. service.Create(task);
with//create a task Task task = new Task(); task.Subject = "Account number is missing"; task.RegardingObjectId = new EntityReference("account", new Guid(context.OutputParameters["id"].ToString())); task.Description = "Account number is missng for the following account. Please enter the account number"; // Create the task in Microsoft Dynamics CRM. service.Create(task);
We replaced the Entity object with Task object and we used task.Description in place of task["description"]. Early binding (strongly typed classes provides us all the crm entities objects, their properties and methods. - Now right click on CRM Package project and select deploy. It will register the plugin assembly as well as step for the plugin. Click on CRM Explorer to check the deployed plugin as shown in the following screen shot. There are two plugins registered, one for post update event that we just created and one for post create event we created in last blog. Update an account to test the plugin.
Advantages for using early binding
Here is the link to explain the differences between early binding and late binding. I am listing few of advantages using early binding.- The biggest advantage of using early binding is that we have access to all the CRM entities(account, contact, opportunity, task etc) their attributes and their relationships.In late binding we use the Entity (dynamic entity) to work with all the CRM entities.
- Access to visual studio intellisense while writing code.In the following screen shot, we create an instance of Task entity and when we type Task, it will display all the attributes and methods belong to task entity.
- There are less chances of misspelling the attributes names. Early bound references are checked at compile time. for e.g if we spell the attribute name wrong, you won’t able to compile the code. In late binding, it will hard to find out if we misspell the attribute name as references will be checked at run time.
Great...
ReplyDeleteThanks Mate.
ReplyDeleteHi Amreek,
ReplyDeleteI have to access Nav web service in crm to pass data from crm 2011 to nav wiht crm values like name ,city ,email etc.
Please help for that.
Hi Singh,
ReplyDeleteplease can you tell me how can i add the Entities reference in my code?
Thanks
Hi,
ReplyDeletei have a an exception by trying this code saying the givin key"id" was not present in the dictionnary, the create post event plugin is working fine
Please what is causing this error? why is the plugin no longer able to retrieve the id?
Thanks for your reply
the problem is this line
Deletetask.RegardingObjectId = new EntityReference("account", new Guid(context.OutputParameters["id"].ToString()));
this will work for post create but not for post update. change that line with
task.RegardingObjectId =new EntityReference("account",entity.Id);
I hope this helps.
Hey Singh,
ReplyDeletePlease can you tell me how can i add the Entities reference in my code?
Does my plugin class has to inherit the Entities class?
just add using Entities; at the top of your plugin file
ReplyDeleteHi,
ReplyDeleteCan you please help with my requirement.
Requirement: in account entity when an oppurtunity status is open.user shouldnt deactivate the account by using plugin.(please share the code)
please help with this.