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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<#
    .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
        }
    }
}