Showing posts with label Associated Records. Show all posts
Showing posts with label Associated Records. Show all posts

Tuesday, September 23, 2008

Delete / DisAssociate Many to Many Relationship Record.


When you say DisAssocaiating a Many to Many Relationship Record, then you usually deattach the record, you won't delete the record.

Below function will DisAssocaiate a record. Here, you need to pass three parameter values
Parent Entity Guid,
Child Entity Guid, and
Relationship Name.

public bool DisassociateManyToManyEntityRecords(Moniker Moniker1, Moniker Moniker2, string strEntityRelationshipName)
{
try
{
// Create a request.
DisassociateEntitiesRequest request = new DisassociateEntitiesRequest();

        // Assign the request a moniker for both entities that need to be disassociated.
request.Moniker1 = Moniker1;
request.Moniker2 = Moniker2;

        // Set the relationship name that associates the two entities.
request.RelationshipName = strEntityRelationshipName.Trim();
        // Execute the request.
DisassociateEntitiesResponse response = (DisassociateEntitiesResponse)Service.Execute(request);

        return true;
}

    catch (SoapException ex)
{
return false;
}
}

Retrieve Associated Many to Many Relationship Records.


In our last post we have seen "Creating Records for Many to Many Relationship in MS CRM".

Now how do you retrieve the (many to many) records ?

Its Very Easy .... :) I'm serious.... See below function........

Lets assume, we have "Hotel Reservation" and "Guest" Entity. We have many to many relationship between Guest and HotelReservation Entity.

Below function will return the list of Hotel Reservations (Guids) for the Guest.



public List GetAllAssocaitedRecords(Guid GuestGuid) 
{ 
    string strEntityRelationshipName = "Specify Relationship Name";
 
    List ObjHotelReservationGuids = new List();
 
    string fetchXml = @"<fetch mapping=""logical""> <entity name=""@HotelReservationContactRelation"">
                            <all-attributes />
                            <filter>
                                <condition attribute=""contactid"" operator=""eq"" value =""@GuestGuid"" />
                            </filter>
                            </entity>
                        </fetch>";
 
 
    fetchXml = fetchXml.Replace("@GuestGuid", GuestGuid.ToString());
    fetchXml = fetchXml.Replace("@HotelReservationContactRelation", strEntityRelationshipName );
 
    string strResult = Service.Fetch(fetchXml);
 
    XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(strResult);
    XmlNodeList nodeList = xmlDoc.SelectNodes("resultset/result");
 
    if (nodeList.Count > 0)
    {
        foreach (XmlNode xmlNode in nodeList)
        {
            ObjHotelReservationGuids.Add(new Guid(xmlNode.SelectSingleNode("neuguest_hotelreservationid").InnerText));
        }
    }
    
    return ObjHotelReservationGuids;
}
 


Happy Coding !!!!!


Thursday, September 11, 2008

Creating Records for Many to Many Relationship in MS CRM


Have you ever created Many to Many Relationship in MS CRM 4.0 ?

Use AssociateEntitiesRequest Class (CrmService)
Contains the data needed to add a link between two entity instances in a many-to-many relationship.
When you create Many to Many Relationship between two entities (system / Custom), MS CRM 4.0 creates New Table in MS CRM database. The name would be the "Relationship name" which you specified while creating Many to Many relationship.

Now, how do u associate a record to Entities?

Let's say, You have One "Entity: Contact" and other "Entity: New_CustomEntity".


Step 1: Create the record for first "Entity: Contact" (you will get a GUID for this record)

Step 2: Create the record for Second "Entity: New_CustomEntity" (you will get a GUID for this record)

Step 3: When you create Many to Many Relationship between two entities, you will specify "Relationship name" Eg: Contact_New_CustomEntity_ManyToMany

Step 4: Call below method and pass both the Entity Information with Relationship name. For more info see below example.


// Code Create Moniker for first Entity: Contact
Moniker Moniker1 = new Moniker();
Moniker1.Id = new Guid("SPECIFY GUID For the record");
Moniker1.Name = EntityName.contact.ToString();
 
// Code Create Moniker for second Entity: New_CustomEntity
Moniker Moniker2 = new Moniker();
Moniker2.Id = new Guid("SPECIFY GUID For the record");
Moniker2.Name = EntityName.New_CustomEntity.ToString();
 
string strManyToManyRelationshipName = "Contact_New_CustomEntity_ManyToMany"; 
 
if (ObjCrmHelper.AssociateManyToManyEntityRecords(Moniker1, Moniker2, strManyToManyRelationshipName))
MessageBox.Show("Associated Entities Record.");
else
MessageBox.Show("Error Occoured.");


//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 = Moniker1;

        // Set the ID of Moniker2 to the ID of the contact.
request.Moniker2 = Moniker2;

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

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

        return true;
}

    catch (SoapException ex)
{
return false;
}
}

/******************/
Happy Coding !!!!!!