This project is read-only.
1

Closed

Disassociate N:N of the same entity

description

I am trying to create a plugin which will disassociate records (in my case the N:N relationship is Account to Account), after an update of a Two Option Field on one of the associated records.
I have assigned the plugin to trigger on Post Update and Asynchronous.
I'll mark out that one side of the Disassociate Action is #Id and the other #p1[accountid] (account) which comes from a Request Attribute (Fetch XML) with an aggregation of ForEach .

However all of this doesn't do the job !!
(I have created in the past a Disassociate triggered from Pre Delete which works fine)
Does this have to do with Update trigger ?? I'm puzzled :(

Avi.

file attachments

Closed Feb 9, 2015 at 10:43 AM by aminekouddane

comments

aminekouddane wrote Jan 29, 2015 at 10:08 PM

Can you post your configuration
I'll try to figure out what's wrong.

-Amine

avsrot wrote Jan 31, 2015 at 7:51 PM

Here is my config:

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfXportObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<XportObject Name="Disassociate Distributers and Manufacturers (imported)" Active="false" Entity="account" PreCreate="false" PostCreate="false" PreUpdate="false" PostUpdate="true" PreDelete="false" PostDelete="false" Synchronous="false" Order="1" TriggerType="0" IsPeriodic="false" Period="0" PeriodUnit="0" StartDate="0001-01-01T02:00:00+02:00">
<Rules>&lt;?xml version="1.0" encoding="utf-16"?&gt;
<ArrayOfCrmRule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CrmRule ParamNumber="1" Type="Request" AttributeState="After" AttributesReturnTypes="String String Number Guid" ReturnCount="4" ReturnType="Array" LogicalName="" FetchXml="&lt;fetch version=&quot;1.0&quot; output-format=&quot;xml-platform&quot; mapping=&quot;logical&quot; distinct=&quot;true&quot;&gt;&#xD; &lt;entity name=&quot;account&quot;&gt;&#xD; &lt;attribute name=&quot;new_foreigname&quot; /&gt;&#xD; &lt;attribute name=&quot;new_hebname&quot; /&gt;&#xD; &lt;attribute name=&quot;new_code&quot; /&gt;&#xD; &lt;attribute name=&quot;accountid&quot; /&gt;&#xD; &lt;order attribute=&quot;new_foreigname&quot; descending=&quot;false&quot; /&gt;&#xD; &lt;link-entity name=&quot;new_products_distributers_accounts&quot; from=&quot;accountidtwo&quot; to=&quot;accountid&quot; visible=&quot;false&quot; intersect=&quot;true&quot;&gt;&#xD; &lt;link-entity name=&quot;account&quot; from=&quot;accountid&quot; to=&quot;accountidone&quot; alias=&quot;ad&quot;&gt;&#xD; &lt;filter type=&quot;and&quot;&gt;&#xD; &lt;condition attribute=&quot;accountid&quot; operator=&quot;eq&quot; value=&quot;{#Id}&quot; /&gt;&#xD; &lt;/filter&gt;&#xD; &lt;/link-entity&gt;&#xD; &lt;/link-entity&gt;&#xD; &lt;/entity&gt;&#xD;&lt;/fetch&gt;" LineFunction="ForEach" LineSeparator="" ColumnSeparator="" />
<CrmRule ParamNumber="6" Type="Attribute" AttributeState="After" ReturnCount="1" ReturnType="Boolean" LogicalName="new_disassociateflag" FetchXml="" LineFunction="None" LineSeparator="" ColumnSeparator="" />
</ArrayOfCrmRule></Rules>
<Actions>&lt;?xml version="1.0" encoding="utf-16"?&gt;
<ArrayOfCrmAction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CrmAction Type="Disassociate" MaxDepth="5" ForEachParamNumber="1" EntityLogicalName="account" RelationshipLogicalName="new_products_distributers_accounts" Order="0">
&lt;Expressions&gt;
  &lt;FieldExpression AttributeLogicalName="account" Type="Lookup" Expression="#Id" AttributeTypeCode="Lookup" Targets="" /&gt;
  &lt;FieldExpression AttributeLogicalName="account" Type="Lookup" Expression="#p1[3]" AttributeTypeCode="Lookup" Targets="" /&gt;
&lt;/Expressions&gt;
&lt;ExpressionId AttributeLogicalName="" Type="None" Expression="" AttributeTypeCode="Boolean" Targets="" /&gt;
&lt;ConditionExpression AttributeLogicalName="" Type="Boolean" Expression="#p6==true" AttributeTypeCode="Boolean" Targets="" /&gt;
</CrmAction>
</ArrayOfCrmAction></Actions>
</XportObject>
</ArrayOfXportObject>


Thanx, Avi.

wrote Jan 31, 2015 at 7:52 PM

wrote Jan 31, 2015 at 7:53 PM

wrote Jan 31, 2015 at 8:01 PM

avsrot wrote Jan 31, 2015 at 8:01 PM

Here it is attached

avsrot wrote Jan 31, 2015 at 8:13 PM

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfXportObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <XportObject Name="Disassociate Distributers and Manufacturers (imported)" Active="false" Entity="account" PreCreate="false" PostCreate="false" PreUpdate="false" PostUpdate="true" PreDelete="false" PostDelete="false" Synchronous="false" Order="1" TriggerType="0" IsPeriodic="false" Period="0" PeriodUnit="0" StartDate="0001-01-01T02:00:00+02:00">
    <Rules>&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;ArrayOfCrmRule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  &lt;CrmRule ParamNumber="1" Type="Request" AttributeState="After" AttributesReturnTypes="String String Number Guid" ReturnCount="4" ReturnType="Array" LogicalName="" FetchXml="&amp;lt;fetch version=&amp;quot;1.0&amp;quot; output-format=&amp;quot;xml-platform&amp;quot; mapping=&amp;quot;logical&amp;quot; distinct=&amp;quot;true&amp;quot;&amp;gt;&amp;#xD;    &amp;lt;entity name=&amp;quot;account&amp;quot;&amp;gt;&amp;#xD;        &amp;lt;attribute name=&amp;quot;new_foreigname&amp;quot; /&amp;gt;&amp;#xD;        &amp;lt;attribute name=&amp;quot;new_hebname&amp;quot; /&amp;gt;&amp;#xD;        &amp;lt;attribute name=&amp;quot;new_code&amp;quot; /&amp;gt;&amp;#xD;        &amp;lt;attribute name=&amp;quot;accountid&amp;quot; /&amp;gt;&amp;#xD;        &amp;lt;order attribute=&amp;quot;new_foreigname&amp;quot; descending=&amp;quot;false&amp;quot; /&amp;gt;&amp;#xD;        &amp;lt;link-entity name=&amp;quot;new_products_distributers_accounts&amp;quot; from=&amp;quot;accountidtwo&amp;quot; to=&amp;quot;accountid&amp;quot; visible=&amp;quot;false&amp;quot; intersect=&amp;quot;true&amp;quot;&amp;gt;&amp;#xD;            &amp;lt;link-entity name=&amp;quot;account&amp;quot; from=&amp;quot;accountid&amp;quot; to=&amp;quot;accountidone&amp;quot; alias=&amp;quot;ad&amp;quot;&amp;gt;&amp;#xD;                &amp;lt;filter type=&amp;quot;and&amp;quot;&amp;gt;&amp;#xD;                    &amp;lt;condition attribute=&amp;quot;accountid&amp;quot; operator=&amp;quot;eq&amp;quot; value=&amp;quot;{#Id}&amp;quot; /&amp;gt;&amp;#xD;                &amp;lt;/filter&amp;gt;&amp;#xD;            &amp;lt;/link-entity&amp;gt;&amp;#xD;        &amp;lt;/link-entity&amp;gt;&amp;#xD;    &amp;lt;/entity&amp;gt;&amp;#xD;&amp;lt;/fetch&amp;gt;" LineFunction="ForEach" LineSeparator="" ColumnSeparator="" /&gt;
  &lt;CrmRule ParamNumber="6" Type="Attribute" AttributeState="After" ReturnCount="1" ReturnType="Boolean" LogicalName="new_disassociateflag" FetchXml="" LineFunction="None" LineSeparator="" ColumnSeparator="" /&gt;
&lt;/ArrayOfCrmRule&gt;</Rules>
    <Actions>&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;ArrayOfCrmAction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  &lt;CrmAction Type="Disassociate" MaxDepth="5" ForEachParamNumber="1" EntityLogicalName="account" RelationshipLogicalName="new_products_distributers_accounts" Order="0"&gt;
    &lt;Expressions&gt;
      &lt;FieldExpression AttributeLogicalName="account" Type="Lookup" Expression="#Id" AttributeTypeCode="Lookup" Targets="" /&gt;
      &lt;FieldExpression AttributeLogicalName="account" Type="Lookup" Expression="#p1[3]" AttributeTypeCode="Lookup" Targets="" /&gt;
    &lt;/Expressions&gt;
    &lt;ExpressionId AttributeLogicalName="" Type="None" Expression="" AttributeTypeCode="Boolean" Targets="" /&gt;
    &lt;ConditionExpression AttributeLogicalName="" Type="Boolean" Expression="#p6==true" AttributeTypeCode="Boolean" Targets="" /&gt;
  &lt;/CrmAction&gt;
&lt;/ArrayOfCrmAction&gt;</Actions>
  </XportObject>
</ArrayOfXportObject>

aminekouddane wrote Feb 1, 2015 at 4:39 PM

Ok

I think I found the problem !

This is your FetchXml :
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
    <entity name="account">
        <attribute name="new_foreigname" />
        <attribute name="new_hebname" />
        <attribute name="new_code" />
        <attribute name="accountid" />
        <order attribute="new_foreigname" descending="false" />
        <link-entity name="new_products_distributers_accounts" from="accountidtwo" to="accountid" visible="false" intersect="true">
            <link-entity name="account" from="accountid" to="accountidone" alias="ad">
                <filter type="and">
                    <condition attribute="accountid" operator="eq" value="{#Id}" />
                </filter>
            </link-entity>
        </link-entity>
    </entity>
</fetch>
Your are using a dissocate action between #Id & #p1[3] but there are always the same
"accountid" attribute is not inside link-entity, it will return updated account's id (#Id)

Try to update your FetchXml by this one :
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
    <entity name="account">
        <attribute name="new_foreigname" />
        <attribute name="new_hebname" />
        <attribute name="new_code" />
        <order attribute="new_foreigname" descending="false" />
        <link-entity name="new_products_distributers_accounts" from="accountidtwo" to="accountid" visible="false" intersect="true">
            <link-entity name="account" from="accountid" to="accountidone" alias="ad">
                <attribute name="accountid" />
                <filter type="and">
                    <condition attribute="accountid" operator="eq" value="{#Id}" />
                </filter>
            </link-entity>
        </link-entity>
    </entity>
</fetch>
You can try to create a synchronous Test plugin on update accounts with an abort action to try to debug your fetchXml and see if it returns the expected results (Discussion here)

Regards,

-Amine

avsrot wrote Feb 1, 2015 at 5:55 PM

Amine,

I have changed the Fetch-XML as suggested,
added an Abort Message Action to show the results from the Fetch-XML
switched the plugin to Synchronous
and got this error message:
Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An item with the same key has already been added.Detail: 
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
  <ErrorCode>-2147220891</ErrorCode>
  <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
    <KeyValuePairOfstringanyType>
      <d2p1:key>OperationStatus</d2p1:key>
      <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value>
    </KeyValuePairOfstringanyType>
  </ErrorDetails>
  <Message>An item with the same key has already been added.</Message>
  <Timestamp>2015-02-01T17:47:07.0929297Z</Timestamp>
  <InnerFault i:nil="true" />
  <TraceText>

[EasyPlugins: EasyPlugins.SynchronousPostUpdateStep]
[9fdef62f-16f3-e211-8a5d-080027865fa2: EasyPlugins.SynchronousPostUpdateStep: Update of  any Entity]


</TraceText>
</OrganizationServiceFault>

Please see if you can help here :(

Avi.

aminekouddane wrote Feb 2, 2015 at 1:46 PM

Please try this FetchXml :
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
  <entity name="new_products_distributers_accounts">
    <attribute name="accountidone" />
    <attribute name="accountidtwo" />
    <filter type="and">
      <filter type="or">
        <filter type="and">
          <condition attribute="accountidone" operator="eq" uitype="account" value="{#Id}" />
          <condition attribute="accountidtwo" operator="not-null" />
        </filter>
        <filter type="and">
          <condition attribute="accountidone" operator="not-null" />
          <condition attribute="accountidtwo" operator="eq" uitype="account" value="{#Id}" />
        </filter>
      </filter>
    </filter>
  </entity>
</fetch>
This request is supposed to retrieve the 2 accounts to disassociate when one of them is updated

Regards,
-Amine

avsrot wrote Feb 2, 2015 at 3:45 PM

Amine,

Although now there is no error message, but actually nothing happens after the plugin is triggered.

Just for the sake of testing - I ran a test Disassociate Plugin against Contact entity with two associations (using the FetcXML you have sent me today):
  1. Contact to Account
  2. Contact to Contact
    On the first one it worked like a charm
    On the second there was no response and nothing was dissociated !
It seems that the fact that both sides of association are the same entity is stopping the plugin for some reason :(

Avi.

avsrot wrote Feb 4, 2015 at 10:43 AM

Amine,

Just a wiled thought.

Since I'm no "Coder" so I didn't look into the depths of your code, but can my issue have anything to do with this blog ??

Thanx again.
Avi.

aminekouddane wrote Feb 4, 2015 at 9:14 PM

Hi Avi,

Absolutely, are you on 2011 version ?
I tested this configuration on CRM 2013, and it worked...
I'm working on it, and will release a fix very soon ;)

Thanks,
-Amine

avsrot wrote Feb 5, 2015 at 10:54 AM

Yes, I'm on CRM 2011 On Premise.
Can't wait for the fix.......

Great thank you :)

wrote Feb 5, 2015 at 5:14 PM

aminekouddane wrote Feb 5, 2015 at 5:14 PM

Hi Avi,

The version 1.3.2.6 for Dynamics CRM 2011 is ready but it currently on test.
if you are hurried, you can use a preview version (solution is attached) :)

Regards,
-Amine

avsrot wrote Feb 6, 2015 at 6:37 AM

Amine,

I gave a try to the attached solution (1.3.2.6).
Sorry, but no difference, still nothing happens after triggering the plugin - the records are still associated !

Avi.

wrote Feb 6, 2015 at 7:19 AM

avsrot wrote Feb 6, 2015 at 7:19 AM

Hi Amine,

I think I found something!
See attached spreadsheet, associated table "new_products_distributers_accountsId" (from the SQL server) - the GUID for all records IS THE SAME !!
After testing this, it looks like some CRM bug, because this happens ONLY when both sides of association are the same(like in my scenario), no matter what entity you choose (I have tested also on Contacts).

Maybe this ties up the plugin in some way!!
Is there any solution for this?

Avi.

wrote Feb 6, 2015 at 10:43 AM

wrote Feb 6, 2015 at 11:42 AM

aminekouddane wrote Feb 6, 2015 at 11:42 AM

Hi Avi,

I just recreated your configuration on my environment.
Please try this :
  • Update your Solution with the new one Attached (UPDATED [2011] EasyPlugins_1_3_2_6_managed.zip)
  • Then try this configuration :
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfXportObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <XportObject Name="Disassociate Distributers and Manufacturers (updated)" Active="false" Entity="account" PreCreate="false" PostCreate="false" PreUpdate="false" PostUpdate="true" PreDelete="false" PostDelete="false" Synchronous="true" Order="11" TriggerType="0" IsPeriodic="false" Period="0" PeriodUnit="0" StartDate="0001-01-01T01:00:00+01:00">
    <Rules>&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;ArrayOfCrmRule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  &lt;CrmRule ParamNumber="1" Type="Request" AttributeState="After" AttributesReturnTypes="Guid Guid" ReturnCount="2" ReturnType="Array" LogicalName="" FetchXml="&amp;lt;fetch version=&amp;quot;1.0&amp;quot; output-format=&amp;quot;xml-platform&amp;quot; mapping=&amp;quot;logical&amp;quot; distinct=&amp;quot;false&amp;quot;&amp;gt;&amp;#xD;  &amp;lt;entity name=&amp;quot;new_products_distributers_accounts&amp;quot;&amp;gt;&amp;#xD;    &amp;lt;attribute name=&amp;quot;accountidone&amp;quot; /&amp;gt;&amp;#xD;    &amp;lt;attribute name=&amp;quot;accountidtwo&amp;quot; /&amp;gt;&amp;#xD;    &amp;lt;filter type=&amp;quot;and&amp;quot;&amp;gt;&amp;#xD;      &amp;lt;filter type=&amp;quot;or&amp;quot;&amp;gt;&amp;#xD;        &amp;lt;filter type=&amp;quot;and&amp;quot;&amp;gt;&amp;#xD;          &amp;lt;condition attribute=&amp;quot;accountidone&amp;quot; operator=&amp;quot;eq&amp;quot; uitype=&amp;quot;account&amp;quot; value=&amp;quot;{#Id}&amp;quot; /&amp;gt;&amp;#xD;          &amp;lt;condition attribute=&amp;quot;accountidtwo&amp;quot; operator=&amp;quot;not-null&amp;quot; /&amp;gt;&amp;#xD;        &amp;lt;/filter&amp;gt;&amp;#xD;        &amp;lt;filter type=&amp;quot;and&amp;quot;&amp;gt;&amp;#xD;          &amp;lt;condition attribute=&amp;quot;accountidone&amp;quot; operator=&amp;quot;not-null&amp;quot; /&amp;gt;&amp;#xD;          &amp;lt;condition attribute=&amp;quot;accountidtwo&amp;quot; operator=&amp;quot;eq&amp;quot; uitype=&amp;quot;account&amp;quot; value=&amp;quot;{#Id}&amp;quot; /&amp;gt;&amp;#xD;        &amp;lt;/filter&amp;gt;&amp;#xD;      &amp;lt;/filter&amp;gt;&amp;#xD;    &amp;lt;/filter&amp;gt;&amp;#xD;  &amp;lt;/entity&amp;gt;&amp;#xD;&amp;lt;/fetch&amp;gt;" LineFunction="ForEach" LineSeparator="" ColumnSeparator="" /&gt;
  &lt;CrmRule ParamNumber="6" Type="Attribute" AttributeState="After" ReturnCount="1" ReturnType="Boolean" LogicalName="new_disassociateflag" FetchXml="" LineFunction="None" LineSeparator="" ColumnSeparator="" /&gt;
&lt;/ArrayOfCrmRule&gt;</Rules>
    <Actions>&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;ArrayOfCrmAction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  &lt;CrmAction Type="Disassociate" MaxDepth="1" ForEachParamNumber="1" EntityLogicalName="account" RelationshipLogicalName="new_products_distributers_accounts" Order="0"&gt;
    &lt;Expressions&gt;
      &lt;FieldExpression AttributeLogicalName="account" Type="Lookup" Expression="#p1[0]" AttributeTypeCode="Lookup" Targets="" /&gt;
      &lt;FieldExpression AttributeLogicalName="account" Type="Lookup" Expression="#p1[1]" AttributeTypeCode="Lookup" Targets="" /&gt;
    &lt;/Expressions&gt;
    &lt;ExpressionId AttributeLogicalName="" Type="None" Expression="" AttributeTypeCode="Boolean" Targets="" /&gt;
    &lt;ConditionExpression AttributeLogicalName="" Type="Boolean" Expression="true" AttributeTypeCode="Boolean" Targets="" /&gt;
  &lt;/CrmAction&gt;
&lt;/ArrayOfCrmAction&gt;</Actions>
  </XportObject>
</ArrayOfXportObject>

avsrot wrote Feb 7, 2015 at 11:40 PM

Hi Amine,

Finally it all works like a charm :) :)
Thanx a lot for your patience and great response.

Have a great Weekend (you certainly deserve it!)

Avi.

wrote Feb 8, 2015 at 10:36 AM

aminekouddane wrote Feb 9, 2015 at 10:43 AM

Hi Avi,

Great ! Thank you

-Amine

wrote Feb 9, 2015 at 10:43 AM