{"id":3512,"date":"2016-08-26T17:03:50","date_gmt":"2016-08-26T11:33:50","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=3512"},"modified":"2016-08-26T17:03:50","modified_gmt":"2016-08-26T11:33:50","slug":"change-createdbymodifiedby-field-at-runtime-in-dynamics-crm-plugin","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2016\/08\/change-createdbymodifiedby-field-at-runtime-in-dynamics-crm-plugin\/","title":{"rendered":"Change Createdby\/Modifiedby Field at Runtime in Dynamics CRM Plugin"},"content":{"rendered":"<p style=\"text-align: justify;\">Recently we came across a scenario where we were assigning CreatedBy and ModifiedBy field of the record that was created by plugin dynamically based on the data of the record that was getting created by plugin.<\/p>\n<p style=\"text-align: justify;\">Let us say, my Contact entity that has field \u201cnew_createdby\u201d, \u201dnew_modifiedby\u201d and its records are getting created by plugin. In our scenario if user wants to set \u201ccreatedby\/modifiedby\u201d field dynamically based on the new_createdby\/new_modifiedby field value while the records get created through plugin, then in that case below code will help you up in assigning createdby\/modifiedby for the record dynamically based on the particular field value.<\/p>\n<pre class=\"lang:default decode:true\">\/\/get entity record for which plugin was fired\nEntity _target = (Entity)pluginExecutionContext.InputParameters[\"Target\"];\n\n\n\/\/check if portaluser name is to be obtained from custom createby or from custom modifiedby\nif (pluginExecutionContext.MessageName.ToUpper() == \"CREATE\")\n{\ncontactid = _target.Attributes.Contains(\"new_createdby\") ? _target.GetAttributeValue&lt;EntityReference&gt;(\"new_createdby\").Id : Guid.Empty;\n}\nelse\n{\ncontactid = _target.Attributes.Contains(\"new_modifiedby\") ? _target.GetAttributeValue&lt;EntityReference&gt;(\"new_modifiedby\").Id : Guid.Empty;\n}\n\n\/\/retrieve contact fullname from contactid\nvar _contact = context.CreateQuery(\"contact\").Where(c =&gt; c.GetAttributeValue&lt;Guid&gt;(\"contactid\").Equals(contactid)).FirstOrDefault();\n\n\n                if (_contact != null)\n                {\n                    if (_contact.Attributes.Contains(\"fullname\"))\n                    {\n                        fullname = _contact.GetAttributeValue&lt;string&gt;(\"fullname\");\n                    }\n\n\/\/retrieve Systemuser that has same name as that of new_portalcreatedby\/ \/\/new_portalmodifiedby\n                    Entity _user = context.CreateQuery(\"systemuser\").Where(e =&gt; e.GetAttributeValue&lt;string&gt;(\"fullname\").Equals(fullname)).FirstOrDefault();\n\n                    if (_user != null)\n                    {\n\n                        \/\/check if we need to update createdby or modifiedby\n                        if (pluginExecutionContext.MessageName.ToUpper() == \"CREATE\")\n                        {\n                            _target[\"createdby\"] = _user.ToEntityReference();\n                        }\n                        else\n                        {\n                            _target[\"modifiedby\"] = _user.ToEntityReference();\n                        }\n\n                        \/\/assign new target to plugin executioncontext\n                        pluginExecutionContext.InputParameters[\"Target\"] = _target;\n                    }<\/pre>\n<p>This will help you to change createdby\/modifiedby field at runtime in plugin.<\/p>\n<p>Planning to Move to <a href=\"http:\/\/www.inogic.com\/product\/data-conversion\/dynamics-crm-on-premise-to-online\" target=\"_blank\" rel=\"noopener noreferrer\">Cloud from Dynamics CRM On-Premises<\/a>? We can help you!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently we came across a scenario where we were assigning CreatedBy and ModifiedBy field of the record that was created by plugin dynamically based on the data of the record that was getting created by plugin. Let us say, my Contact entity that has field \u201cnew_createdby\u201d, \u201dnew_modifiedby\u201d and its records are getting created by plugin.\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2016\/08\/change-createdbymodifiedby-field-at-runtime-in-dynamics-crm-plugin\/\">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":[19,24,42],"tags":[374,1196],"class_list":["post-3512","post","type-post","status-publish","format-standard","hentry","category-dynamics-crm","category-dynamics-crm-2016","category-plugin","tag-createdby-field-dynamics-crm","tag-modifiedby-field-dynamics-crm"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/3512","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=3512"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/3512\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=3512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=3512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=3512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}