github.com/schmorrison/Zoho@v1.1.4/crm/records.go (about) 1 package crm 2 3 import ( 4 "fmt" 5 "time" 6 7 zoho "github.com/schmorrison/Zoho" 8 ) 9 10 // ListRecords will return a list of the records provided in the request field, and specified by the module 11 // https://www.zoho.com/crm/help/api/v2/#record-api 12 func (c *API) ListRecords( 13 request interface{}, 14 module Module, 15 params map[string]zoho.Parameter, 16 ) (data interface{}, err error) { 17 endpoint := zoho.Endpoint{ 18 Name: "records", 19 URL: fmt.Sprintf("https://www.zohoapis.%s/crm/v2/%s", c.ZohoTLD, module), 20 Method: zoho.HTTPGet, 21 ResponseData: request, 22 URLParameters: map[string]zoho.Parameter{ 23 "fields": "", 24 "sort_order": "", 25 "sort_by": "", 26 "converted": "", 27 "approved": "", 28 "page": "", 29 "per_page": "200", 30 "cvid": "", 31 }, 32 } 33 34 for k, v := range params { 35 endpoint.URLParameters[k] = v 36 } 37 38 err = c.Zoho.HTTPRequest(&endpoint) 39 if err != nil { 40 return nil, fmt.Errorf("Failed to retrieve records of %s: %s", module, err) 41 } 42 43 if endpoint.ResponseData != nil { 44 return endpoint.ResponseData, nil 45 } 46 47 return nil, fmt.Errorf("Data returned was nil") 48 } 49 50 // InsertRecords will add records in request to the specified module 51 // https://www.zoho.com/crm/help/api/v2/#ra-insert-records 52 func (c *API) InsertRecords( 53 request InsertRecordsData, 54 module Module, 55 ) (data InsertRecordsResponse, err error) { 56 endpoint := zoho.Endpoint{ 57 Name: "records", 58 URL: fmt.Sprintf("https://www.zohoapis.%s/crm/v2/%s", c.ZohoTLD, module), 59 Method: zoho.HTTPPost, 60 ResponseData: &InsertRecordsResponse{}, 61 RequestBody: request, 62 } 63 64 err = c.Zoho.HTTPRequest(&endpoint) 65 if err != nil { 66 return InsertRecordsResponse{}, fmt.Errorf( 67 "Failed to insert records of %s: %s", 68 module, 69 err, 70 ) 71 } 72 73 if v, ok := endpoint.ResponseData.(*InsertRecordsResponse); ok { 74 return *v, nil 75 } 76 77 return InsertRecordsResponse{}, fmt.Errorf("Data returned was nil") 78 } 79 80 //UpdateRecordsResponseData is the data provided to UpdateRecords 81 type UpdateRecordsResponseData struct { 82 Message string `json:"message,omitempty"` 83 Details struct { 84 ExpectedDataType string `json:"expected_data_type,omitempty"` 85 APIName string `json:"api_name"` 86 } `json:"details,omitempty"` 87 Status string `json:"status,omitempty"` 88 Code string `json:"code,omitempty"` 89 } 90 91 // InsertRecordsData is the data provided to InsertRecords 92 type InsertRecordsData struct { 93 Data interface{} `json:"data,omitempty"` 94 Trigger []string `json:"trigger,omitempty"` 95 } 96 type InsertRecordsResponseData struct { 97 Message string `json:"message,omitempty"` 98 Details struct { 99 CreatedBy struct { 100 ID string `json:"id,omitempty"` 101 Name string `json:"name,omitempty"` 102 } `json:"created_by,omitempty"` 103 ID string `json:"id,omitempty"` 104 ModifiedBy struct { 105 ID string `json:"id,omitempty"` 106 Name string `json:"name,omitempty"` 107 } `json:"modified_by,omitempty"` 108 ModifiedTime time.Time `json:"modified_time,omitempty"` 109 CreatedTime time.Time `json:"created_time,omitempty"` 110 } `json:"details,omitempty"` 111 Status string `json:"status,omitempty"` 112 Code string `json:"code,omitempty"` 113 } 114 115 // InsertRecordsResponse is the data returned by InsertRecords 116 type InsertRecordsResponse struct { 117 Data []InsertRecordsResponseData `json:"data,omitempty"` 118 } 119 120 // UpdateRecords will modify records by the data provided to request in the specified module 121 // https://www.zoho.com/crm/help/api/v2/#ra-update-records 122 // 123 // When performing an update, because the natural state of the records fields in this package is to 'omitempty', 124 // if you want to empty the fields contents you will need to embed the records type in a struct in your own package, 125 // and override the field with a field that has a json tag that does not contain 'omitempty'. 126 // eg. 127 // type struct Account { 128 // crm.Account 129 // CustomField string `json:"Custom_Field"` 130 // } 131 func (c *API) UpdateRecords( 132 request UpdateRecordsData, 133 module Module, 134 ) (data UpdateRecordsResponse, err error) { 135 endpoint := zoho.Endpoint{ 136 Name: "records", 137 URL: fmt.Sprintf("https://www.zohoapis.%s/crm/v2/%s", c.ZohoTLD, module), 138 Method: zoho.HTTPPut, 139 ResponseData: &UpdateRecordsResponse{}, 140 RequestBody: request, 141 } 142 143 err = c.Zoho.HTTPRequest(&endpoint) 144 if err != nil { 145 return UpdateRecordsResponse{}, fmt.Errorf( 146 "Failed to insert records of %s: %s", 147 module, 148 err, 149 ) 150 } 151 152 if v, ok := endpoint.ResponseData.(*UpdateRecordsResponse); ok { 153 return *v, nil 154 } 155 156 return UpdateRecordsResponse{}, fmt.Errorf("Data returned was nil") 157 } 158 159 // UpdateRecordsData is the data provided to UpdateRecords 160 type UpdateRecordsData = InsertRecordsData 161 162 // UpdateRecordsResponse is the data returned by UpdateRecords 163 type UpdateRecordsResponse struct { 164 Data []UpdateRecordsResponseData `json:"data,omitempty"` 165 } 166 167 // UpsertRecords will insert the provided records in the request, if they already exist it will be updated 168 // https://www.zoho.com/crm/help/api/v2/#ra-insert-or-update 169 // 170 // When performing an upsert, because the natural state of the records fields in this package is to 'omitempty' when encoding json, 171 // if you want to empty the fields contents in zoho you will need to embed the records type in a struct in your own package, 172 // and override the field with a field that has a json tag that does not contain 'omitempty'. 173 // eg. 174 // type struct Account { 175 // crm.Account 176 // CustomField string `json:"Custom_Field"` 177 // } 178 func (c *API) UpsertRecords( 179 request UpsertRecordsData, 180 module Module, 181 duplicateFieldsCheck []string, 182 ) (data UpsertRecordsResponse, err error) { 183 endpoint := zoho.Endpoint{ 184 Name: "records", 185 URL: fmt.Sprintf("https://www.zohoapis.%s/crm/v2/%s/upsert", c.ZohoTLD, module), 186 Method: zoho.HTTPPost, 187 ResponseData: &UpsertRecordsResponse{}, 188 RequestBody: request, 189 URLParameters: map[string]zoho.Parameter{ 190 "duplicate_field_check": func() zoho.Parameter { 191 if len(duplicateFieldsCheck) > 0 { 192 fields := "" 193 for i, a := range duplicateFieldsCheck { 194 fields += a 195 if i < len(duplicateFieldsCheck)-1 { 196 fields += "," 197 } 198 } 199 return zoho.Parameter(fields) 200 } 201 return "" 202 }(), 203 }, 204 } 205 206 err = c.Zoho.HTTPRequest(&endpoint) 207 if err != nil { 208 return UpsertRecordsResponse{}, fmt.Errorf( 209 "Failed to insert records of %s: %s", 210 module, 211 err, 212 ) 213 } 214 215 if v, ok := endpoint.ResponseData.(*UpsertRecordsResponse); ok { 216 return *v, nil 217 } 218 219 return UpsertRecordsResponse{}, fmt.Errorf("Data returned was nil") 220 } 221 222 // UpsertRecordsData is the data provided to UpsertRecords 223 type UpsertRecordsData struct { 224 Data interface{} `json:"data,omitempty"` 225 Trigger []string `json:"trigger,omitempty"` 226 } 227 228 // UpsertRecordsResponse is the data returned by UpsertRecords 229 type UpsertRecordsResponse struct { 230 Data []struct { 231 Message string `json:"message,omitempty"` 232 Details struct { 233 CreatedBy struct { 234 ID string `json:"id,omitempty"` 235 Name string `json:"name,omitempty"` 236 } `json:"created_by,omitempty"` 237 ID string `json:"id,omitempty"` 238 ModifiedBy struct { 239 ID string `json:"id,omitempty"` 240 Name string `json:"name,omitempty"` 241 } `json:"modified_by,omitempty"` 242 ModifiedTime time.Time `json:"modified_time,omitempty"` 243 CreatedTime time.Time `json:"created_time,omitempty"` 244 } `json:"details,omitempty"` 245 Status string `json:"status,omitempty"` 246 DuplicateField string `json:"duplicate_field,omitempty"` 247 Action string `json:"action,omitempty"` 248 Code string `json:"code,omitempty"` 249 } `json:"data,omitempty"` 250 } 251 252 // DeleteRecords will delete the records in the ids in the specified module 253 // https://www.zoho.com/crm/help/api/v2/#delete-bulk-records 254 func (c *API) DeleteRecords(module Module, ids []string) (data DeleteRecordsResponse, err error) { 255 if len(ids) == 0 { 256 return DeleteRecordsResponse{}, fmt.Errorf( 257 "Failed to delete records, must provide at least 1 ID", 258 ) 259 } 260 261 endpoint := zoho.Endpoint{ 262 Name: "records", 263 URL: fmt.Sprintf("https://www.zohoapis.%s/crm/v2/%s", c.ZohoTLD, module), 264 Method: zoho.HTTPDelete, 265 ResponseData: &DeleteRecordsResponse{}, 266 URLParameters: map[string]zoho.Parameter{ 267 "ids": func() zoho.Parameter { 268 idStr := "" 269 for i, a := range ids { 270 idStr += a 271 if i < len(ids)-1 { 272 idStr += "," 273 } 274 } 275 return zoho.Parameter(idStr) 276 }(), 277 }, 278 } 279 280 err = c.Zoho.HTTPRequest(&endpoint) 281 if err != nil { 282 return DeleteRecordsResponse{}, fmt.Errorf( 283 "Failed to insert records of %s: %s", 284 module, 285 err, 286 ) 287 } 288 289 if v, ok := endpoint.ResponseData.(*DeleteRecordsResponse); ok { 290 return *v, nil 291 } 292 293 return DeleteRecordsResponse{}, fmt.Errorf("Data returned was nil") 294 } 295 296 // DeleteRecordsResponse is the data returned by DeleteRecords 297 type DeleteRecordsResponse struct { 298 Data []struct { 299 Code string `json:"code,omitempty"` 300 Details struct { 301 ID string `json:"id,omitempty"` 302 } `json:"details,omitempty"` 303 Message string `json:"message,omitempty"` 304 Status string `json:"status,omitempty"` 305 } `json:"data,omitempty"` 306 } 307 308 // ListDeletedRecords will return a list of all records that have been deleted in the specified module. The records can be filtered by the kind parameter. 309 // https://www.zoho.com/crm/help/api/v2/#ra-deleted-records 310 func (c *API) ListDeletedRecords( 311 module Module, 312 kind DeletedRecordsType, 313 params map[string]zoho.Parameter, 314 ) (data ListDeletedRecordsResponse, err error) { 315 endpoint := zoho.Endpoint{ 316 Name: "records", 317 URL: fmt.Sprintf("https://www.zohoapis.%s/crm/v2/%s/deleted", c.ZohoTLD, module), 318 Method: zoho.HTTPGet, 319 ResponseData: &ListDeletedRecordsResponse{}, 320 URLParameters: map[string]zoho.Parameter{ 321 "type": zoho.Parameter(kind), 322 "page": "", 323 "per_page": "200", 324 }, 325 } 326 327 for k, v := range params { 328 endpoint.URLParameters[k] = v 329 } 330 331 err = c.Zoho.HTTPRequest(&endpoint) 332 if err != nil { 333 return ListDeletedRecordsResponse{}, fmt.Errorf( 334 "Failed to insert records of %s: %s", 335 module, 336 err, 337 ) 338 } 339 340 if v, ok := endpoint.ResponseData.(*ListDeletedRecordsResponse); ok { 341 return *v, nil 342 } 343 344 return ListDeletedRecordsResponse{}, fmt.Errorf("Data returned was nil") 345 } 346 347 // DeletedRecordsType is a type for filtering deleted records 348 type DeletedRecordsType string 349 350 const ( 351 // AllDeleted - To get the list of all deleted records 352 AllDeleted DeletedRecordsType = "All" 353 // RecycledDeleted - To get the list of deleted records from recycle bin 354 RecycledDeleted DeletedRecordsType = "Recycle" 355 // PermanentDeleted - To get the list of permanently deleted records 356 PermanentDeleted DeletedRecordsType = "Permanent" 357 ) 358 359 // ListDeletedRecordsResponse is the data returned by ListDeletedRecords 360 type ListDeletedRecordsResponse struct { 361 Data []struct { 362 DeletedBy struct { 363 Name string `json:"name,omitempty"` 364 ID string `json:"id,omitempty"` 365 } `json:"deleted_by,omitempty"` 366 ID string `json:"id,omitempty"` 367 DisplayName string `json:"display_name,omitempty"` 368 Type string `json:"type,omitempty"` 369 CreatedBy struct { 370 Name string `json:"name,omitempty"` 371 ID string `json:"id,omitempty"` 372 } `json:"created_by,omitempty"` 373 DeletedTime time.Time `json:"deleted_time,omitempty"` 374 } `json:"data,omitempty"` 375 Info PageInfo `json:"info,omitempty"` 376 } 377 378 // SearchRecords is used for searching records in the specified module using the parameters. 379 // Parameters are 'criteria', 'email', 'phone', and 'word' 380 // https://www.zoho.com/crm/help/api/v2/#ra-search-records 381 func (c *API) SearchRecords( 382 response interface{}, 383 module Module, 384 params map[string]zoho.Parameter, 385 ) (data interface{}, err error) { 386 endpoint := zoho.Endpoint{ 387 Name: "records", 388 URL: fmt.Sprintf("https://www.zohoapis.%s/crm/v2/%s/search", c.ZohoTLD, module), 389 Method: zoho.HTTPGet, 390 ResponseData: response, 391 URLParameters: map[string]zoho.Parameter{ 392 "criteria": "", 393 "email": "", 394 "phone": "", 395 "word": "", 396 "page": "", 397 "per_page": "200", 398 }, 399 } 400 401 for k, v := range params { 402 endpoint.URLParameters[k] = v 403 } 404 405 err = c.Zoho.HTTPRequest(&endpoint) 406 if err != nil { 407 return nil, fmt.Errorf("Failed to insert records of %s: %s", module, err) 408 } 409 410 if endpoint.ResponseData != nil { 411 return endpoint.ResponseData, nil 412 } 413 414 return nil, fmt.Errorf("Data returned was nil") 415 } 416 417 // GetRecord will retrieve the specified record by id in the specified module. 418 // https://www.zoho.com/crm/help/api/v2/#single-records 419 func (c *API) GetRecord( 420 request interface{}, 421 module Module, 422 ID string, 423 ) (data interface{}, err error) { 424 endpoint := zoho.Endpoint{ 425 Name: "records", 426 URL: fmt.Sprintf("https://www.zohoapis.%s/crm/v2/%s/%s", c.ZohoTLD, module, ID), 427 Method: zoho.HTTPGet, 428 ResponseData: request, 429 } 430 431 err = c.Zoho.HTTPRequest(&endpoint) 432 if err != nil { 433 return nil, fmt.Errorf("Failed to retrieve blueprint: %s", err) 434 } 435 436 if endpoint.ResponseData != nil { 437 return endpoint.ResponseData, nil 438 } 439 440 return nil, fmt.Errorf("Data returned was nil") 441 } 442 443 // InsertRecord will insert the specifed record in the module 444 // https://www.zoho.com/crm/help/api/v2/#create-specify-records 445 func (c *API) InsertRecord( 446 request InsertRecordData, 447 module Module, 448 ) (data InsertRecordResponse, err error) { 449 endpoint := zoho.Endpoint{ 450 Name: "records", 451 URL: fmt.Sprintf("https://www.zohoapis.%s/crm/v2/%s", c.ZohoTLD, module), 452 Method: zoho.HTTPPost, 453 ResponseData: &InsertRecordResponse{}, 454 RequestBody: request, 455 } 456 457 err = c.Zoho.HTTPRequest(&endpoint) 458 if err != nil { 459 return InsertRecordResponse{}, fmt.Errorf("Failed to insert records of %s: %s", module, err) 460 } 461 462 if v, ok := endpoint.ResponseData.(*InsertRecordResponse); ok { 463 return *v, nil 464 } 465 466 return InsertRecordResponse{}, fmt.Errorf("Data returned was nil") 467 } 468 469 // InsertRecordData is the data provided to InsertRecord 470 type InsertRecordData = InsertRecordsData 471 472 // InsertRecordResponse is the data returned by InsertRecord 473 type InsertRecordResponse = InsertRecordsResponse 474 475 // UpdateRecord will update the record specified by ID in the specified module 476 // https://www.zoho.com/crm/help/api/v2/#update-specify-records 477 func (c *API) UpdateRecord( 478 request UpdateRecordData, 479 module Module, 480 ID string, 481 ) (data UpdateRecordResponse, err error) { 482 endpoint := zoho.Endpoint{ 483 Name: "records", 484 URL: fmt.Sprintf("https://www.zohoapis.%s/crm/v2/%s/%s", c.ZohoTLD, module, ID), 485 Method: zoho.HTTPPut, 486 ResponseData: &UpdateRecordResponse{}, 487 RequestBody: request, 488 } 489 490 err = c.Zoho.HTTPRequest(&endpoint) 491 if err != nil { 492 return UpdateRecordResponse{}, fmt.Errorf("Failed to insert records of %s: %s", module, err) 493 } 494 495 if v, ok := endpoint.ResponseData.(*UpdateRecordResponse); ok { 496 return *v, nil 497 } 498 499 return UpdateRecordResponse{}, fmt.Errorf("Data returned was nil") 500 } 501 502 // UpdateRecordData is the data provided to UpdateRecord 503 type UpdateRecordData = InsertRecordsData 504 505 // UpdateRecordResponse is the data returned by UpdateRecord 506 type UpdateRecordResponse = UpdateRecordsResponse 507 508 // DeleteRecord will delete the record specified by the id in the specified module 509 // https://www.zoho.com/crm/help/api/v2/#delete-specify-records 510 func (c *API) DeleteRecord(module Module, ID string) (data DeleteRecordResponse, err error) { 511 endpoint := zoho.Endpoint{ 512 Name: "records", 513 URL: fmt.Sprintf("https://www.zohoapis.%s/crm/v2/%s/%s", c.ZohoTLD, module, ID), 514 Method: zoho.HTTPDelete, 515 ResponseData: &DeleteRecordResponse{}, 516 } 517 518 err = c.Zoho.HTTPRequest(&endpoint) 519 if err != nil { 520 return DeleteRecordResponse{}, fmt.Errorf("Failed to insert records of %s: %s", module, err) 521 } 522 523 if v, ok := endpoint.ResponseData.(*DeleteRecordResponse); ok { 524 return *v, nil 525 } 526 527 return DeleteRecordResponse{}, fmt.Errorf("Data returned was nil") 528 } 529 530 // DeleteRecordResponse is the data returned by DeleteRecord 531 type DeleteRecordResponse = DeleteRecordsResponse 532 533 // ConvertLead will modify the Lead record specified by ID and convert it to a Contact/Potential depending on the request data 534 // https://www.zoho.com/crm/help/api/v2/#convert-lead 535 func (c *API) ConvertLead( 536 request ConvertLeadData, 537 ID string, 538 ) (data ConvertLeadResponse, err error) { 539 endpoint := zoho.Endpoint{ 540 Name: "records", 541 URL: fmt.Sprintf( 542 "https://www.zohoapis.%s/crm/v2/%s/%s/actions/convert", 543 c.ZohoTLD, 544 LeadsModule, 545 ID, 546 ), 547 Method: zoho.HTTPPost, 548 ResponseData: &ConvertLeadResponse{}, 549 RequestBody: request, 550 } 551 552 err = c.Zoho.HTTPRequest(&endpoint) 553 if err != nil { 554 return ConvertLeadResponse{}, fmt.Errorf( 555 "Failed to insert records of %s: %s", 556 LeadsModule, 557 err, 558 ) 559 } 560 561 if v, ok := endpoint.ResponseData.(*ConvertLeadResponse); ok { 562 return *v, nil 563 } 564 565 return ConvertLeadResponse{}, fmt.Errorf("Data returned was nil") 566 } 567 568 // ConvertLeadData is the data provided to ConvertLead 569 type ConvertLeadData struct { 570 Data []struct { 571 Overwrite bool `json:"overwrite,omitempty"` 572 NotifyLeadOwner bool `json:"notify_lead_owner,omitempty"` 573 NotifyNewEntityOwner bool `json:"notify_new_entity_owner,omitempty"` 574 Accounts string `json:"Accounts,omitempty"` 575 Contacts string `json:"Contacts,omitempty"` 576 AssignTo string `json:"assign_to,omitempty"` 577 Deals struct { 578 CampaignSource string `json:"Campaign_Source,omitempty"` 579 DealName string `json:"Deal_Name,omitempty"` 580 ClosingDate string `json:"Closing_Date,omitempty"` 581 Stage string `json:"Stage,omitempty"` 582 Amount float64 `json:"Amount,omitempty"` 583 } `json:"Deals,omitempty"` 584 } `json:"data,omitempty"` 585 } 586 587 // ConvertLeadResponse is the data returned by ConvertLead 588 type ConvertLeadResponse struct { 589 Data []struct { 590 Contacts string `json:"Contacts,omitempty"` 591 Deals string `json:"Deals,omitempty"` 592 Accounts string `json:"Accounts,omitempty"` 593 } `json:"data,omitempty"` 594 }