codeulike
codeulike

Reputation: 23064

Dynamics Crm: creating Connection entities via API

So Connections in Dynamics CRM provide a general purpose way of linking things together.

Internally the Connections entity has a Record1Id attribute and a Record2Id attribute, among other things.

When you create a connection via the UI, CRM actually "creates two entries in the Connection table in the database. Each entry allows you to search for the related record from the originating record or the related record."

That is, if you connect A and B, it saves two rows to the (behind the scenes) table:

This is to make searching for connections easier. If you do an Advanced Find on connections, you only have to do the search 'one way round'.

So my question is:

When you create Connections via the API (late bound), which goes something like this:

Entity connection = new Entity("connection");
connection["record1id"] = new EntityReference("contact", someContactId);
connection["record1objecttypecode"] = new OptionSetValue(2);
connection["record1roleid"] = new EntityReference("connectionrole", someConnectionRoleId);
connection["record2id"] = new EntityReference("incident", someCaseId);
connection["record2objecttypecode"] = new OptionSetValue(122);
connection["record2roleid"] = new EntityReference("connectionrole", someOtherConnectionRoleId);
var newId = service.Create(connection);

... is it sufficient to create them 'one way round' as above, and then behind the scenes CRM will create connections in both directions?
... or do you need to manually create them in both directions? (by saving twice and swapping round the record1id record2id values, etc)

Or, in other words, does the CRM API for Connections encapsulate the 'its actually two connections behind the scenes' functionality, or do you need to manually handle that yourself?

Upvotes: 3

Views: 4622

Answers (1)

Andy Meyers
Andy Meyers

Reputation: 1581

You just need to create one connection record. One thing to note is I don't think you need to set the typecodes as you are doing above. Just setting the logical names in the entity references should be enough. Here is the sample from the SDK:

Connection newConnection = new Connection
{
    Record1Id = new EntityReference(Account.EntityLogicalName,
        _accountId),
    Record1RoleId = new EntityReference(ConnectionRole.EntityLogicalName,
        _connectionRoleId),                             
    Record2RoleId = new EntityReference(ConnectionRole.EntityLogicalName,
        _connectionRoleId),                            
    Record2Id = new EntityReference(Contact.EntityLogicalName,
        _contactId)
};
_connectionId = _serviceProxy.Create(newConnection);

Upvotes: 4

Related Questions