Quantcast
Channel: CRM Development forum
Viewing all articles
Browse latest Browse all 9244

Deactivate custom entity on deactivation of Connection

$
0
0

Okay, I've done some research, but I'm kind of stumped as to why it's not working as I expect. Let me explain the issue, my thoughts on it, and hopefully someone can tell me what I might be missing. (I can post the code, but it's a little long in some cases, and really I just need help figuring out the logic of why it works one way, but not the other).

I have a plugin that creates a custom entity on the creation of a specific type of connection. This plugin will work regardless of if the connection is created at the contact level of the account level. (My understanding is this works because the paired connection is always created when one is done.)

The problem is when I deactivate the connection, I need it to deactivate the custom entity as well.

It works fine if the Connection is deactivated from the Account level, but it won't deactivate it when I deactivate the connection from the contact level.

This seems strange to me, because in the creation code I have it so that it only creates the custom entity when the record1roleid is created. (so as to keep from getting duplicate custom entities).

the record1roleid is only populated on the contact connection (verified this via SQL). So I'm baffled as to why it won't deactivate the custom entity when I deactivate it from the contact level. (using the same limits, only when record1roleid is populated).

if it is working when I deactivate the connection from the Account level, why isn't it working when I deactivate it from the contact level.

I am using StStateDynamicEntity as the trigger, and having it do a QueryExpression to find the related role.

My understanding is that when I deactivate the connection from the Account level, it deactivates the connection at the contact level automatically(it does), which is what triggers the plugin.

but what logic would prevent it from deactivating the custom entity when I deactivate the connection from the Contact record level?

if any more information is needed to understand my question, please let me know.

 if (postImageEntity.Attributes.Contains("record1roleid"))
            {
                EntityReference accountid = (EntityReference)(postImageEntity.Attributes["record1id"]);
                EntityReference contactid = (EntityReference)(postImageEntity.Attributes["record2id"]);
                EntityReference roleid = (EntityReference)(postImageEntity.Attributes["record1roleid"]);

                EntityReference cwrid = new EntityReference("pa_contactwebroles", new Guid("270C014B-52B9-E111-817D-00505683000D"));


                Entity pa_contactwebroles = new Entity("pa_contactwebroles");

                //Capturing record  State & Status
                OptionSetValue state = (OptionSetValue)context.InputParameters["State"];
                OptionSetValue status = (OptionSetValue)context.InputParameters["Status"];


                #region Deactivate related Contact Web Role

                if (roleid.Id == new Guid("1780F6B1-5BFC-E111-B820-005056BF0011"))
                {
                    if (state.Value == 1)
                    {

                        /// querry expression to retrieve related Content Web Roles for Contact.
                        QueryExpression contactwebroleconnection = new QueryExpression { EntityName = "pa_contactwebroles", ColumnSet = new ColumnSet("pa_contactwebrolesid", "pa_contactid", "pa_contactroleid") };
                        contactwebroleconnection.Criteria.AddCondition("pa_contactid", ConditionOperator.Equal, contactid.Id);
                        contactwebroleconnection.Criteria.AddCondition("pa_contactroleid", ConditionOperator.Equal, cwrid.Id);
                        contactwebroleconnection.Criteria.AddCondition("statuscode", ConditionOperator.Equal, 1);

                        // Check weather statecode of the record are Active
                        contactwebroleconnection.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
                        EntityCollection RetrievecontactCWR = service.RetrieveMultiple(contactwebroleconnection);


                        if (RetrievecontactCWR.Entities.Count > 0)
                        {


                            foreach (var a in RetrievecontactCWR.Entities)
                            {
                                SetStateRequest contactsetStateReq = new SetStateRequest();
                                contactsetStateReq.EntityMoniker = new EntityReference(a.LogicalName, new Guid(a.Id.ToString()));
                                contactsetStateReq.State = new OptionSetValue(1);
                                contactsetStateReq.Status = new OptionSetValue(-1);
                                service.Execute(contactsetStateReq);
                            }
                        }
                    }
                }

                #endregion


Daniel



Viewing all articles
Browse latest Browse all 9244

Trending Articles