{"id":3412,"date":"2016-08-08T17:26:21","date_gmt":"2016-08-08T11:56:21","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=3412"},"modified":"2016-08-08T17:26:21","modified_gmt":"2016-08-08T11:56:21","slug":"scenario-where-impersonation-in-custom-workflow-can-be-used-for-dynamics-crm","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2016\/08\/scenario-where-impersonation-in-custom-workflow-can-be-used-for-dynamics-crm\/","title":{"rendered":"Scenario where Impersonation in custom workflow can be used for Dynamics CRM"},"content":{"rendered":"<p style=\"text-align: justify;\"><strong>Requirement:<\/strong><strong>\u00a0<\/strong><\/p>\n<p style=\"text-align: justify;\">Recently we had a request where client wanted to have an automated process of assigning the case to a team member.<\/p>\n<p style=\"text-align: justify;\">To explain further, whenever any case is\u00a0created in Dynamics CRM and when it is\u00a0assigned to a team, that case should automatically get assigned to the first team member of that team. Once the case is assigned to the first team member, and if a new case gets assigned to same team again\u00a0then that new case should be assigned to the second team member instead of first one.<\/p>\n<p style=\"text-align: justify;\">In this process of automation, if any user does not want case to be assigned to him for some period then he can remove himself from the team and when he is available then he could add himself back in the same team. For this scenario, we started thinking of impersonation.<\/p>\n<p style=\"text-align: justify;\">Because in this scenario, normal user (User who does not have the privilege to add\/ remove a user in a team) can Add\/Remove himself in a team.<\/p>\n<p style=\"text-align: justify;\"><strong>Problem Faced:<\/strong><\/p>\n<p style=\"text-align: justify;\">Here is\u00a0the problem faced while adding the user in a team.<\/p>\n<p style=\"text-align: justify;\">After creating real time OOB workflow that has the owner as System Administrator user and which will call custom assembly.<\/p>\n<p style=\"text-align: justify;\">The normal user need to run this real time OOB on-demand in order to add himself to team by clicking the \u201c<strong>Run Workflow<\/strong>\u201d button on team.<\/p>\n<p style=\"text-align: justify;\">In custom assembly, we had used \u201c<strong>AddMembersTeamRequest<\/strong>\u201d to Add the members in team and passed the \u201c<strong>UserId<\/strong>\u201d to the CreateOrganizationService.<\/p>\n<p style=\"text-align: justify;\">When we ran the OOB on team by clicking the \u201c<strong>RUN WORKFLOW<\/strong>\u201d, below shown permission error popped up as normal user ran the workflow that did not had privilege to add member in a team.\u00a0<a href=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2016\/08\/business-process-error.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-3413\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2016\/08\/business-process-error.png\" alt=\"business process error\" width=\"556\" height=\"265\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">On team, after executing the OOB on-demand, we noticed below thing.<\/p>\n<ul style=\"text-align: justify;\">\n<li>Whenever we run the workflow on-demand, we get the UserId and IntiatingUserId as follows.<\/li>\n<\/ul>\n<ol style=\"text-align: justify;\">\n<li>UserId: User running on-demand workflow.<\/li>\n<li>InitiatingUserId: User running on-demand workflow.<\/li>\n<\/ol>\n<p style=\"text-align: justify;\">As you can see, both are same.<\/p>\n<ul style=\"text-align: justify;\">\n<li>And whenever any workflow get trigger automatically<\/li>\n<\/ul>\n<ol style=\"text-align: justify;\">\n<li>UserId: Workflow owner<\/li>\n<li>InitiatingUserId: Triggering user.<\/li>\n<\/ol>\n<p style=\"text-align: justify;\">In our case, in custom assembly we had passed the UserId to CreateOrganizationService which means assembly was running under normal user (user who didn&#8217;t had the privilege to add user in team) that&#8217;s why we were getting the error.<\/p>\n<p style=\"text-align: justify;\"><strong>Solution:<\/strong><\/p>\n<p style=\"text-align: justify;\">To overcome this, we retrieved SYSTEM user id and passed that user id to the CreateOrganizationService as shown below.<\/p>\n<pre class=\"lang:default decode:true \">public Guid getUserId(IOrganizationService service, string userName)\n        {\n            Guid systemUserId = Guid.Empty;\n            try\n            {\n                QueryByAttribute queryByAttribute = new QueryByAttribute(\"systemuser\");\n                ColumnSet columns = new ColumnSet(\"systemuserid\");\n                queryByAttribute.AddAttributeValue(\"fullname\", userName);\n\n                EntityCollection retrieveUser = service.RetrieveMultiple(queryByAttribute);\n                systemUserId = ((Entity)retrieveUser.Entities[0]).Id;\n            }\n            catch (FaultException&lt;OrganizationServiceFault&gt; e)\n            {\n                throw new Exception(\"Error: &gt;&gt;\" + e.Message);\n            }\n\n            return systemUserId;\n        }\n\n   IOrganizationService systemService = serviceFactory.CreateOrganizationService(getUserId(service, \u201cSYSTEM\u201d));\n<\/pre>\n<p>So the workflow ran under the system admin user who has all the privileges and user were added to team successfully.<\/p>\n<p style=\"text-align: justify;\">Or else we can directly pass the guid of the any other user who has the privilege to add the user in a team to the createOrganizationService as shown below.<\/p>\n<p style=\"text-align: justify;\">IOrganizationService service = serviceFactory.CreateOrganizationService(new Guid(&#8220;C1A5741C-6131-E611-80EE-C4346BDC2F41&#8221;));<\/p>\n<p style=\"text-align: justify;\"><strong>Conclusion:<\/strong><\/p>\n<p style=\"text-align: justify;\">In this way Impersonation can be achieved in custom workflows by passing the SYSTEM user id to the CreateOrganizationService. So that user can automate the process of assigning the case to a team Member.<\/p>\n<p style=\"text-align: justify;\">Have you checked whats new in <a href=\"http:\/\/bit.ly\/maplytics-august-release\" target=\"_blank\" rel=\"noopener noreferrer\">Maplytics August Release<\/a>?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Requirement:\u00a0 Recently we had a request where client wanted to have an automated process of assigning the case to a team member. To explain further, whenever any case is\u00a0created in Dynamics CRM and when it is\u00a0assigned to a team, that case should automatically get assigned to the first team member of that team. Once the\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2016\/08\/scenario-where-impersonation-in-custom-workflow-can-be-used-for-dynamics-crm\/\">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,19,24,67],"tags":[917,918],"class_list":["post-3412","post","type-post","status-publish","format-standard","hentry","category-development","category-dynamics-crm","category-dynamics-crm-2016","category-workflows","tag-impersonation-in-custom-workflow","tag-impersonation-in-custom-workflow-in-dynamics-crm"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/3412","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=3412"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/3412\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=3412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=3412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=3412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}