Public/Get-SurveyResponsesFromCsv.ps1
<# .SYNOPSIS Reads the survey responses from a CSV file. .DESCRIPTION Returns a message body that can be used to send to eCC to create a survey response. .INPUTS None. You cannot pipe objects to Get-SurveyResponsesFromCsv. .OUTPUTS A hashtable that can be used to create the content of the message to send to eCC API for survey responses. .PARAMETER SurveyName The name of the survey. Much be in the survey definitions for the org. .PARAMETER AnswerFile The file name of the CSV containing the answers to respond .PARAMETER SurveyDefinitions The survey definitions from the Get-SurveyDefinitions cmdlet. This is optiona and the the SurveyDefinitions cmdlet will be used if not passed. .EXAMPLE PS> $surveyDefs = Get-SurveyResponsesFromCsv -SurveyName "my-survey" -AnswerFile "my-survey-answers.csv" .LINK Set-FileConfig .NOTES Assumes config is initialized for org access. #> function Get-SurveyResponsesFromCsv { param($SurveyName, $AnswerFile, $SurveyDefinitions) if (-not $PSBoundParameters.ContainsKey('SurveyDefinitions')) { $SurveyDefinitions = Get-SurveyDefinitions } $Survey = $SurveyDefinitions.surveys | Where-Object { $_.Name -eq $SurveyName } if (-not $Survey) { throw "Survey Name '$($SurveyName)' not found in org." } if (-not (Test-Path $AnswerFile -PathType Leaf)) { throw "Survey response file '$($AnswerFile)' not found." } $Body = @{ body = @{ surveyResponse = @{ Survey__c = $Survey.Id Survey_Response_Language__c = "en" Responses = @() } } } $answerRows = Get-Content $AnswerFile | ConvertFrom-Csv foreach ($answerRow in $answerRows) { $question = $SurveyDefinitions.questions | Where-Object { $_.phecc__Question__c -eq $answerRow.Question -and $_.phecc__Survey__c -eq $Survey.Id } if (-not $question) { Write-Error "question not found in org for file '$($AnswerFile)' - $($answerRow.Question)" } $answer = $SurveyDefinitions.answers | Where-Object { $_.phecc__Answer__c -eq $answerRow.Answer -and $_.phecc__Survey_Question__c -eq $question.Id } if (-not $answer) { Write-Error "answer not found in org for file '$($AnswerFile)' - $($answerRow.Answer)" } $Body.body.SurveyResponse.Responses += @{ Survey_Question__c = $question.Id Answers = @( @{ Survey_Answer__c = $answer.Id } ); } } $Body } |