public override void ExecutePlugin() { Trace("Plugin Start"); var triggerEvent = PluginCtx.MessageName; Trace("Has us_opportunitytype " + Target.Contains("us_opportunitytype").ToString()); var opportunutytype = Target.GetAttributeValue <OptionSetValue>("us_opportunitytype"); if (opportunutytype == null) { return; } Trace("opportunutytype " + opportunutytype.Value.ToString()); us_autonumber autoNumber = GetTargetAutoNumberRecord(opportunutytype.Value); LockAutoNumberRecord(autoNumber); var targetAttribute = autoNumber.us_field_name; Trace("targetAttribute " + targetAttribute.ToString()); if (PluginCtx.MessageName == PluginMessage.Update && !Target.Contains(targetAttribute)) { return; // Continue, if this is an Update event and the target does not contain the trigger value } else if (Target.Contains(targetAttribute) && !string.IsNullOrWhiteSpace(Target.GetAttributeValue <string>(targetAttribute))) { return; // Continue so we don't overwrite a manual value } else if (triggerEvent == PluginMessage.Update && PreImage.Contains(targetAttribute) && !string.IsNullOrWhiteSpace(PreImage.GetAttributeValue <string>(targetAttribute))) { return; // Continue, so we don't overwrite an existing value } var numDigits = autoNumber.us_digits; Trace("numDigit " + numDigits); var prefix = string.Empty; var postfix = string.Empty; if (autoNumber.Contains(EntityMetadata <us_autonumber> .AttributeName(a => a.us_prefix))) { prefix = ReplacePredefined(autoNumber.us_prefix); } if (autoNumber.Contains(EntityMetadata <us_autonumber> .AttributeName(a => a.us_postfix))) { postfix = ReplacePredefined(autoNumber.us_postfix); } Trace("autoNumber.us_next_number.Value " + autoNumber.us_next_number); //Trace("autoNumber.us_next_number.Value " + autoNumber.us_next_number); var number = numDigits == 0 ? "" : autoNumber.us_next_number.Value.ToString("D" + numDigits); Trace("Number " + number.ToString()); Target[targetAttribute] = $"{prefix}{number}{postfix}"; var updatedAutoNumber = new us_autonumber() { Id = autoNumber.Id, us_next_number = autoNumber.us_next_number + 1, us_preview = Target[targetAttribute].ToString() }; OrgSvc.Update(updatedAutoNumber); }
public void Execute(IServiceProvider serviceProvider) { #region definitionAttributes Entity PreImage; string entityName = "new_contracting"; Guid paymentTerms = default(Guid); Guid UnitID = default(Guid); #endregion //Extract the tracing service for use in debugging sandboxed plug-ins. ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); // Obtain the execution context from the service provider. IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext)); IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); // The InputParameters collection contains all the data passed in the message request. if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { // Obtain the target entity from the input parameters. Entity entity = (Entity)context.InputParameters["Target"]; // Verify that the target entity represents an sale. // If not, this plug-in was not registered correctly. if (entity.LogicalName != entityName) return; //Update if (context.MessageName.ToLower() == "update") { PreImage = (Entity)context.PreEntityImages["PreImage"]; if (PreImage == null) return; if (entity.Attributes.Contains("new_paymentterms")) paymentTerms = ((EntityReference)entity.Attributes["new_paymentterms"]).Id; else if (PreImage.Contains("new_paymentterms")) paymentTerms = ((EntityReference)PreImage.Attributes["new_paymentterms"]).Id; if (entity.Attributes.Contains("new_unit")) UnitID = ((EntityReference)entity.Attributes["new_unit"]).Id; else if (PreImage.Contains("new_unit")) UnitID = ((EntityReference)PreImage.Attributes["new_unit"]).Id; } //Create if (context.MessageName.ToLower() == "create") { if (entity.Attributes.Contains("new_paymentterms")) paymentTerms = ((EntityReference)entity.Attributes["new_paymentterms"]).Id; if (entity.Attributes.Contains("new_unit")) UnitID = ((EntityReference)entity.Attributes["new_unit"]).Id; } int unitBuildingStatus = ((OptionSetValue)service.RetrieveMultiple(Fetches.BuildingStatueString(UnitID)).Entities.FirstOrDefault()["ohd_buildingstatus"]).Value; EntityCollection Result = service.RetrieveMultiple(Fetches.PaymentTerms_String(unitBuildingStatus, UnitID)); if (Result.Entities.Count > 0) { bool IsFound = false; foreach (Entity PaymentTerm in Result.Entities) { if (PaymentTerm["new_paymenttermsid"].ToString() == paymentTerms.ToString()) { IsFound = true; break; } } if (!IsFound) { throw new InvalidPluginExecutionException("Sorry!! You must choose payment term related to the same unit and have the building status equal building status of related unit"); } } } }