Tuesday, July 12, 2011

CRM 2011 - Create and Delete Many To Many Relationship Records Using C#

Create >>>>>
            // Code Create Moniker for first Entity: Contact
                    Moniker Moniker1 = new Moniker ( );
                    Moniker1.Id = contactId;
                    Moniker1.Name = "contact";//Entity Name



                    // Code Create Moniker for second Entity: New_CustomEntity
                    Moniker Moniker2 = new Moniker ( );
                    Moniker2.Id = contactTypeId;
                    Moniker2.Name = "mship_contacttype";//Entity Name


         //Many to MAny Relationship Entity Name
                    string strManyToManyRelationshipName = "fia_mship_contacttype_contact";
//Call
AssociateManyToManyEntityRecords ( Moniker1, Moniker2, strManyToManyRelationshipName );

//Method which will associate the records.
        public bool AssociateManyToManyEntityRecords ( Moniker moniker1, Moniker moniker2, string strEntityRelationshipName )
        {
            try
            {
                // Create an AssociateEntities request.
                AssociateEntitiesRequest request = new AssociateEntitiesRequest ( );


                // Set the ID of Moniker1 to the ID of the lead.
                request.Moniker1 = new EntityReference { Id = moniker1.Id, LogicalName = moniker1.Name };


                // Set the ID of Moniker2 to the ID of the contact.
                request.Moniker2 = new EntityReference { Id = moniker2.Id, LogicalName = moniker2.Name };


                // Set the relationship name to associate on.
                request.RelationshipName = strEntityRelationshipName;


                // Execute the request.
                this.Service.Execute ( request );


                return true;
            }


            catch ( SoapException ex )
            {
                return false;
            }

        }


Delete >>>>>

//Method which will associate the records.
        public bool DeAssociateManyToManyEntityRecords ( Moniker moniker1, Moniker moniker2, string strEntityRelationshipName )
        {
            try
            {
                // Create an AssociateEntities request.
                DisassociateEntitiesRequest  request = new DisassociateEntitiesRequest ( );


                // Set the ID of Moniker1 to the ID of the lead.
                request.Moniker1 = new EntityReference { Id = moniker1.Id, LogicalName = moniker1.Name };


                // Set the ID of Moniker2 to the ID of the contact.
                request.Moniker2 = new EntityReference { Id = moniker2.Id, LogicalName = moniker2.Name };


                // Set the relationship name to associate on.
                request.RelationshipName = strEntityRelationshipName;


                // Execute the request.
                this.Service.Execute ( request );


                return true;
            }


            catch ( SoapException ex )
            {
                return false;
            }

        }
 

No comments:

Post a Comment