Exemplo n.º 1
0
        public override void Execute(JonasPluginBag bag)
        {
            var pcref = bag.TargetEntity.GetAttributeValue <EntityReference>("primarycontactid");

            if (pcref == null || pcref.Id.Equals(Guid.Empty))
            {
                bag.Trace("PC is null");
                return;
            }
#if DEBUG
            // This will enable extensive details about requests and results when compiling with debug configuration
            bag.TracingService.Verbose = true;
#endif
            bag.TraceBlockStart("Check other accounts");
            bag.Trace("Looking for other accounts with Primary Contact {0}", pcref.Id);
            var qry = new QueryExpression("account");
            qry.ColumnSet.AddColumn("name");
            qry.Criteria.AddCondition("accountid", ConditionOperator.NotEqual, bag.PluginContext.PrimaryEntityId);
            qry.Criteria.AddCondition("primarycontactid", ConditionOperator.Equal, pcref.Id);
            var otheraccounts = bag.Service.RetrieveMultiple(qry);
            bag.TraceBlockEnd();
            if (otheraccounts.Entities.Count > 0)
            {
                bag.TraceBlockStart("Throwing up");
                var acctnames = string.Join(", ", otheraccounts.Entities.Select(e => e.GetAttributeValue <string>("name") ?? "?"));
                bag.Trace("Other account(s): {0}", acctnames);
                var contact = bag.Service.Retrieve("contact", pcref.Id, new ColumnSet("fullname"));
                throw new InvalidPluginExecutionException($"{contact.Name(bag, false)} is already primary contact for account(s) :\n{acctnames}");
                bag.TraceBlockEnd();
            }
        }
Exemplo n.º 2
0
 // Update contacts with the address information of the account that triggered the plugin
 private static void UpdateContacts(JonasPluginBag bag, EntityCollection contacts)
 {
     bag.TraceBlockStart();
     foreach (var contact in contacts.Entities)
     {
         bag.TraceBlockStart($"Updating contact: {contact.Name(bag, false)}");
         foreach (var attr in addressAttributes)
         {
             UpdateAttribute(bag, contact, attr);
         }
         bag.Service.Update(contact);
         bag.TraceBlockEnd();
     }
     bag.TraceBlockEnd();
 }
Exemplo n.º 3
0
        public override void Execute(JonasPluginBag bag)
        {
            if (bag == null)
            {
                throw new ArgumentNullException(nameof(bag));
            }

            bag.Trace("Hello world");

            if (bag.PluginContext.PrimaryEntityName != "contact")
            {
                bag.Trace($"Triggered incorrectly for {bag.PluginContext.PrimaryEntityName}");
                return;
            }
            bag.TraceBlockStart("Checking images");
            bag.Trace($"PreImage count: {bag.PluginContext.PreEntityImages.Count}");
            bag.Trace($"PostImage count: {bag.PluginContext.PostEntityImages.Count}");
            bag.TraceBlockEnd();
            if (!(bag.PostImage is Entity contact))
            {
                return;
            }
            bag.TraceBlockStart("Doing the things");
            bag.Trace($"Entity is {contact.LogicalName} {contact.Id}");

            if (!(contact.GetParent(bag, Contact.CompanyName, Account.PrimaryName, Account.AccountNumber) is Entity account))
            {
                return;
            }
            bag.Trace($"Parent is {account.LogicalName} {account[Account.PrimaryName]}");

            var cases = account.GetChildren(bag, Case.EntityName, Case.Customer, Case.Subject, Case.CaseNumber);

            bag.Trace($"Found {cases.Count()} cases: {string.Join(", ", cases.Select(c => c[Case.CaseNumber]))}");

            bag.TraceBlockStart("Looping cases");
            foreach (var acase in cases)
            {
                acase[Case.Description]  = null;
                acase[Case.Satisfaction] = new OptionSetValue((int)Case.Satisfaction_OptionSet.VerySatisfied);
                acase.Save(bag);
            }
            bag.TraceBlockEnd();
            bag.TraceBlockEnd();
        }
Exemplo n.º 4
0
        public static bool ReplaceRegardingCondition(this FetchExpression fetch, JonasPluginBag bag, bool makeDistinct)
        {
            bag.TraceBlockStart();
            var query = (bag.Service.Execute(new FetchXmlToQueryExpressionRequest {
                FetchXml = fetch.Query
            }) as FetchXmlToQueryExpressionResponse)?.Query;

            if (query.ReplaceRegardingCondition(bag, makeDistinct))
            {
                fetch.Query = (bag.Service.Execute(new QueryExpressionToFetchXmlRequest {
                    Query = query
                }) as QueryExpressionToFetchXmlResponse)?.FetchXml;
                return(true);
            }
            bag.TraceBlockEnd();
            return(false);
        }
Exemplo n.º 5
0
        private static void UpdateAttribute(JonasPluginBag bag, Entity contact, string attr)
        {
            bag.TraceBlockStart($"Checking: {attr}");
            var newvalue = bag.CompleteEntity.GetAttributeValue <string>(attr) ?? string.Empty;
            var oldvalue = contact.GetAttributeValue <string>(attr) ?? string.Empty;

            if (!oldvalue.Equals(newvalue))
            {
                bag.Trace("Setting {0} = {1}", attr, newvalue);
                contact[attr] = newvalue.Equals(string.Empty) ? null : newvalue;
            }
            else if (contact.Contains(attr))
            {
                bag.Trace("Attribute {0} not changed, removing from entity to update", attr);
                contact.Attributes.Remove(attr);
            }
            bag.TraceBlockEnd();
        }
Exemplo n.º 6
0
        // Retrieve all contacts of the triggering account, that have the old address of the account or no address at all.
        private static EntityCollection GetContactsInheritingAddress(JonasPluginBag bag)
        {
            bag.TraceBlockStart();
            var qry = new QueryExpression("contact");

            qry.ColumnSet.AddColumn("fullname");
            qry.ColumnSet.AddColumns(addressAttributes.ToArray());

            qry.Criteria.AddCondition("parentcustomerid", ConditionOperator.Equal, bag.PluginContext.PrimaryEntityId);

            var fltAddress     = qry.Criteria.AddFilter(LogicalOperator.Or);
            var fltNoAddress   = fltAddress.AddFilter(LogicalOperator.And);
            var fltSameAddress = fltAddress.AddFilter(LogicalOperator.And);

            foreach (var attr in addressAttributes)
            {
                fltNoAddress.AddCondition(attr, ConditionOperator.Null);
            }

            foreach (var attr in addressAttributes)
            {
                var prevalue = bag.PreImage.GetAttributeValue <string>(attr);
                bag.Trace("Adding filter for {0} = {1}", attr, prevalue);
                if (prevalue == null)
                {
                    fltSameAddress.AddCondition(attr, ConditionOperator.Null);
                }
                else
                {
                    fltSameAddress.AddCondition(attr, ConditionOperator.Equal, prevalue);
                }
            }
            var contacts = bag.Service.RetrieveMultiple(qry);

            bag.TraceBlockEnd();
            return(contacts);
        }
Exemplo n.º 7
0
        public static bool ReplaceRegardingCondition(this QueryExpression query, JonasPluginBag bag, bool makeDistinct)
        {
            bag.TraceBlockStart();
            if (query.EntityName != "activitypointer")
            {
                bag.Trace($"Wrong entity: {query.EntityName}");
                bag.TraceBlockEnd();
                return(false);
            }
            var nullCondition = query.GetCondition("activityid", ConditionOperator.Null, bag.TracingService);

            if (nullCondition == null)
            {
                bag.Trace("No null condition for activityid");
                bag.TraceBlockEnd();
                return(false);
            }
            Guid contactId;
            var  regardingCondition = query.GetCondition("regardingobjectid", ConditionOperator.Equal, bag.TracingService);

            if (regardingCondition != null &&
                regardingCondition.Operator.Equals(ConditionOperator.Equal) &&
                regardingCondition.Values.Count == 1 &&
                regardingCondition.Values[0] is Guid regardingConditionId)
            {
                contactId = regardingConditionId;
                query.RemoveCondition(regardingCondition, bag.TracingService);
            }
            else
            {
                bag.Trace("No condition for regardingobjectid");
                var contactCondition = query.GetCondition("contactid", ConditionOperator.Equal, bag.TracingService);
                if (contactCondition != null &&
                    contactCondition.Operator.Equals(ConditionOperator.Equal) &&
                    contactCondition.Values.Count == 1 &&
                    contactCondition.Values[0] is Guid contactConditionId)
                {
                    contactId = contactConditionId;
                    query.RemoveCondition(contactCondition, bag.TracingService);
                }
                else
                {
                    bag.Trace("No condition for contactid");
                    bag.TraceBlockEnd();
                    return(false);
                }
            }
            if (contactId.Equals(Guid.Empty))
            {
                bag.Trace("No contactid identified in query");
                bag.TraceBlockEnd();
                return(false);
            }

            query.RemoveCondition(nullCondition, bag.TracingService);

            bag.Trace("Adding link-entity and condition for activity party");
            var leActivityparty = query.AddLink("activityparty", "activityid", "activityid");

            leActivityparty.LinkCriteria.AddCondition("partyid", ConditionOperator.Equal, contactId);

            if (makeDistinct)
            {
                query.Distinct = true;
            }
            bag.TraceBlockEnd();
            return(true);
        }