github.com/mhlo/force@v0.22.28-0.20150915022417-6d05ecfb0b47/sobject.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"encoding/xml"
     6  	"fmt"
     7  	"html"
     8  	"io/ioutil"
     9  	"os"
    10  	"strings"
    11  )
    12  
    13  var cmdSobject = &Command{
    14  	Run:   runSobject,
    15  	Usage: "sobject",
    16  	Short: "Manage standard & custom objects",
    17  	Long: `
    18  Manage sobjects
    19  
    20  Usage:
    21  
    22    force sobject list
    23  
    24    force sobject create <object> [<field>:<type> [<option>:<value>]]
    25  
    26    force sobject delete <object>
    27  
    28    force sobject import
    29  
    30  Examples:
    31  
    32    force sobject list
    33  
    34    force sobject create Todo Description:string
    35  
    36    force sobject delete Todo
    37  `,
    38  }
    39  
    40  func runSobject(cmd *Command, args []string) {
    41  	if len(args) == 0 {
    42  		cmd.printUsage()
    43  	} else {
    44  		switch args[0] {
    45  		case "list":
    46  			runSobjectList(args[1:])
    47  		case "create", "add":
    48  			runSobjectCreate(args[1:])
    49  		case "delete", "remove":
    50  			runSobjectDelete(args[1:])
    51  		case "import":
    52  			runSobjectImport(args[1:])
    53  		default:
    54  			ErrorAndExit("no such command: %s", args[0])
    55  		}
    56  	}
    57  }
    58  
    59  func getSobjectList(args []string) (l []ForceSobject) {
    60  	force, _ := ActiveForce()
    61  	sobjects, err := force.ListSobjects()
    62  	if err != nil {
    63  		ErrorAndExit(fmt.Sprintf("ERROR: %s\n", err))
    64  	}
    65  
    66  	for _, sobject := range sobjects {
    67  		if len(args) == 1 {
    68  			if strings.Contains(sobject["name"].(string), args[0]) {
    69  				l = append(l, sobject)
    70  			}
    71  		} else {
    72  			l = append(l, sobject)
    73  		}
    74  	}
    75  	return
    76  }
    77  func runSobjectList(args []string) {
    78  	l := getSobjectList(args)
    79  	DisplayForceSobjects(l)
    80  }
    81  
    82  func runSobjectCreate(args []string) {
    83  	if len(args) < 1 {
    84  		ErrorAndExit("must specify object name")
    85  	}
    86  	force, _ := ActiveForce()
    87  	if err := force.Metadata.CreateCustomObject(args[0]); err != nil {
    88  		ErrorAndExit(err.Error())
    89  	}
    90  	fmt.Println("Custom object created")
    91  
    92  	if len(args) > 1 {
    93  		args[0] = fmt.Sprintf("%s__c", args[0])
    94  		runFieldCreate(args)
    95  	}
    96  }
    97  
    98  func runSobjectDelete(args []string) {
    99  	if len(args) < 1 {
   100  		ErrorAndExit("must specify object")
   101  	}
   102  	force, _ := ActiveForce()
   103  	if err := force.Metadata.DeleteCustomObject(args[0]); err != nil {
   104  		ErrorAndExit(err.Error())
   105  	}
   106  	fmt.Println("Custom object deleted")
   107  }
   108  
   109  func runSobjectImport(args []string) {
   110  	var objectDef = `
   111  <cmd:sObjects>
   112  	<cmd:type>%s</cmd:type>
   113  %s</cmd:sObjects>`
   114  
   115  	// Need to read the file into a query result structure
   116  	data, err := ioutil.ReadAll(os.Stdin)
   117  
   118  	var query ForceQueryResult
   119  	json.Unmarshal(data, &query)
   120  	if err != nil {
   121  		ErrorAndExit(err.Error())
   122  	}
   123  
   124  	var soapMsg = ""
   125  	var objectType = ""
   126  	for _, record := range query.Records {
   127  		var fields = ""
   128  		for key, _ := range record {
   129  			if key == "Id" {
   130  				continue
   131  			} else if key == "attributes" {
   132  				x := record[key].(map[string]interface{})
   133  				val, ok := x["type"]
   134  				if ok {
   135  					objectType, ok = val.(string)
   136  				}
   137  			} else {
   138  				if record[key] != nil {
   139  					val, ok := record[key].(string)
   140  					if ok {
   141  						fields += fmt.Sprintf("\t<%s>%s</%s>\n", key, html.EscapeString(val), key)
   142  					} else {
   143  						valf, ok := record[key].(float64)
   144  						if ok {
   145  							fields += fmt.Sprintf("\t<%s>%f</%s>\n", key, valf, key)
   146  						} else {
   147  							fields += fmt.Sprintf("\t<%s>%s</%s>\n", key, record[key].(string), key)
   148  						}
   149  					}
   150  				}
   151  			}
   152  		}
   153  		soapMsg += fmt.Sprintf(objectDef, objectType, fields)
   154  	}
   155  
   156  	force, _ := ActiveForce()
   157  	response, err := force.Partner.soapExecuteCore("create", soapMsg)
   158  
   159  	type errorData struct {
   160  		Fields     string `xml:"field"`
   161  		Message    string `xml:"message"`
   162  		StatusCode string `xml:"statusCode"`
   163  	}
   164  
   165  	type result struct {
   166  		Id      string      `xml:"id"`
   167  		Success bool        `xml:"success"`
   168  		Errors  []errorData `xml:"errors"`
   169  	}
   170  
   171  	var xmlresponse struct {
   172  		Results []result `xml:"Body>createResponse>result"`
   173  	}
   174  
   175  	xml.Unmarshal(response, &xmlresponse)
   176  
   177  	for i, res := range xmlresponse.Results {
   178  		if res.Success {
   179  			fmt.Printf("%s created successfully\n", res.Id)
   180  		} else {
   181  			for _, e := range res.Errors {
   182  				fmt.Printf("%s\n\t%s\n%s\n", e.StatusCode, e.Message, query.Records[i])
   183  			}
   184  		}
   185  	}
   186  }