Public/New-SurveyResponseCsv.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<#
    .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
    }
}