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. If not supplied then the user will be prompted with an Out-GridView

    .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 {

    [CmdletBinding()]
    [OutputType([System.Void])]
    param(
        [Parameter(Mandatory = $false, Position = 0, ValueFromPipeline)]
        [ValidateNotNullOrEmpty()]
        [String]
        $SurveyName,

        [Parameter(Mandatory = $false, Position = 1)]
        [ValidateNotNullOrEmpty()]
        [String]
        $ResponseFile,

        [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
        }
        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 PSCustomObject -Property @{
                    Question = $question.phecc__Question__c
                    Answer   = $allAnswer
                }
            }
            $output | ConvertTo-Csv | Set-Content -Path $ResponseFile
        }
    }
}