Plugin on Pre-Validation Stage in Dynamics 365 CE

Recently we had a requirement to delete the Account record without deleting the associated Contact records.

If we try deleting the account record we’d get the following message box

The relationship definition can’t be updated as well to achieve this

So, we wrote a plugin on the pre-validation stage of pre-delete event of Account, which will retrieve and loop through all the child contact records and set its parent customer field as null.

In case of pre-operation the child records were not available.


public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext pluginContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService organizationService = serviceFactory.CreateOrganizationService(pluginContext.UserId);
EntityReference targetEntity = (EntityReference)pluginContext.InputParameters["Target"];
QueryExpression getContacts = new QueryExpression("contact");
getContacts.Criteria.AddCondition(new ConditionExpression("parentcustomerid", ConditionOperator.Equal, targetEntity.Id));EntityCollection allContacts = organizationService.RetrieveMultiple(getContacts);foreach (Entity contact in allContacts.Entities)
{
Entity contactToBeUpdated = new Entity("contact");
contactToBeUpdated.Id = contact.Id;
contactToBeUpdated.Attributes["parentcustomerid"] = null;
organizationService.Update(contactToBeUpdated);
}}

 

Another practical scenario

https://www.inogic.com/blog/2017/03/plugin-pre-validation-operation-to-show-an-error-message-as-well-as-log-the-error/

Hope it helps..

Advertisements

Unit Test RetrieveAttributeResponse in CRM using Microsoft Fakes.

Hi,

Recently we wrote a plugin that was using RetrieveAttribute class to get the label as well as value of the optionset field.

The issue that we faced over here is that the AttributeMetadata property of RetrieveAttributeResponse is read only, we cannot set it. The following post provided the solution i.e. writing a wrapper class over the RetrieveAttributeResponse

http://www.alexanderdevelopment.net/post/2013/01/13/How-to-unit-test-C-Dynamics-CRM-interface-code-part-III


organizationService.ExecuteOrganizationRequest = request =>
 {
 var retrievedPicklistAttributeMetadata = new PicklistAttributeMetadata();

if (request.Parameters["LogicalName"].ToString() == "lss_service_request_status")
 {
 var optionMetadata = new OptionMetadata(new Label("Closed", 1033), 10);
 optionMetadata.Label.UserLocalizedLabel = new LocalizedLabel("Closed", 1033);
 optionMetadata.Label.UserLocalizedLabel.Label = "Closed";

var serviceRequestTypeOptionSet = new OptionSetMetadata
 {
 Name = "lss_service_request_status",
 DisplayName = new Label("Service Request Status", 1033),
 IsGlobal = false,
 OptionSetType = OptionSetType.Picklist,
 Options = { optionMetadata }
 };

retrievedPicklistAttributeMetadata.OptionSet = serviceRequestTypeOptionSet;
 }

var retrAttResponse = new RetrieveAttributeResponseWrapper(new RetrieveAttributeResponse());
 retrAttResponse.AttributeMetadata = retrievedPicklistAttributeMetadata;
 return retrAttResponse;
 };

Hope it helps!