I've got a method that will link an annotation to a sales order:
/// <summary> Links. </summary>
/// <param name="noteGuid"> Unique identifier for the note. </param>
/// <param name="salesOrderGuid"> Unique identifier for the sales order. </param>
/// <returns> A SalesOrder. </returns>
public SalesOrder Link(Guid noteGuid, Guid salesOrderGuid)
{
var associateRequest = new AssociateRequest
{
Target =
new EntityReference(
SalesOrder.EntityLogicalName,
salesOrderGuid),
RelatedEntities =
new EntityReferenceCollection
{
new EntityReference(
Annotation
.EntityLogicalName,
noteGuid)
},
Relationship = new Relationship("SalesOrder_Annotation")
};
_xrmServiceContext.Execute(associateRequest);
return GetSalesOrderByOrderGuid(salesOrderGuid);
}
I am attempting to unit test this method with the following test:
[Test]
public void Link_ExistingRecordHavingNotes_LinksItemCorrectly()
{
using (var xrmServiceContext = new XrmServiceContext(_fakeOrganizationService))
{
// Arrange
var salesOrderGuid = Guid.NewGuid();
var note1 = new Annotation { Id = Guid.NewGuid(), Subject = "this is note1" };
var salesOrder = new SalesOrder
{
Id = salesOrderGuid
};
_fakeContext.Initialize(new List<Entity> { salesOrder, note1 });
this._fakeContext.AddRelationship(
"SalesOrder_Annotation",
new XrmFakedRelationship
{
Entity2LogicalName = "annotation",
Entity2Attribute = "salesorderid",
Entity1LogicalName = "salesorder",
Entity1Attribute = "SalesOrder_Annotation.Id",
RelationshipType = XrmFakedRelationship.enmFakeRelationshipType.OneToMany
});
var sut = new SalesOrderService(xrmServiceContext);
// Act
var linkedRecord = sut.Link(note1.Id, salesOrderGuid);
var annotations = xrmServiceContext.AnnotationSet.FirstOrDefault(note => note.ObjectId.Id == salesOrderGuid);
...
I am not understanding why annotations
is null. When I link an entity to another entity using the above associate request, should it not link the 2 entities via ObjectId?
Annotations need to be created using a Create message with an explicit ObjectId property rather than with AssociateRequest.
Ex:
Then you will be able to query them.