Untitled

mail@pastecode.io avatar
unknown
plain_text
11 days ago
6.1 kB
2
Indexable
Never
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TRIAL1.PLUGIN.VISITREPORT
{
    public class Utility
    {
        public static void RetrieveContactsFromParticipants(IOrganizationService service, Entity visitReport, Entity appointment, string participantsField)
        {
            if (appointment.Attributes.Contains(participantsField))
            {
                EntityCollection participants = (EntityCollection)appointment[participantsField];

                foreach (Entity participant in participants.Entities)
                {
                    if (participant.Contains("partyid") && participant["partyid"] is EntityReference)
                    {
                        EntityReference partyIdRef = (EntityReference)participant["partyid"];

                        Entity contact = null;
                        if (partyIdRef.LogicalName == "contact")
                        {
                            contact = service.Retrieve(partyIdRef.LogicalName, partyIdRef.Id, new ColumnSet("firstname", "contactid"));
                        }
                        else if (partyIdRef.LogicalName == "account")
                        {
                            contact = RetrievePrimaryContactFromAccount(service, partyIdRef.Id);

                        }
                        else if (partyIdRef.LogicalName == "systemuser")
                        {
                            contact = RetrieveContactFromSystemUser(service, partyIdRef.Id);

                        }
                        if (contact != null)
                        {
                            AddContactToVisitReportSubgrid(service, visitReport, contact);
                        }
                    }
                }
            }
        }

        public static Entity RetrievePrimaryContactFromAccount(IOrganizationService service, Guid accountId)
        {
            QueryExpression query = new QueryExpression("contact");
            query.ColumnSet = new ColumnSet("fullname", "contactid");
            query.AddLink("account", "contactid", "primarycontactid", JoinOperator.Inner);
            query.LinkEntities[0].LinkCriteria.AddCondition("accountid", ConditionOperator.Equal, accountId);

            EntityCollection results = service.RetrieveMultiple(query);

            return (results.Entities.Count > 0) ? results.Entities[0] : null;
        }

        public static Entity RetrieveContactFromSystemUser(IOrganizationService service, Guid systemUserId)
        {
            QueryExpression query = new QueryExpression("contact");
            query.ColumnSet = new ColumnSet("firstname", "contactid");
            query.AddLink("systemuser", "contactid", "yod_relatedcontact", JoinOperator.Inner);
            query.LinkEntities[0].LinkCriteria.AddCondition("systemuserid", ConditionOperator.Equal, systemUserId);

            EntityCollection results = service.RetrieveMultiple(query);
            return (results.Entities.Count > 0) ? results.Entities[0] : null;
        }

        public static void AddContactToVisitReportSubgrid(IOrganizationService service, Entity visitReport, Entity contact)
        {
            EntityReference contactRef = contact.ToEntityReference();
            service.Associate(visitReport.LogicalName, visitReport.Id, new Relationship("yod_visitreport_yod_visitreport_Contact"), new EntityReferenceCollection { contactRef });
        }

    }

}



using Microsoft.VisualStudio.TestTools.UnitTesting;
using TRIAL1.PLUGIN.VISITREPORT;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Moq;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;


namespace TRIAL1.PLUGIN.VISITREPORT
{
    [TestClass()]
    public class UtilityTests
    {
        [TestMethod]
        public void RetrieveContactFromSystemUser_ShouldReturnContactForValidSystemUserId()
        {
            // Arrange
            Guid validSystemUserId = Guid.NewGuid();
            var serviceMock = new Mock<IOrganizationService>();

            // Setup a mock response for the RetrieveMultiple method
            var contactEntity = new Entity("contact");
            contactEntity.Attributes["fullname"] = "John Doe";
            contactEntity.Attributes["contactid"] = Guid.NewGuid();
            var entityCollection = new EntityCollection(new List<Entity> { contactEntity });

            serviceMock.Setup(service => service.RetrieveMultiple(It.IsAny<QueryExpression>()))
                       .Returns(entityCollection);

            var myDataService = new Utility(); // Create an instance of your service

            // Act
            var result = myDataService.RetrieveContactFromSystemUser(serviceMock.Object, validSystemUserId);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual("John Doe", result.GetAttributeValue<string>("fullname"));
            // Add more assertions based on your specific requirements and expected data
        }

        [TestMethod]
        public void RetrieveContactFromSystemUser_ShouldReturnNullForInvalidSystemUserId()
        {
            // Arrange
            Guid invalidSystemUserId = Guid.NewGuid();
            var serviceMock = new Mock<IOrganizationService>();

            // Setup a mock response for the RetrieveMultiple method (empty collection)
            serviceMock.Setup(service => service.RetrieveMultiple(It.IsAny<QueryExpression>()))
                       .Returns(new EntityCollection());

            var myDataService = new Utility(); // Create an instance of your service

            // Act
            var result = myDataService.RetrieveContactFromSystemUser(serviceMock.Object, invalidSystemUserId);

            // Assert
            Assert.IsNull(result);
        }
    }
}
Leave a Comment