D365: IOrganizationService.Update(Entity) Method, Part II

In today’s blog we’ll talk about IOrganizationService.Update() method, this is the second part of the blog for this subject, in the first part we explained: `What is IOrganizationService.Update() method` and `How to create a custom plugin where we use the IOrganizationService.Update() method`, if you want to refresh your memory here is the link for the first part of the blog: IOrganizationService.Update() Method, Part 1.

Differently from the first part of the blog, today we will discuss a common mistake made by beginners when using this method to update fields in CRM.

Let’s look at the picture below and explain the code, how to do things and what to avoid:

First off, lets explain every part of the code from line 33-49 :

-Line 33 : In this line we declare a variable named entity of type Entity and by using the ‘context’ variable we retrieve all the information from the ‘target’ entity which is the entity we specify when we create the step for our plugin in Plugin Registration Tool, in our example above the target entity is a custom entity I have created named “fisoft_customer”.

-Line 35 : In this line we declare a string variable named CustomerName which we assign it equal to the value of the Name field (fisoft_name), the value of which we retrieve from the target entity.

-Line 36 : In this line we declare a string variable named name which we assign the string `User`.

-Line 38 : In this line we have an IF condition where we check if the value of the variable `CustomerName` is equal to the value of the variable `name` , if yes then the code inside the if statement gets executed else it does nothing and the code stops running.

-Line 40 : Here we declare a string variable named newName assigned to the string ‘Hello user’.

Until this point everything is correct (my way of doing it, you can go a different route), but now after line 40 we can see in the picture two blocks of code each with a comment as Method 1 and Method 2, now one of these methods is the correct way of using the IOrganizationService.Update() method and the other one is the incorrect way. So let’s start with Method 1

Method 1 :

-Line 43 : In this line we grab the fisoft_name field from the target entity and we assign this field with our newName variable.

-Line 44 : In this line we use the IOrganization.Update() method to update the entity in between the parentheses , in this case we pass the entity variable which is the target entity.

This method is the problematic way of using the Update() method. Let’s say this plugin will be set on update of the name field (fisoft_name) , then when we use this method we directly modify the target entity in our case the variable entity (fisoft_customer) , so the entity gets updated and this code gets triggered , then our name field gets updated as shown in the code but the update of this field causes the re-update of the target entity and so this goes on and on creating an infinite loop. Even if we don’t have the plugin in the update , let’s say we have it on create of a new record , still using this method is an issue because when we update the field we want , all the other fields also get updated and this causes other plugins or scripts to trigger , which we don’t want.

Method 2 :

-Line 47 : In this line we declare a variable named `UpdateCustomer` of type entity and we assign it with a new copy of our target entity , where we also pass the logical name and the id of the target entity.

-Line 48 : In this line we use earlier entity variable we declared and we grab the field we want (fisoft_name) and we assign this field with the value of our `newName` variable.

-Line 49 : And lastly on this line we update the copy entity (UpdateCustomer) of our target entity (fisoft_customer).

Now let’s go to the environment and see the plugin in work :

We go to the Customer entity in our code this entity is our target entity , we create a new record but if you have existing ones then we just edit the Name field to trigger the run of our plugin as seen below :

After saving the changes to our record we can see the Name field gets modified with our new value form the code :

And as we can see the plugin works (using the Method 2) , the value of the Name field gets updated with the static value we had in the code.