/// <summary>
        /// Creates an application document record
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="source"></param>
        /// <param name="applicationId"></param>
        /// <param name="documentName"></param>
        /// <param name="documentLink"></param>
        /// <param name="caseId"></param>
        /// <param name="emailId"></param>
        /// <param name="createdById"></param>
        /// <param name="owningUserId"></param>
        /// <param name="owningTeamId"></param>
        /// <returns></returns>
        public Guid CreateApplicationDocument(
            string documentName,
            string documentLink,
            string fileName,
            defra_ApplicationDocumentSource source,
            Guid?applicationId,
            Guid?caseId,
            Guid?emailId,
            Guid?createdById,
            Guid?owningUserId,
            Guid?owningTeamId)
        {
            TracingService.Trace($"CreateApplicationDocument() documentName={documentName}, documentLink={documentLink}, fileName={fileName}, source={source}, applicationId={applicationId}, caseId={caseId}, emailId={emailId}, owningUserId={owningUserId}, owningTeamId={owningTeamId}");

            // Prep the entity
            Entity appDocumentEntity = new Entity(defra_applicationdocument.EntityLogicalName);

            if (applicationId.HasValue)
            {
                appDocumentEntity.Attributes.Add(defra_applicationdocument.Fields.defra_applicationid, new EntityReference(defra_application.EntityLogicalName, applicationId.Value));
            }

            if (caseId.HasValue)
            {
                appDocumentEntity.Attributes.Add(defra_applicationdocument.Fields.defra_caseid, new EntityReference(Incident.EntityLogicalName, caseId.Value));
            }

            if (emailId.HasValue)
            {
                appDocumentEntity.Attributes.Add(defra_applicationdocument.Fields.defra_emailid, new EntityReference(Email.EntityLogicalName, emailId.Value));
            }

            if (createdById.HasValue)
            {
                appDocumentEntity.Attributes.Add(defra_applicationdocument.Fields.CreatedBy, new EntityReference(Incident.EntityLogicalName, createdById.Value));
                appDocumentEntity.Attributes.Add(defra_applicationdocument.Fields.CreatedOnBehalfBy, new EntityReference(Incident.EntityLogicalName, createdById.Value));
            }

            if (owningUserId.HasValue)
            {
                appDocumentEntity.Attributes.Add(defra_applicationdocument.Fields.OwnerId, new EntityReference(SystemUser.EntityLogicalName, owningUserId.Value));
            }
            else if (owningTeamId.HasValue)
            {
                appDocumentEntity.Attributes.Add(defra_applicationdocument.Fields.OwnerId, new EntityReference(Team.EntityLogicalName, owningTeamId.Value));
            }

            appDocumentEntity.Attributes.Add(defra_applicationdocument.Fields.defra_name, documentName);
            appDocumentEntity.Attributes.Add(defra_applicationdocument.Fields.defra_filename, fileName);
            appDocumentEntity.Attributes.Add(defra_applicationdocument.Fields.defra_url, documentLink);

            // Get CRM to Create record
            return(OrganisationService.Create(appDocumentEntity));
        }
Exemple #2
0
        /// <summary>
        /// Creates a defra_applicationtask record
        /// </summary>
        /// <param name="applicationId">defra_application id to link the record to</param>
        /// <param name="ownerTeamId"></param>
        /// <param name="applicationTaskDefinitionId">defra_applicationtaskdefinition id to link the record to</param>
        /// <param name="ownerUserId"></param>
        /// <returns>Newly created record id</returns>
        public Guid CreateApplicationTask(Guid applicationId, Guid?ownerUserId, Guid?ownerTeamId, Guid applicationTaskDefinitionId)
        {
            TracingService.Trace($"CreateApplicationTask(applicationId={applicationId}, ownerUserId={ownerUserId}, ownerTeamId={ownerTeamId},applicationTaskDefinitionId={applicationTaskDefinitionId})");

            Entity applicationTask = new Entity(defra_applicationtask.EntityLogicalName);

            applicationTask.Attributes.Add(defra_applicationtask.Fields.defra_applicationid, new EntityReference(defra_application.EntityLogicalName, applicationId));
            applicationTask.Attributes.Add(defra_applicationtask.Fields.defra_applicationtaskdefinitionid, new EntityReference(defra_applicationtaskdefinition.EntityLogicalName, applicationTaskDefinitionId));

            if (ownerUserId.HasValue)
            {
                applicationTask.Attributes.Add(defra_applicationtask.Fields.OwnerId, new EntityReference(SystemUser.EntityLogicalName, ownerUserId.Value));
            }
            else if (ownerTeamId.HasValue)
            {
                applicationTask.Attributes.Add(defra_applicationtask.Fields.OwnerId, new EntityReference(Team.EntityLogicalName, ownerTeamId.Value));
            }
            return(OrganisationService.Create(applicationTask));
        }
        /// <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);
        }
        /// <summary>
        /// Creates a defra_application_answer record using the given codes
        /// </summary>
        /// <param name="applicationQuestionCode">Question code to set in the newly created applciation answer record</param>
        /// <param name="applicationAnswerOptionCode">Answer Option code to set in the new record, may be null</param>
        /// <param name="applicationAnswerText">Answer plain text to be set, may be null</param>
        /// <param name="application">Application to link to</param>
        /// <param name="applicationLine">Application Line to link to, may be null</param>
        /// <returns>Guid for the newly created application answer record</returns>
        private Guid CreateApplicationAnswer(
            string applicationQuestionCode,
            string applicationAnswerOptionCode,
            string applicationAnswerText,
            EntityReference application,
            EntityReference applicationLine)
        {
            // Prep the new entity for creation
            Entity newAnswer = new Entity(defra_applicationanswer.EntityLogicalName);

            // Set Application
            newAnswer.Attributes.Add(defra_applicationanswer.Fields.defra_application, application);

            // Set Answer Text
            newAnswer.Attributes.Add(defra_applicationanswer.Fields.defra_answertext, applicationAnswerText);

            // Set Application Line
            if (applicationLine != null)
            {
                newAnswer.Attributes.Add(defra_applicationanswer.Fields.defra_applicationlineid, applicationLine);
            }

            // Set Answer Option and Question
            if (!string.IsNullOrWhiteSpace(applicationAnswerOptionCode))
            {
                // Get the corresponding application question option, and the question in one go, save query time by doing one query
                Entity questionAndOptionEntities =
                    GetApplicationQuestionAndOption(applicationQuestionCode, applicationAnswerOptionCode);

                if (questionAndOptionEntities == null)
                {
                    throw new InvalidPluginExecutionException(
                              $"Could not find Application Question Option with code {applicationAnswerOptionCode} for application question {applicationQuestionCode}");
                }

                // Set the Answer Option
                if (questionAndOptionEntities.Contains(defra_applicationquestionoption.Fields
                                                       .defra_applicationquestionoptionId))
                {
                    EntityReference optionEntityReference =
                        new EntityReference(defra_applicationquestionoption.EntityLogicalName,
                                            questionAndOptionEntities.GetAttributeValue <Guid>(defra_applicationquestionoption.Fields
                                                                                               .defra_applicationquestionoptionId));
                    newAnswer.Attributes.Add(defra_applicationanswer.Fields.defra_answer_option, optionEntityReference);
                }

                // Set the Question
                string questionAttribute =
                    $"{QuestionEntityAlias}.{defra_applicationquestion.Fields.defra_applicationquestionId}";
                if (questionAndOptionEntities.Contains(questionAttribute))
                {
                    EntityReference questionEntityReference =
                        new EntityReference(defra_applicationquestionoption.EntityLogicalName,
                                            (Guid)questionAndOptionEntities.GetAttributeValue <AliasedValue>(questionAttribute).Value);
                    newAnswer.Attributes.Add(defra_applicationanswer.Fields.defra_question, questionEntityReference);
                }
            }
            // Set the answer Question, no answer option
            else
            {
                // Get the question record for the given code
                Entity applicationQuestion = GetApplicationQuestion(applicationQuestionCode);

                if (applicationQuestion == null)
                {
                    throw new InvalidPluginExecutionException(
                              $"Could not find Application Question with code {applicationAnswerOptionCode}");
                }

                // Set Application Question
                newAnswer.Attributes.Add(defra_applicationanswer.Fields.defra_question,
                                         applicationQuestion.ToEntityReference());
            }

            // Tell CRM to create the answer record, and return it's Id
            return(OrganisationService.Create(newAnswer));
        }