public static void ProcessSurveyResponse(List <CRCaseSurveySetup> setup)
        {
            var graph          = PXGraph.CreateInstance <CaseSurveyResponseEngine>();
            var setupRecord    = graph.SetupRecord.SelectSingle();
            var setupRecordExt = graph.SetupRecord.Cache.GetExtension <CRSetupExt>(setupRecord);

            var fromDate = setupRecordExt.UsrLastSurveySyncDate;
            var toDate   = DateTime.UtcNow;

            var dataReader = new PX.SurveyMonkeyReader.SurveyMonkeyReader(setupRecordExt.UsrSurveyID,
                                                                          setupRecordExt.UsrAccessToken);

            List <SurveyResponse> surveyResponses;

            try
            {
                surveyResponses = dataReader.GetSurveyResponsesByDateRange(fromDate, toDate);
            }
            catch (Exception ex)
            {
                throw new PXException(ex.Message);
            }

            foreach (var surveyResponse in surveyResponses)
            {
                graph.Cases.Current = (CRCase)graph.Cases.Search <CRCase.caseCD>(surveyResponse.CaseCD);
                if (graph.Cases.Current == null)
                {
                    continue;
                }

                // Surveys can technically be answered by more than one person; if responses exist already just skip over the response since
                // the integration doesn't handle multiple responses per case.
                if (graph.CaseSurveyResponses.SelectSingle() != null)
                {
                    continue;
                }

                var caseExt = PXCache <CRCase> .GetExtension <CRCaseExt>(graph.Cases.Current);

                caseExt.UsrResponseID   = surveyResponse.ResponseID;
                caseExt.UsrResponseDate = surveyResponse.ResponseDate;
                graph.Cases.Current     = graph.Cases.Update(graph.Cases.Current);

                foreach (var question in surveyResponse.Questions)
                {
                    graph.SetQuestionAnswer(dataReader, question);
                }

                graph.Actions.PressSave();
            }

            setupRecordExt.UsrLastSurveySyncDate = toDate;
            graph.SetupRecord.Update(setupRecord);

            graph.Actions.PressSave();

            PXProcessing <CRCaseSurveySetup> .SetInfo("Successfully synched");
        }
        private void CreateQuestion(PX.SurveyMonkeyReader.SurveyMonkeyReader dataReader, SurveyResponseQuestion surveyResponseQuestion)
        {
            // Question does not specify a parent question?  Set current parent question to null.
            if (surveyResponseQuestion.ParentQuestionID == null)
            {
                ParentSurveyQuestions.Current = null;
            }
            else
            {
                CRSurveyQuestion parentQuestion =
                    PXSelect <CRSurveyQuestion,
                              Where <CRSurveyQuestion.surveyQuestionID,
                                     Equal <Required <CRSurveyQuestion.surveyQuestionID> >,
                                     And <CRSurveyQuestion.surveyLastModified,
                                          Equal <Required <CRSurveyQuestion.surveyLastModified> > > > >
                    .Select(this,
                            surveyResponseQuestion.ParentQuestionID,
                            surveyResponseQuestion.SurveyLastModified);

                // Parent question not yet in DB?  Request parent question info from api and insert it into the DB
                if (parentQuestion == null)
                {
                    parentQuestion          = GetQuestionDetails(dataReader, surveyResponseQuestion.ParentQuestionID.Value);
                    SurveyQuestions.Current = null; // Don't hang onto old question info
                    parentQuestion          = ParentSurveyQuestions.Insert(parentQuestion);
                }

                ParentSurveyQuestions.Current = parentQuestion;
            }

            // Request question info from api and insert it into the DB
            var question = GetQuestionDetails(dataReader, surveyResponseQuestion.ResponseIdentifier.QuestionID);

            SurveyQuestions.Current = SurveyQuestions.Insert(question);

            // Put the answers for the newly created question into the database too
            var newSurveyAnswers = GetQuestionAnswers(dataReader, surveyResponseQuestion.ResponseIdentifier.QuestionID);

            if (newSurveyAnswers != null)
            {
                foreach (var newSurveyAnswer in newSurveyAnswers)
                {
                    SurveyAnswers.Insert(newSurveyAnswer);
                }
            }
        }
        private static IEnumerable <CRSurveyAnswer> GetQuestionAnswers(PX.SurveyMonkeyReader.SurveyMonkeyReader dataReader, long surveyQuestionId)
        {
            SurveyQuestion surveyQuestion;

            try
            {
                surveyQuestion = dataReader.GetSurveyQuestion(surveyQuestionId);
            }
            catch (Exception ex)
            {
                throw new PXException(ex.Message);
            }

            return(surveyQuestion.Answers?.Select(answer => new CRSurveyAnswer
            {
                SurveyAnswerID = answer.AnswerID, SurveyLastModified = surveyQuestion.SurveyLastModified, Answer = answer.Answer
            }).ToList());
        }
        private static CRSurveyQuestion GetQuestionDetails(PX.SurveyMonkeyReader.SurveyMonkeyReader dataReader,
                                                           long surveyQuestionId)
        {
            SurveyQuestion questionDetails;

            try
            {
                questionDetails = dataReader.GetSurveyQuestion(surveyQuestionId);
            }
            catch (Exception ex)
            {
                throw new PXException(ex.Message);
            }

            var newQuestion = new CRSurveyQuestion
            {
                SurveyQuestionID   = questionDetails.QuestionID,
                SurveyLastModified = questionDetails.SurveyLastModified,
                Question           = questionDetails.Question
            };

            return(newQuestion);
        }
        public void SetQuestionAnswer(PX.SurveyMonkeyReader.SurveyMonkeyReader dataReader, SurveyResponseQuestion surveyResponseQuestion)
        {
            var question = (CRSurveyQuestion)PXSelect <CRSurveyQuestion,
                                                       Where <CRSurveyQuestion.surveyQuestionID,
                                                              Equal <Required <CRSurveyQuestion.surveyQuestionID> >,
                                                              And <CRSurveyQuestion.surveyLastModified,
                                                                   Equal <Required <CRSurveyQuestion.surveyLastModified> > > > >
                           .Select(this, surveyResponseQuestion.ResponseIdentifier.QuestionID, surveyResponseQuestion.SurveyLastModified);

            // Question already in DB?  Set the current question to it
            if (question != null)
            {
                SurveyQuestions.Current = question;
            }
            else
            {
                CreateQuestion(dataReader, surveyResponseQuestion);
            }

            // If the current question's answers can be found in the answers table, use the answer ID for the current answer.
            // Otherwise, the answer is stored as freetext in the survey responses table, so don't store it in the current answer at all
            SurveyAnswers.Current = surveyResponseQuestion.ResponseIdentifier.AnswerID > 0
                                             ? SurveyAnswers.Search <CRSurveyAnswer.surveyAnswerID>(surveyResponseQuestion.ResponseIdentifier.AnswerID)
                                             : null;

            //Other CRCaseSurveyResponse fields will be field based on current values for other views through PXDBDefault
            var caseSurveyResponse = new CRCaseSurveyResponse {
                Answer = surveyResponseQuestion.Answer
            };

            if (surveyResponseQuestion.ResponseIdentifier.AnswerID == 0)
            {
                caseSurveyResponse.AnswerLineNbr = 0;
            }
            CaseSurveyResponses.Insert(caseSurveyResponse);
        }