public void Execute(IServiceProvider serviceProvider) { if (serviceProvider == null) { throw new ArgumentNullException(nameof(serviceProvider)); } // Construct the local plug-in context. LocalPluginContext localcontext = new LocalPluginContext(serviceProvider); localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Entered {0}.Execute()", ChildClassName)); try { // Invoke the custom implementation ExecuteCrmPlugin(localcontext); // now exit - if the derived plug-in has incorrectly registered overlapping event registrations, // guard against multiple executions. } catch (FaultException <OrganizationServiceFault> e) { localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Exception: {0}", e.ToString())); // Handle the exception. throw; } finally { localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Exiting {0}.Execute()", ChildClassName)); } }
public static Entity Retrieve(this LocalPluginContext local, string entityname, Guid id, ColumnSet columns) { local.Trace($"Internal R {entityname}, {id}"); var sw = Stopwatch.StartNew(); var result = local.OrganizationService.Retrieve(entityname, id, columns); sw.Stop(); local.Trace($"Returning record after {sw.ElapsedMilliseconds} ms"); return(result); }
public static Entity GetParent(this Entity entity, LocalPluginContext context, string lookupattribute, params string[] attributes) { if (!entity.Contains(lookupattribute)) { context.Trace($"Missing lookup attribute {lookupattribute}"); return(null); } if (!(entity[lookupattribute] is EntityReference parentref)) { context.Trace($"{lookupattribute} is not EntityReference"); return(null); } return(context.OrganizationService.Retrieve(parentref.LogicalName, parentref.Id, new ColumnSet(attributes))); }
public static EntityCollection RetrieveMultiple(this LocalPluginContext local, QueryBase query) { local.Trace($"Internal RM {query.GetType()}"); if (query is QueryExpression qx) { local.Trace($"Querying entity {qx.EntityName}"); local.Trace($"Attributes: {string.Join(", ", qx.ColumnSet.Columns)}"); } var sw = Stopwatch.StartNew(); var result = local.OrganizationService.RetrieveMultiple(query); sw.Stop(); local.Trace($"Returning {result.Entities.Count} records after {sw.ElapsedMilliseconds} ms"); return(result); }
protected override void ExecuteCrmPlugin(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException(nameof(localContext)); } localContext.Trace("Hello world"); if (localContext.PluginExecutionContext.PrimaryEntityName != "contact") { localContext.Trace($"Triggered incorrectly for {localContext.PluginExecutionContext.PrimaryEntityName}"); return; } localContext.Trace($"PreImage count: {localContext.PluginExecutionContext.PreEntityImages.Count}"); localContext.Trace($"PostImage count: {localContext.PluginExecutionContext.PostEntityImages.Count}"); if (!(localContext.PluginExecutionContext.PostImage() is Entity contact)) { return; } localContext.Trace($"Entity is {contact.LogicalName} {contact.Id}"); if (!(contact.GetParent(localContext, Contact.CompanyName, Account.PrimaryName, Account.AccountNumber) is Entity account)) { return; } localContext.Trace($"Parent is {account.LogicalName} {account[Account.PrimaryName]}"); var cases = account.GetChildren(localContext, Case.EntityName, Case.Customer, Case.Subject, Case.CaseNumber); localContext.Trace($"Found {cases.Count()} cases: {string.Join(", ", cases.Select(c => c[Case.CaseNumber]))}"); foreach (var acase in cases) { acase[Case.Description] = null; acase[Case.Satisfaction] = new OptionSetValue((int)Case.Satisfaction_OptionSet.VerySatisfied); localContext.OrganizationService.Update(acase); } }