HornbillContactCreateWorkflow.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

<#PSScriptInfo
 
.VERSION 1.1.1
 
.GUID 2eb1e8f8-9628-4a3e-92ce-4c1e17133ea9
 
.AUTHOR steve.goldthorpe@hornbill.com
 
.COMPANYNAME Hornbill
 
.TAGS hornbill powershell azure automation workflow runbook
 
.LICENSEURI https://wiki.hornbill.com/index.php/The_Hornbill_Community_License_(HCL)
 
.PROJECTURI https://github.com/hornbill/powershellHornbillAzureRunbooks
 
.ICONURI https://wiki.hornbill.com/skins/common/images/HBLOGO.png
 
.RELEASENOTES
Included parameter descriptions
 
.DESCRIPTION
 Azure Automation Runbook to create a contact record on a Hornbill instance.
 
#>


#.PARAMETER instanceName
#MANDATORY: The name of the Instance to connect to.

#.PARAMETER instanceKey
#MANDATORY: An API key with permission on the Instance to carry out the required API calls.

#.PARAMETER h_firstname
#MANDATORY: Contact firstname

#.PARAMETER h_lastname
#MANDATORY: Contact surname

#.PARAMETER organisationName
#Name of the organisation that this contact belongs to. The organisation must aready exist in your Hornbill instance

#.PARAMETER h_jobtitle
#Contact job title

#.PARAMETER h_tel_1
#Contact primary phone number

#.PARAMETER h_tel_2
#Contact secondary phone number

#.PARAMETER h_email_1
#Contact primary email

#.PARAMETER h_email_2
#Contact secondary email

#.PARAMETER h_description
#Contact description

#.PARAMETER h_country
#Contact country code

#.PARAMETER h_language
#Contact language code

#.PARAMETER h_owner
#MANDATORY: Contact owner account ID

#.PARAMETER h_notes
#Contact notes

#.PARAMETER h_private
#Contact mark as private (true or false)

#.PARAMETER h_custom_1
#Contact custom field

#.PARAMETER h_custom_2
#Contact custom field

#.PARAMETER h_custom_3
#Contact custom field

#.PARAMETER h_custom_4
#Contact custom field

#.PARAMETER h_custom_5
#Contact custom field

#.PARAMETER h_custom_6
#Contact custom field

#Requires -Module @{ModuleVersion = '1.1.0'; ModuleName = 'HornbillAPI'}
#Requires -Module @{ModuleVersion = '1.1.1'; ModuleName = 'HornbillHelpers'}

workflow Hornbill_ContactCreate_Workflow
{
    # Define output stream type
    [OutputType([object])]

    # Define runbook input params
    Param
    (
        # Instance Connection Params
        [Parameter (Mandatory= $true)]
        [string] $instanceName,
        [Parameter (Mandatory= $true)]
        [string] $instanceKey,

        # API Params
        [Parameter (Mandatory= $true)]
        [string] $h_firstname,
        [Parameter (Mandatory= $true)]
        [string] $h_lastname,
        [Parameter (Mandatory= $true)]
        [string] $h_owner,
        [string] $organisationName,
        [string] $h_jobtitle,
        [string] $h_tel_1,
        [string] $h_tel_2,
        [string] $h_email_1,
        [string] $h_email_2,
        [string] $h_description,
        [string] $h_country,
        [string] $h_language,
        [string] $h_notes,
        [string] $h_private,
        [string] $h_custom_1,
        [string] $h_custom_2,
        [string] $h_custom_3,
        [string] $h_custom_4,
        [string] $h_custom_5,
        [string] $h_custom_6
    )

    # Define instance details
    Set-HB-Instance -Instance $instanceName -Key $instanceKey

    # Build XMLMC call
    Add-HB-Param "h_firstname"        $h_firstname    $false
    Add-HB-Param "h_lastname"        $h_lastname        $false
    Add-HB-Param "h_jobtitle"        $h_jobtitle        $false
    Add-HB-Param "h_tel_1"            $h_tel_1        $false
    Add-HB-Param "h_tel_2"            $h_tel_2        $false
    Add-HB-Param "h_email_1"        $h_email_1        $false
    Add-HB-Param "h_email_2"        $h_email_2        $false
    Add-HB-Param "h_description"    $h_description    $false
    Add-HB-Param "h_country"         $h_country        $false
    Add-HB-Param "h_language"         $h_language        $false
    Add-HB-Param "h_owner"             $h_owner        $false
    Add-HB-Param "h_notes"             $h_notes        $false
    Add-HB-Param "h_private"         $h_private        $false
    Add-HB-Param "h_custom_1"         $h_custom_1        $false
    Add-HB-Param "h_custom_2"         $h_custom_2        $false
    Add-HB-Param "h_custom_3"         $h_custom_3        $false
    Add-HB-Param "h_custom_4"         $h_custom_4        $false
    Add-HB-Param "h_custom_5"         $h_custom_5        $false
    Add-HB-Param "h_custom_6"         $h_custom_6        $false

    # Invoke XMLMC call, output returned as PSObject
    $xmlmcOutput = Invoke-HB-XMLMC "apps/com.hornbill.core/Contact" "addContactNew"

    # Read output status
    if($xmlmcOutput.status -eq "ok") {
        $NewContactID = $xmlmcOutput.params.h_pk_id
    }

    # Build resultObject to write to output
    $resultObject = New-Object PSObject -Property @{
        Status = $xmlmcOutput.status
        Error = $xmlmcOutput.error
        ContactID = $NewContactID
    }

    if($NewContactID -and $NewContactID -ne "" -and $organisationName -and $organisationName -ne "") {
        $OrgObj = Get-HB-OrganisationID $organisationName
        if($OrgObj -and $OrgObj.OrganisationID -and $OrgObj.OrganisationID -ne ""){
            Add-HB-Param "contactId" $NewContactID
            Add-HB-Param "orgId" $OrgObj.OrganisationID
            $xmlmcOutputAddOrg = Invoke-HB-XMLMC "apps/com.hornbill.core/Contact" "changeOrg"
            if($xmlmcOutputAddOrg.status -ne "ok"){
                # Build resultObject to write to output
                $resultObject = New-Object PSObject -Property @{
                    Status = $xmlmcOutput.status
                    Error = $xmlmcOutput.error
                    ContactID = $NewContactID
                    Warnings = "Contact added but unable to associate to Organisation: "+$xmlmcOutputAddOrg.error
                }
            }
        }
    }

    if($resultObject.Status -ne "ok"){
        Write-Error $resultObject
    } else {
        Write-Output $resultObject
    }
}