Public/Get-SurveyResponsesFromCsv.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<#
    .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 = @{
            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 += @{
                Survey_Question__c = $question.Id
                Answers            = @(
                    @{
                        Survey_Answer__c = $answer.Id
                    }
                );
            }
        }
        Write-Output [PSCustomObject]$Body
    }
}