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 PSCustomObject 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-Config

    .NOTES
    Assumes config is initialized for org access.
#>

function Get-SurveyResponsesFromCsv {

    [CmdletBinding()]
    [OutputType([PSCustomObject])]
    param(
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline)]
        [ValidateNotNullOrEmpty()]
        [String]
        $SurveyName,

        [Parameter(Mandatory = $true, Position = 1)]
        [ValidateNotNullOrEmpty()]
        [String]
        $AnswerFile,

        [Parameter(Mandatory = $false, Position = 2)]
        [ValidateNotNull()]
        [PSCustomObject]
        $SurveyDefinitions
    )

    begin {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started"
    }

    end {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete"
    }

    process {
        Write-Debug "[$($MyInvocation.MyCommand.Name)] PSBoundParameters: $($PSBoundParameters | Out-String)"

        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 = [PSCustomObject]@{
            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 += [PSCustomObject]@{
                Survey_Question__c = $question.Id
                Answers            = @(
                    @{
                        Survey_Answer__c = $answer.Id
                    }
                );
            }
        }
        Write-Output $Body
    }
}