locked
Disassociate Many to many relationship records not Working RRS feed

  • Question

  • i have opportunity entity in that  i have custom product grid it has N:N relationship with Product and other opportunityproduct grid it has 1:N relation.Now if i deleting opportunityproduct record the related record also should be disassociated in the custom product.but it is not disassociating i have code while debugging nothing error is coming.

    Below is my code could anybody references appreciated

     if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
                {
    
                    EntityReference targetEntity = (EntityReference)context.InputParameters["Target"];
    
                    EntityReference Opportunityproduct = new EntityReference(targetEntity.LogicalName, targetEntity.Id);
                    Guid oppprodid = Opportunityproduct.Id;
    
                    string fetchxml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                        <entity name='opportunityproduct'>
                        <attribute name='productid'/>
                        <attribute name='opportunityid'/>
                        <filter type='and'>
                        <condition attribute='opportunityproductid' operator='eq' value='" + oppprodid + @"'/>
                        </filter>
                        </entity>
                        </fetch>";
                    EntityCollection collRecords = service.RetrieveMultiple(new FetchExpression(fetchxml));
    
                    foreach (Entity oppproduct in collRecords.Entities)
                    {
    
                        EntityReference products = (EntityReference)oppproduct.Attributes["productid"];
                        Guid prodid = products.Id;
    
                        EntityReference opportunties = (EntityReference)oppproduct.Attributes["opportunityid"];
                        Guid oppid = opportunties.Id;
    
                        //DisassociateEntitiesRequest disreq = new DisassociateEntitiesRequest();
                        //disreq.Moniker1 = new EntityReference(oppproduct.LogicalName,oppproduct.Id);
                        //disreq.Moniker2 = new EntityReference(products.LogicalName,prodid);
                        //disreq.RelationshipName = "shimadzu_opportunity_product";
                        //DisassociateEntitiesResponse response = (DisassociateEntitiesResponse)service.Execute(disreq);
    
    
                        DisassociateRequest dreq = new DisassociateRequest();
    
                        //Target is the entity that you are disassociating your entities with.
                        dreq.Target = new EntityReference(opportunties.LogicalName, oppid);
    
                        //RelatedEntities are the entities you are disassociating to your target (can be more than 1)
                        dreq.RelatedEntities = new EntityReferenceCollection();
                        dreq.RelatedEntities.Add(new EntityReference(products.LogicalName, prodid));
    
                        //The relationship schema name in CRM you are using to disassociate the entities. 
                        //found in settings - customization - entity - relationships
                        dreq.Relationship = new Relationship("shimadzu_opportunity_product");
                        DisassociateResponse disresponse = (DisassociateResponse)service.Execute(dreq);
                        //execute the request
                        service.Execute(dreq);
    
    Any references plz...


    hsk srinivas

    Tuesday, July 1, 2014 6:12 AM

All replies

  • Hi Srinivas,

    Use the following code for reference and let us know if you have any issues..

     public static void DisassociateContactsToAccount(EntityReference contact, EntityReference account, IOrganizationService service)
            {
    
                // Creating EntityReferenceCollection for the Contact
                EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
                // Add the related entity contact
                relatedEntities.Add(contact);
                // Add the Account Contact relationship schema name
                Relationship relationship = new Relationship("contact_customer_accounts");
                // Disassociate the contact record to Account
                service.Disassociate(account.LogicalName, account.Id, relationship, relatedEntities);
            }

    --

    Thanks and Regards,

    Gopinath

    http://mscrmtechie.blogspot.com


    Monday, July 14, 2014 1:09 PM