Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 46379

Plugin: In ability to change account on saved case crm dynamics 2016

$
0
0

Hi Friends,

I do have custom plugin,It will calling the GUID for one security role assoicated with the root business unit.

I need to change it to calling the secuirty role for each individual business unit.

public class CaseFacilityPreUpdate : IPlugin
{

/// <summary>
///
/// </summary>
/// <param name="serviceProvider"></param>
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target"))
{
// Obtain the target entity from the input parameters.
Entity incident = context.InputParameters["Target"] as Entity;

// Verify that the target entity represents an incident.
// If not, this plug-in was not registered correctly.
if (incident != null && incident.LogicalName == "incident")
{
/*
* Build DynCRM Plugin to Manage Case Update Security
* Functional Definition: Role to update the Case’s Facility/Customer field (customerid).
* If User without this role attempts to update and Save the record, upon Save an error message saying
* “User does not have proper permissions to update the Account/Facility field on Case’s. Please contact your manager to make the update. Administrator Note: the Case Update – Facility/Customer Security Role is required.”
* and the value will not be updated on the Case.
*/
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService organizationService = serviceFactory.CreateOrganizationService(context.UserId);
if (ValidateRoleToUpdateFacility(serviceFactory, organizationService, context.UserId) == false)
{
throw new Exception("User does not have proper permissions to update the Account/Facility field on Case’s. Please contact your manager to make the update. Administrator Note: the Case Update – Facility/Customer Security Role is required.");
}
}
}
}

/// <summary>
///
/// </summary>
private bool ValidateRoleToUpdateFacility(IOrganizationServiceFactory serviceFactory, IOrganizationService organizationService, Guid currentUserId)
{
bool doesCurrentUserHaveProperSecurityRole = false;
Guid caseUpdateFacilitySecurityRoleId = new Guid("8CDC5E56-DF45-E511-BE65-0050568E3F49");
List<Guid> rolesList = new List<Guid>();
string fetchXml = @"<fetch mapping=""logical"" count=""50"" version=""1.0"">
<entity name=""role"">
<attribute name=""name"" />
<attribute name=""roleid"" />
<link-entity name=""systemuserroles"" from=""roleid"" to=""roleid"">
<filter>
<condition attribute=""systemuserid"" operator=""eq"" value=""{0}"" />
<condition attribute=""roleid"" operator=""eq"" value=""{1}"" />
</filter>
</link-entity>
</entity>
</fetch>";
fetchXml = string.Format(fetchXml, currentUserId, caseUpdateFacilitySecurityRoleId);
var securityRolesResult = organizationService.RetrieveMultiple(new FetchExpression(fetchXml));
if (securityRolesResult != null)
{
if (securityRolesResult.Entities != null)
{
if (securityRolesResult.Entities.Count > 0)
doesCurrentUserHaveProperSecurityRole = true;

}
}
return doesCurrentUserHaveProperSecurityRole;
}

}


Viewing all articles
Browse latest Browse all 46379

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>