/// <summary>
        /// Creates an answer record with the given parameters
        /// </summary>
        /// <param name="applicationId">Application to link the answer to</param>
        /// <param name="appQuestionAndLine">Details of the question and line if applicable</param>
        public ApplicationAnswer CreateAnswerRecord(Guid applicationId, ApplicationQuestionsAndLines appQuestionAndLine)
        {
            ApplicationAnswer newAnswer = new ApplicationAnswer
            {
                ApplicationQuestionId = appQuestionAndLine.ApplicationQuestionId,
                ApplicationLineId     = appQuestionAndLine.ApplicationLineId
            };

            // Application answer does not exist, create it
            Entity newAnswerEntity = new Entity(defra_applicationanswer.EntityLogicalName);

            newAnswerEntity.Attributes.Add(defra_applicationanswer.Fields.defra_application,
                                           new EntityReference(defra_application.EntityLogicalName, applicationId));

            if (appQuestionAndLine.ApplicationQuestionId.HasValue)
            {
                newAnswerEntity.Attributes.Add(defra_applicationanswer.Fields.defra_question,
                                               new EntityReference(defra_applicationquestion.EntityLogicalName,
                                                                   appQuestionAndLine.ApplicationQuestionId.Value));
            }

            // If Question is at the Item level, link the answer to the corresponding application line
            if (appQuestionAndLine.ApplicationLineId.HasValue &&
                appQuestionAndLine.Scope.HasValue &&
                appQuestionAndLine.Scope.Value == (int)defra_application_task_scope.Item)
            {
                newAnswerEntity.Attributes.Add(
                    defra_applicationanswer.Fields.defra_applicationlineid,
                    new EntityReference(
                        defra_applicationline.EntityLogicalName,
                        appQuestionAndLine.ApplicationLineId.Value));
            }

            // Talk to CRM, create the answer record
            newAnswer.ApplicationAnswerId = OrganisationService.Create(newAnswerEntity);

            // Return answer
            return(newAnswer);
        }
        private static bool HasMatchingApplicationAnswer(List <ApplicationAnswer> currentApplicationAnswers, ApplicationQuestionsAndLines appQuestionAndLine)
        {
            foreach (var currentApplicationAnswer in currentApplicationAnswers)
            {
                if (currentApplicationAnswer.ApplicationQuestionId != appQuestionAndLine.ApplicationQuestionId)
                {
                    // Not the same question
                    continue;
                }

                if (appQuestionAndLine.Scope == (int)defra_application_task_scope.Item &&
                    currentApplicationAnswer.ApplicationLineId == appQuestionAndLine.ApplicationLineId)
                {
                    // Question is at the item/activity level, check for an exact match against the application line
                    return(true);
                }

                if (!appQuestionAndLine.Scope.HasValue || appQuestionAndLine.Scope.Value == (int)defra_application_task_scope.Application)
                {
                    // Question is at the application level, check for answers not linked to an application line
                    return(true);
                }
            }
            return(false);
        }