private Entity ReturnEmailData(Guid recordId)
        {
            var queryEmail = new QueryExpression(Email.EntityLogicalName);

            queryEmail.TopCount = 1;

            // Add columns for Email entity
            queryEmail.ColumnSet.AddColumns(Email.Fields.Description, Email.Fields.Subject, Email.Fields.ActivityId, Email.Fields.StatusCode, Email.Fields.RegardingObjectId, Email.Fields.DirectionCode, Email.Fields.Sender, Email.Fields.ToRecipients, Email.Fields.CreatedOn, Email.Fields.defra_uploadedtosharepoint);

            // Define filter
            queryEmail.Criteria.AddCondition(Email.Fields.ActivityId, ConditionOperator.Equal, recordId);

            // Add Application link-entity and define an alias.
            // Its an outer join as we want to return results even if not regarding an application
            var queryEmailApplication = queryEmail.AddLink(Application.EntityLogicalName, Email.Fields.RegardingObjectId, Application.ApplicationId, JoinOperator.LeftOuter);

            queryEmailApplication.EntityAlias = "application";

            // Add columns for Application link entity
            queryEmailApplication.Columns.AddColumns(Application.ApplicationNumber, Application.Name, Application.PermitNumber);

            // Add link-entity to Case and define an alias.
            // Its an outer join as we want to return results even if not regarding a case
            var queryEmailIncident = queryEmail.AddLink(Case.EntityLogicalName, Case.RegardingObjectId, Case.IncidentId, JoinOperator.LeftOuter);

            queryEmailIncident.EntityAlias = "case";

            // Add columns for Case link entity
            queryEmailIncident.Columns.AddColumns(Case.CaseType, Case.Title, Case.IncidentId, Case.TicketNumber);

            // Add link-entity from Case to Application and define an alias
            var queryEmailIncidentApplication = queryEmailIncident.AddLink(Application.EntityLogicalName, Case.Application, Application.ApplicationId, JoinOperator.LeftOuter);

            queryEmailIncidentApplication.EntityAlias = "case.application";

            // Add columns to for Application entity regarding the Case
            queryEmailIncidentApplication.Columns.AddColumns(Application.ApplicationNumber, Application.Name, Application.PermitNumber, defra_application.Fields.defra_applicationId);

            var results = Service.RetrieveMultiple(queryEmail);

            if (results != null && results.Entities.Count >= 1)
            {
                CrmExtensions.TraceEntity(TracingService, results.Entities[0]);
                return(results.Entities[0]);
            }

            return(null);
        }
        private Entity ReturnAttachmentData(Guid recordId)
        {
            // Instantiate QueryExpression QEactivitymimeattachment
            var queryActivityMimeAttachment = new QueryExpression(ActivityMimeAttachment.EntityLogicalName);

            queryActivityMimeAttachment.TopCount = 1;

            // Add columns to ActivityMimeAttachment Entity
            queryActivityMimeAttachment.ColumnSet.AddColumns(ActivityMimeAttachment.Filename, ActivityMimeAttachment.Body, ActivityMimeAttachment.Id);

            // Define filter on Primary Key
            queryActivityMimeAttachment.Criteria.AddCondition(ActivityMimeAttachment.Id, ConditionOperator.Equal, recordId);

            // Add link-entity QEactivitymimeattachment_email. Inner join as it must be regarding an Email
            var queryActivityMimeAttachmentEmail = queryActivityMimeAttachment.AddLink(Email.EntityLogicalName, ActivityMimeAttachment.ObjectId, Email.Fields.ActivityId);

            queryActivityMimeAttachmentEmail.EntityAlias = "email";

            // Add columns to QEactivitymimeattachment_email.Columns
            queryActivityMimeAttachmentEmail.Columns.AddColumns(
                Email.Fields.Description,
                Email.Fields.Subject,
                Email.Fields.DirectionCode,
                Email.Fields.ActivityId,
                Email.Fields.StatusCode,
                Email.Fields.RegardingObjectId,
                Email.Fields.Sender,
                Email.Fields.ToRecipients,
                Email.Fields.CreatedOn,
                Email.Fields.OwningUser,
                Email.Fields.OwningTeam);

            // Add Application link-entity and define an alias.
            // Its an outer join as we want to return results even if not regarding an application
            var queryActivityMimeAttachmentEmailApplication = queryActivityMimeAttachmentEmail.AddLink(
                Application.EntityLogicalName,
                Email.Fields.RegardingObjectId,
                Application.ApplicationId,
                JoinOperator.LeftOuter);

            queryActivityMimeAttachmentEmailApplication.EntityAlias = "application";

            // Add columns to Application Entity
            queryActivityMimeAttachmentEmailApplication.Columns.AddColumns(Application.Name, Application.PermitNumber, Application.ApplicationNumber, defra_application.Fields.defra_applicationId);

            // Add Application link-entity and define an alias.
            // Its an outer join as we want to return results even if not regarding an case
            var queryActivityMimeAttachmentIncident = queryActivityMimeAttachmentEmail.AddLink(Case.EntityLogicalName,
                                                                                               Email.Fields.RegardingObjectId, Case.IncidentId,
                                                                                               JoinOperator.LeftOuter);

            queryActivityMimeAttachmentIncident.EntityAlias = "case";

            // Add columns to Case Entity
            queryActivityMimeAttachmentIncident.Columns.AddColumns(Case.Title, Case.IncidentId, Case.CaseType, Case.TicketNumber, Incident.Fields.IncidentId);

            // Add link-entity to Application Entity from Case and define an alias
            var queryActivityMimeAttachmentEmailApplicationIncident = queryActivityMimeAttachmentIncident.AddLink(
                Application.EntityLogicalName,
                Case.Application,
                Application.ApplicationId,
                JoinOperator.LeftOuter);

            queryActivityMimeAttachmentEmailApplicationIncident.EntityAlias = "case.application";

            // Add columns to Application Entity that we got via Case
            queryActivityMimeAttachmentEmailApplicationIncident.Columns.AddColumns(Application.ApplicationNumber, Application.Name, Application.PermitNumber, defra_application.Fields.defra_applicationId);

            var results = Service.RetrieveMultiple(queryActivityMimeAttachment);

            if (results != null && results.Entities.Count >= 1)
            {
                CrmExtensions.TraceEntity(TracingService, results.Entities[0]);
                return(results.Entities[0]);
            }

            return(null);
        }
        private Entity ReturnAnnotationData(Guid recordId)
        {
            // Instantiate QueryExpression QEannotation
            var queryAnnotation = new QueryExpression(Annotation.EntityLogicalName);

            queryAnnotation.TopCount = 1;

            // Add columns to annotation entity
            queryAnnotation.ColumnSet.AddColumns(
                Annotation.Fields.Subject,
                Annotation.Fields.DocumentBody,
                Annotation.Fields.FileName,
                Annotation.Fields.Id,
                Annotation.Fields.NoteText,
                Annotation.Fields.FileSize,
                Annotation.Fields.IsDocument,
                Annotation.Fields.CreatedOn,
                Annotation.Fields.CreatedBy);

            // Define filter on Primary key
            queryAnnotation.Criteria.AddCondition(Annotation.Fields.Id, ConditionOperator.Equal, recordId);

            // Add link-entity to defra_application. Outer join as it might be regarding a case or application
            var queryExpressionAnnotationApp = queryAnnotation.AddLink(Application.EntityLogicalName, Annotation.Fields.ObjectId, Application.ApplicationId, JoinOperator.LeftOuter);

            queryExpressionAnnotationApp.EntityAlias = "application";

            // Add columns to Application entity
            queryExpressionAnnotationApp.Columns.AddColumns(
                defra_application.Fields.defra_applicationId,
                defra_application.Fields.defra_name,
                defra_application.Fields.defra_permitnumber,
                defra_application.Fields.defra_applicationnumber,
                defra_application.Fields.StatusCode,
                defra_application.Fields.OwningUser,
                defra_application.Fields.OwningTeam);

            // Add link-entity to Case. Outer join as it might be regarding case or application
            var queryExpressionAnnotationIncident = queryAnnotation.AddLink(Incident.EntityLogicalName, Annotation.Fields.ObjectId, Incident.Fields.IncidentId, JoinOperator.LeftOuter);

            queryExpressionAnnotationIncident.EntityAlias = "case";

            // Add columns to Case entity
            queryExpressionAnnotationIncident.Columns.AddColumns(
                Incident.Fields.Title,
                Incident.Fields.IncidentId,
                Incident.Fields.CaseTypeCode,
                Incident.Fields.TicketNumber,
                Incident.Fields.IncidentId,
                Incident.Fields.OwningUser,
                Incident.Fields.OwningTeam);

            // Add link-entity from case to application
            var queryExpressionAnnotationIncidentApplication = queryExpressionAnnotationIncident.AddLink(defra_application.EntityLogicalName, Case.Application, defra_application.Fields.defra_applicationId, JoinOperator.LeftOuter);

            queryExpressionAnnotationIncidentApplication.EntityAlias = "case.application";

            // Add columns to QEannotation_incident_defra_application.Columns
            queryExpressionAnnotationIncidentApplication.Columns.AddColumns(defra_application.Fields.defra_applicationnumber, defra_application.Fields.defra_name, defra_application.Fields.defra_permitnumber, defra_application.Fields.defra_applicationId);

            var results = Service.RetrieveMultiple(queryAnnotation);

            if (results != null && results.Entities.Count >= 1)
            {
                CrmExtensions.TraceEntity(TracingService, results.Entities[0]);
                return(results.Entities[0]);
            }

            return(null);
        }