Public/New-SurveyResponseCsv.ps1

<#
    .SYNOPSIS
    Create a new template survey response file for a defined survey.

    .DESCRIPTION
    Create a new template survey response file for a defined survey.

    .INPUTS
    None. You cannot pipe objects to New-SurveyResponseCsv.

    .OUTPUTS
    None.

    .PARAMETER SurveyName
    The name of the survey in the eCC salesforce org

    .PARAMETER ResponseFile
    The name of the template response file to create. If not supplied the file name used will be the {SurveyName}.csv

    .PARAMETER SurveyDefinitions
    The survey definitions from Get-SurveyDefinitions call. Will call Get-SurveyDefinitions is not supplied

    .LINK
    Get-SurveyDefinitions
    Set-FileConfig

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

function New-SurveyResponseCsv {
    param($SurveyName, $ResponseFile, $SurveyDefinitions)
    if (-not $PSBoundParameters.ContainsKey('SurveyDefinitions')) {
        $SurveyDefinitions = Get-SurveyDefinitions
    }
    if (-not $PSBoundParameters.ContainsKey('SurveyName')) {
        $SurveyName = $SurveyDefinitions.surveys | Select-Object name | Out-GridView -PassThru | Select-Object Name -ExpandProperty Name
    }
    if ($SurveyName) {
        if (-not $PSBoundParameters.ContainsKey('ResponseFile')) {
            $ResponseFile = "$($SurveyName).csv"
        }
        $output = @()
        $survey = $SurveyDefinitions.surveys | Where-Object { $_.Name -eq $SurveyName }
        $questions = $SurveyDefinitions.questions | Where-Object { $_.phecc__Survey__c -eq $survey.Id }
        foreach($question in $questions) {
            $answers =  $SurveyDefinitions.answers | Where-Object { $_.phecc__Survey_Question__c -eq $question.Id }
            $allAnswer = ""
            foreach($answer in $answers) {
                if ($allAnswer.Length -gt 0) {
                    $allAnswer += "|"
                }
                $allAnswer += $answer.phecc__Answer__c
            }
            $output += New-Object PSObject -Property @{
                Question = $question.phecc__Question__c
                Answer =  $allAnswer
            }
        }
        $output | ConvertTo-Csv | Set-Content -Path $ResponseFile
    }
}