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

<#PSScriptInfo
 
.VERSION 1.1.0
 
.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
Removed requirement to provide instanceZone param
 
.DESCRIPTION
 Azure Automation Runbook to create a contact record on a Hornbill instance.
 
#>


#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
    }
}