{"id":1276,"date":"2015-05-11T12:41:29","date_gmt":"2015-05-11T07:11:29","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=1276"},"modified":"2015-05-11T12:41:29","modified_gmt":"2015-05-11T07:11:29","slug":"api-enhancement-service-update-message-in-crm-2015-update-1","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2015\/05\/api-enhancement-service-update-message-in-crm-2015-update-1\/","title":{"rendered":"API enhancement \u2013 Service.Update Message in CRM 2015 Update 1"},"content":{"rendered":"<p><span style=\"text-decoration: underline;\"><strong>Introduction:<br \/>\n<\/strong><\/span><\/p>\n<p>For updating an existing record in Dynamics CRM, we have always had the Service.Update message. Pass in the entity object and it would update the corresponding record back in CRM. This message however only allowed you to update regular fields. CRM has always had some special fields like Status, Owner etc that could not be updated using the Update Message. These had special messages in the SDK to update the values.<\/p>\n<p>To update Owner, use Assign request<\/p>\n<p>To update Status, use the SetState request.<\/p>\n<p>In this current release, the API has been update, to remove the need to execute special messages for Status and Owner and instead we can now update these by setting these fields and passing them as a part of the Update request to CRM.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Code Sample differences:<br \/>\n<\/strong><\/span><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Updating the rating, the Owner and the status of the contact record in Dynamics CRM<br \/>\n<\/strong><\/span><\/p>\n<p><span style=\"color: black; background-color: white;\">Entity contact = new Entity();<br \/>\n<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: black; background-color: white;\">\/\/specify the entity logical name<br \/>\n<\/span><\/p>\n<p><span style=\"color: black; background-color: white;\">contact.LogicalName = &#8220;contact&#8221;;<br \/>\n<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: black; background-color: white;\">\/\/specify the contact that need to be updated<br \/>\n<\/span><\/p>\n<p><span style=\"color: black; background-color: white;\">contact.Id = new Guid(&#8220;A925C42D-CFE4-E411-80E8-C4346BAD5414&#8221;);<br \/>\n<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>\/\/Set the fax field<\/p>\n<p style=\"text-align: justify;\"><span style=\"color: black; background-color: white;\">contact[&#8220;fax&#8221;] = &#8220;412&#8221;;<br \/>\n<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>\/\/Call the update message<\/p>\n<p><span style=\"color: black;\"><span style=\"background-color: white;\">service.Update(contact);<\/span><br \/>\n<\/span><\/p>\n<p>\/\/Call the Assign to change the owner<\/p>\n<p>AssignRequest assign = new AssignRequest<\/p>\n<p>{<\/p>\n<p>Assignee = new EntityReference(SystemUser.EntityLogicalName, _otherUserId),<\/p>\n<p>Target = new EntityReference(Contact.EntityLogicalName,<span style=\"color: black; background-color: white;\">new Guid(&#8220;A925C42D-CFE4-E411-80E8-C4346BAD5414&#8221;)<\/span>)<\/p>\n<p>};<\/p>\n<p>\/\/ Execute the Request<\/p>\n<p>_service.Execute(assign);<\/p>\n<p>\/\/Now change the status<\/p>\n<p>\/\/ Create the Request Object<\/p>\n<p>SetStateRequest state = new SetStateRequest();<\/p>\n<p>EntityReference contactReference = new EntityReference()<\/p>\n<p>{<\/p>\n<p>LogicalName = Contact.EntityLogicalName,<\/p>\n<p>Id = <span style=\"color: black; background-color: white;\">new Guid(&#8220;A925C42D-CFE4-E411-80E8-C4346BAD5414&#8221;)<\/span><\/p>\n<p>};<\/p>\n<p>state.EntityMoniker = contactReference<\/p>\n<p>\/\/ Set the Request Object&#8217;s Properties<\/p>\n<p>state.State = new OptionSetValue(1);<\/p>\n<p>state.Status = new OptionSetValue(2);<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Execute the Request<\/p>\n<p>_service.Execute(state);<\/p>\n<p>&nbsp;<\/p>\n<p>As you can see, it took 3 separate request to get these 3 fields updated.<\/p>\n<p>With the new API you can simply perform all of these three with a single Update request<\/p>\n<p><span style=\"color: black; background-color: white;\">Entity contact = new Entity();<br \/>\n<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: black; background-color: white;\">\/\/specify the entity logical name<br \/>\n<\/span><\/p>\n<p><span style=\"color: black; background-color: white;\">contact.LogicalName = &#8220;contact&#8221;;<br \/>\n<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: black; background-color: white;\">\/\/specify the contact that need to be updated<br \/>\n<\/span><\/p>\n<p><span style=\"color: black; background-color: white;\">contact.Id = new Guid(&#8220;A925C42D-CFE4-E411-80E8-C4346BAD5414&#8221;);<br \/>\n<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><span style=\"color: black; background-color: white;\">\/\/set the fax<br \/>\n<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"color: black; background-color: white;\">contact[&#8220;fax&#8221;] = &#8220;412&#8221;;<br \/>\n<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"color: black; background-color: white;\">\/\/change the owner of the contact<br \/>\n<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"color: black; background-color: white;\">contact[&#8220;ownerid&#8221;] = new EntityReference(&#8220;systemuser&#8221;, new Guid(&#8220;FB8C44BF-B0E9-E411-80ED-C4346BAD5414&#8221;));<br \/>\n<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: black; background-color: white;\">\/\/change the status to inactive<br \/>\n<\/span><\/p>\n<p><span style=\"color: black; background-color: white;\">contact[&#8220;statecode&#8221;] = new OptionSetValue(1);<br \/>\n<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: black; background-color: white;\">\/\/change the state to inactive<br \/>\n<\/span><\/p>\n<p><span style=\"color: black; background-color: white;\">contact[&#8220;statuscode&#8221;] = new OptionSetValue(2);<br \/>\n<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: black; background-color: white;\">\/\/update the contact<br \/>\n<\/span><\/p>\n<p><span style=\"color: black;\"><span style=\"background-color: white;\">service.Update(contact);<\/span><br \/>\n<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Impact on Plugins:<br \/>\n<\/strong><\/span><\/p>\n<p>The SetState and Assign request have always called the Update request also internally. This means, if you had a plugin registered on SetState, Assign and Update message of the &#8220;Contact&#8221; entity. The sample code above would have executed the SetState and Update, both plugins for just the SetState request. Similarly the Assign would have executed the plugin registered on both Assign as well as Update message. So it was the Update message that was executed irrespective of which fields or through which message a record was updated in Dynamics CRM.<\/p>\n<p>The new API Update message, will not internally execute the SetState or Assign request if you were to execute the above sample code to update the owner and the status. So any previous plugins registered on the SetState or the Assign request will be have to re-registered on the Update message, should you plan to start using the new Update message to update the owner and status as well.<\/p>\n<p>When you change the owner or the state of the record from CRM UI, it still executes the SetState and Assign Plugin, so it does not impact there.<\/p>\n<p>The new API would not execute the SetState or Assign message plugin, BUT, it will call an UPDATE request for each of these messages i.e the sample code above would result in the UPDATE plugin being executed thrice.<\/p>\n<p>1<sup>st<\/sup> time for the updating Fax \u2013 This time, the target would receive only the Fax field (non-owner\/non-status fields)<\/p>\n<p>2<sup>nd<\/sup> time for the Owner \u2013 only the owner field is available in Target<\/p>\n<p>3<sup>rd<\/sup> time for the state \u2013 This time only status fields are passed in Target.<\/p>\n<p>The following sample plugin code, would help you understand how to identify for which field the plugin has been triggered.<\/p>\n<p style=\"margin-left: 36pt;\">\/\/ Check if plugin context contains the Target<\/p>\n<p style=\"margin-left: 36pt;\">if (context.InputParameters.ContainsKey(&#8220;Target&#8221;))<\/p>\n<p style=\"margin-left: 36pt;\">{<\/p>\n<p style=\"margin-left: 36pt;\">Entity targetContactEntity = (Entity)context.InputParameters[&#8220;Target&#8221;];<\/p>\n<p style=\"margin-left: 36pt;\">string desc = &#8220;&#8221;;<\/p>\n<p style=\"margin-left: 36pt;\">if (targetContactEntity!= null)<\/p>\n<p style=\"margin-left: 36pt;\">{<\/p>\n<p style=\"margin-left: 36pt;\">\/\/ When fired for owner change<\/p>\n<p style=\"margin-left: 36pt;\">if (targetContactEntity.Attributes.ContainsKey(&#8220;ownerid&#8221;))<\/p>\n<p style=\"margin-left: 36pt;\">{<\/p>\n<p style=\"margin-left: 36pt;\">desc = &#8220;Owner changed&#8221;;<\/p>\n<p style=\"margin-left: 36pt;\">}<\/p>\n<p style=\"margin-left: 36pt;\">\/\/ When fired for status change<\/p>\n<p style=\"margin-left: 36pt;\">if (targetContactEntity.Attributes.ContainsKey(&#8220;statecode&#8221;))<\/p>\n<p style=\"margin-left: 36pt;\">{<\/p>\n<p style=\"margin-left: 36pt;\">desc = &#8220;Status Changed&#8221;;<\/p>\n<p style=\"margin-left: 36pt;\">}<\/p>\n<p style=\"margin-left: 36pt;\">\/\/ Create Task activity<\/p>\n<p style=\"margin-left: 36pt;\">Entity task = new Entity(&#8220;task&#8221;);<\/p>\n<p style=\"margin-left: 36pt;\">task[&#8220;subject&#8221;] = &#8220;Task from plugin : &#8221; + desc;<\/p>\n<p style=\"margin-left: 36pt;\">_service.Create(task);<\/p>\n<p style=\"margin-left: 36pt;\">}<\/p>\n<p style=\"margin-left: 36pt;\">}<\/p>\n<p>&nbsp;<\/p>\n<ol style=\"margin-left: 54pt;\">\n<li>Once for all non -owner \/ non-status fields i.e. &#8220;fax&#8221; \u2013 When fired for rest of these fields the target entity attribute does not contain &#8220;owner&#8221; and &#8220;statecode&#8221;. Hence the &#8220;Task&#8221; which we are creating in this plug-in is created with subject \u2013 &#8220;Task from plug-in&#8221;.<\/li>\n<li>Once for owner field change \u2013 When fired for owner field the target entity attribute will only contain updated owner field. So here we can find out if the plug-in has been fired for owner field change by checking for if the target entity attribute contains &#8220;ownerid&#8221; as done in above plug-in code. Hence the &#8220;Task&#8221; is created with subject \u2013 &#8220;Task from plug-in : Owner changed&#8221;.<\/li>\n<li>\n<div>Once for Status field change &#8211; When fired for status field the target entity attribute will only contain updated status field. So here we can find out if the plug-in has been fired for status field change by checking for if the target entity attribute contains &#8220;statecode&#8221; as done in above plug-in code. Hence the &#8220;Task&#8221; is created with subject \u2013 &#8220;Task from plug-in : Status changed&#8221;.<\/div>\n<p>&nbsp;<\/li>\n<\/ol>\n<p>So total 3 tasks will be created on single update request executed in the sample code above. The depth each time would be 1.<\/p>\n<p><strong>Impact on Workflow<\/strong>:<\/p>\n<ol>\n<li>Workflows registered for the <strong>Assign<\/strong> message by users continue to be triggered by updates to owner fields.<\/li>\n<li>Workflows containing the <strong>Change Status<\/strong> step continue to be triggered by updates to state\/status fields.<\/li>\n<\/ol>\n<p><strong>For example<\/strong>:<\/p>\n<p>If you have a workflow registered to execute when a record is assigned, updating the owner of the record using &#8220;<strong>Update<\/strong>&#8221; operation would trigger the workflow. Similarly for the Status changes workflow, they are triggered as well when the state is changed using the new Update message.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Conclusion:<br \/>\n<\/strong><\/span><\/p>\n<ul>\n<li>Allows to change the Status, State, Owner, Business Unit and Manager using a single Update operation.<\/li>\n<li>Reduces the code complexity.<\/li>\n<li>Additional service calls to the CRM are reduced thereby improving performance.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: For updating an existing record in Dynamics CRM, we have always had the Service.Update message. Pass in the entity object and it would update the corresponding record back in CRM. This message however only allowed you to update regular fields. CRM has always had some special fields like Status, Owner etc that could not\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2015\/05\/api-enhancement-service-update-message-in-crm-2015-update-1\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15,23,24],"tags":[],"class_list":["post-1276","post","type-post","status-publish","format-standard","hentry","category-development","category-dynamics-crm-2015-update-1","category-dynamics-crm-2016"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/1276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=1276"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/1276\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=1276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=1276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=1276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}