forked from ForceCLI/force
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sobject.go
186 lines (159 loc) · 3.79 KB
/
sobject.go
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
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"html"
"io/ioutil"
"os"
"strings"
)
var cmdSobject = &Command{
Run: runSobject,
Usage: "sobject",
Short: "Manage standard & custom objects",
Long: `
Manage sobjects
Usage:
force sobject list
force sobject create <object> [<field>:<type> [<option>:<value>]]
force sobject delete <object>
force sobject import
Examples:
force sobject list
force sobject create Todo Description:string
force sobject delete Todo
`,
}
func runSobject(cmd *Command, args []string) {
if len(args) == 0 {
cmd.printUsage()
} else {
switch args[0] {
case "list":
runSobjectList(args[1:])
case "create", "add":
runSobjectCreate(args[1:])
case "delete", "remove":
runSobjectDelete(args[1:])
case "import":
runSobjectImport(args[1:])
default:
ErrorAndExit("no such command: %s", args[0])
}
}
}
func getSobjectList(args []string) (l []ForceSobject) {
force, _ := ActiveForce()
sobjects, err := force.ListSobjects()
if err != nil {
ErrorAndExit(fmt.Sprintf("ERROR: %s\n", err))
}
for _, sobject := range sobjects {
if len(args) == 1 {
if strings.Contains(sobject["name"].(string), args[0]) {
l = append(l, sobject)
}
} else {
l = append(l, sobject)
}
}
return
}
func runSobjectList(args []string) {
l := getSobjectList(args)
DisplayForceSobjects(l)
}
func runSobjectCreate(args []string) {
if len(args) < 1 {
ErrorAndExit("must specify object name")
}
force, _ := ActiveForce()
if err := force.Metadata.CreateCustomObject(args[0]); err != nil {
ErrorAndExit(err.Error())
}
fmt.Println("Custom object created")
if len(args) > 1 {
args[0] = fmt.Sprintf("%s__c", args[0])
runFieldCreate(args)
}
}
func runSobjectDelete(args []string) {
if len(args) < 1 {
ErrorAndExit("must specify object")
}
force, _ := ActiveForce()
if err := force.Metadata.DeleteCustomObject(args[0]); err != nil {
ErrorAndExit(err.Error())
}
fmt.Println("Custom object deleted")
}
func runSobjectImport(args []string) {
var objectDef = `
<cmd:sObjects>
<cmd:type>%s</cmd:type>
%s</cmd:sObjects>`
// Need to read the file into a query result structure
data, err := ioutil.ReadAll(os.Stdin)
var query ForceQueryResult
json.Unmarshal(data, &query)
if err != nil {
ErrorAndExit(err.Error())
}
var soapMsg = ""
var objectType = ""
for _, record := range query.Records {
var fields = ""
for key, _ := range record {
if key == "Id" {
continue
} else if key == "attributes" {
x := record[key].(map[string]interface{})
val, ok := x["type"]
if ok {
objectType, ok = val.(string)
}
} else {
if record[key] != nil {
val, ok := record[key].(string)
if ok {
fields += fmt.Sprintf("\t<%s>%s</%s>\n", key, html.EscapeString(val), key)
} else {
valf, ok := record[key].(float64)
if ok {
fields += fmt.Sprintf("\t<%s>%f</%s>\n", key, valf, key)
} else {
fields += fmt.Sprintf("\t<%s>%s</%s>\n", key, record[key].(string), key)
}
}
}
}
}
soapMsg += fmt.Sprintf(objectDef, objectType, fields)
}
force, _ := ActiveForce()
response, err := force.Partner.soapExecuteCore("create", soapMsg)
type errorData struct {
Fields string `xml:"field"`
Message string `xml:"message"`
StatusCode string `xml:"statusCode"`
}
type result struct {
Id string `xml:"id"`
Success bool `xml:"success"`
Errors []errorData `xml:"errors"`
}
var xmlresponse struct {
Results []result `xml:"Body>createResponse>result"`
}
xml.Unmarshal(response, &xmlresponse)
for i, res := range xmlresponse.Results {
if res.Success {
fmt.Printf("%s created successfully\n", res.Id)
} else {
for _, e := range res.Errors {
fmt.Printf("%s\n\t%s\n%s\n", e.StatusCode, e.Message, query.Records[i])
}
}
}
}