Microsoft Dynamics CRM 2015 Update 1 introduced new capabilities to the CRM SDK. The following are the new enhancements.
CRM 2015 enhanced capability of update method not we can use the Update
method to apply special operations for which we had separate requests earlier. For example, earlier we had to use a special request to perform assignment, status changes, and other special operations, but now we can set these properties in the update method only. The following is an example of using the Update
method for assignment:
using (OrganizationService crmService = new OrganizationService("OrganizationService")) { Entity accountToUpdate = new Entity("account"); accountToUpdate["ownerid"] = new EntityReference("systemuser", new Guid("38FC3E74-B30B-E511-80FC-C4346BAD26CC ")); crmService.Update(accountToUpdate); }
In the preceding code we are changing the owner of the record, which means we are assigning this record to another CRM user.
New messages have been introduced to improve performance, where we can execute multiple options under a single transactions. This is very important when we are doing integration with another system (such as a financial management system) and performing multiple dependent changes, where we want to commit all of them or none. CRM 2015 Update 1 introduced ExecuteTransactionRequest
, which helps us to combine multiple entity operations under one transaction; they will be executed based on the order in the collection. If any operation fails, it will rollback all the changes. ExecuteTransactionFault
helps us to identify the operation that caused the fault. The following is an example of creating multiple account records:
using (OrganizationService crmService = new OrganizationService("OrganizationService")) { ExecuteTransactionRequest request = new ExecuteTransactionRequest() { Requests = new OrganizationRequestCollection() }; for (int i = 1; i <= 10; i++) { Entity account = new Entity("account"); account["name"] = string.Format("Account Transaction Demo {0}", i); CreateRequest createRequest = new CreateRequest() { Target = account }; request.Requests.Add(createRequest); } ExecuteTransactionResponse response = (ExecuteTransactionResponse)crmService.Execute(request); }
In the preceding code we have created 10 account demo records in a single transaction; once this code is executed we can see 10 records created in the CRM:
Now we have the flexibility to define our own custom keys using the Keys option for our entities. This new feature helps us to uniquely identify our entity records using these key fields, which means that we now don't need to depend on the primary key attribute for record updates. On the basis of these custom keys, we update our entity record.
We can define alternate keys by following these steps:
Upsert
is another new enhancement that helps us to integrate CRM with other applications easily, especially if we are doing data synchronization between CRM and other applications. Sometimes we are not sure if a particular record exists in the CRM or not. The Upsert
request helps us to create a record in the CRM database if it does not exist after validating the custom key; if the record exists, it will update with the latest values applied. The following is an example of executing Upsert
over our member entity:
using (OrganizationService crmService = new OrganizationService("OrganizationService")) { Entity account = new Entity("account") { KeyAttributes = new KeyAttributeCollection { {"accountnumber", "1234" } } }; account["name"] = "Upsert Example"; UpsertRequest request = new UpsertRequest() { Target = account }; UpsertResponse response = (UpsertResponse)crmService.Execute(request); }
This code will first check if there are any member records with the preceding member id value. If not, it will create a new member record with this value; otherwise, it will update the existing records.
The optimistic concurrency feature helps us to avoid any type of data inconsistency when many concurrent users are working. For example let's say that, once we have retrieved the entity record, we need to work on another calculation; then we need to update the same record. But if during that this time another user has updated that entity record it can lead to data inconsistency. To avoid this situation, now we can check the record's RowVersion
and can apply the logic to update it is the same as before while updating the entity record.
You can refer to https://msdn.microsoft.com/en-us/library/dn707955.aspx for more detail on optimistic concurrency.
If you are a developer, then most of the time you will be using tracing to troubleshoot development issues and for debugging your code. When exceptions are thrown by CRM or through the code, we can get tracing information by downloading the log file (it may be available under system jobs). We will discuss how to use tracing in our code in a later chapter.
CRM 2015 Update 1 released a new plug-in tracing feature, where we can write our log in the plugintracelog
entity so that later it can be viewed from the CRM UI.
These logs will be written only when using the ITracingService
service. By default this feature is disabled; we can enable it by navigating to Settings | Administration | System Settings | Customization.
You can get details about ITracingService
from: https://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.itracingservice.aspx.
3.147.6.118