Skip to content

Commit

Permalink
Merge pull request #385 from heroku/field_create_label
Browse files Browse the repository at this point in the history
Field create label
  • Loading branch information
dcarroll authored Mar 8, 2017
2 parents 3a69c2d + 509f8ba commit adaf6b8
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ test
*.json

*.xml
*.exe
*.exe
*.sh
23 changes: 23 additions & 0 deletions force.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type ForceCredentials struct {
Namespace string
ApiVersion string
RefreshToken string
ProfileId string
ForceEndpoint ForceEndpoint
IsHourly bool
HourlyCheck bool
Expand Down Expand Up @@ -1087,6 +1088,28 @@ func (f *Force) RetrieveBulkBatchResults(jobId string, batchId string) (results
return
}

func (f *Force) SetFLS(profileId string, objectName string, fieldName string) {
// First, write out a file to a temporary location with a package.xml

f.Metadata.UpdateFLSOnProfile(objectName, fieldName)
}

func (f *Force) QueryProfile(fields ...string) (results ForceQueryResult, err error) {

url := fmt.Sprintf("%s/services/data/%s/tooling/query?q=Select+%s+From+Profile+Where+Id='%s'",
f.Credentials.InstanceUrl,
apiVersion,
strings.Join(fields, ","),
f.Credentials.ProfileId)

body, err := f.httpGet(url)
if err != nil {
return
}
json.Unmarshal(body, &results)
return
}

func (f *Force) QueryTraceFlags() (results ForceQueryResult, err error) {
url := fmt.Sprintf("%s/services/data/%s/tooling/query/?q=Select+Id,+DebugLevel.DeveloperName,++ApexCode,+ApexProfiling,+Callout,+CreatedDate,+Database,+ExpirationDate,+System,+TracedEntity.Name,+Validation,+Visualforce,+Workflow+From+TraceFlag+Order+By+ExpirationDate,TracedEntity.Name", f.Credentials.InstanceUrl, apiVersion)
body, err := f.httpGet(url)
Expand Down
1 change: 1 addition & 0 deletions login.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func ForceSaveLogin(creds ForceCredentials) (username string, err error) {
}
fmt.Printf("Logged in as '%s' (API %s)\n", me["Username"], apiVersion)
title := fmt.Sprintf("\033];%s\007", me["Username"])
creds.ProfileId = fmt.Sprintf("%s", me["ProfileId"])
fmt.Printf(title)

describe, err := force.Metadata.DescribeMetadata()
Expand Down
84 changes: 84 additions & 0 deletions metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ type AutoNumberFieldRequired struct {
}

type AutoNumberField struct {
Label string `xml:"label"`
StartingNumber int `xml:"startingNumber"`
DisplayFormat string `xml:"displayFormat"`
Description string `xml:"description"`
Expand All @@ -233,6 +234,7 @@ type FloatFieldRequired struct {
}

type FloatField struct {
Label string `xml:"label"`
Description string `xml:"description"`
HelpText string `xml:"helpText"`
Unique bool `xml:"unique"`
Expand All @@ -250,6 +252,7 @@ type NumberFieldRequired struct {
}

type NumberField struct {
Label string `xml:"label"`
Description string `xml:"description"`
HelpText string `xml:"helpText"`
Unique bool `xml:"unique"`
Expand All @@ -265,6 +268,7 @@ type DatetimeFieldRequired struct {
}

type DatetimeField struct {
Label string `xml:"label"`
Description string `xml:"description"`
HelpText string `xml:"helpText"`
DefaultValue time.Time `xml:"defaultValue"`
Expand All @@ -283,6 +287,7 @@ type PicklistFieldRequired struct {
}

type PicklistField struct {
Label string `xml:"label"`
Picklist []PicklistValue `xml:"picklist>picklistValues"`
}

Expand All @@ -291,6 +296,7 @@ type BoolFieldRequired struct {
}

type BoolField struct {
Label string `xml:"label"`
Description string `xml:"description"`
HelpText string `xml:"helpText"`
DefaultValue bool `xml:"defaultValue"`
Expand Down Expand Up @@ -412,6 +418,10 @@ type UrlField struct {
type EmailFieldRequired struct {
}

type EmailField struct {
Label string `xml:"label"`
}

type TextAreaFieldRequired struct {
}

Expand Down Expand Up @@ -458,6 +468,7 @@ type RichTextAreaField struct {
type LookupFieldRequired struct{}

type LookupField struct {
Label string `xml:"label"`
ReferenceTo string `xml:"referenceTo"`
RelationshipLabel string `xml:"relationshipLabel"`
RelationshipName string `xml:"relationshipName"`
Expand All @@ -466,6 +477,7 @@ type LookupField struct {
type MasterDetailRequired struct{}

type MasterDetail struct {
Label string `xml:"label"`
ReferenceTo string `xml:"referenceTo"`
RelationshipLabel string `xml:"relationshipLabel"`
RelationshipName string `xml:"relationshipName"`
Expand Down Expand Up @@ -971,6 +983,60 @@ func (fm *ForceMetadata) CreateCustomField(object, field, typ string, options ma
if err = fm.CheckStatus(status.Id); err != nil {
return
}
serr := fm.UpdateFLSOnProfile(object, field)
if serr != nil {
fmt.Println("INFO: Failed to set FLS on new Field (field was created).")
}
return
}

func (fm *ForceMetadata) GetFLSUpdateXML(objectName string, fieldName string) (result string) {
if !strings.HasSuffix(fieldName, "__c") {
fieldName = fieldName + "__c"
}

result = fmt.Sprintf(
`<?xml version="1.0" encoding="UTF-8"?>
<Profile xmlns="http://soap.sforce.com/2006/04/metadata">
<fieldPermissions>
<editable>true</editable>
<field>%s.%s</field>
<readable>true</readable>
</fieldPermissions>
<objectPermissions>
<allowCreate>true</allowCreate>
<allowDelete>true</allowDelete>
<allowEdit>true</allowEdit>
<allowRead>true</allowRead>
<viewAllRecords>false</viewAllRecords>
<modifyAllRecords>false</modifyAllRecords>
<object>%s</object>
</objectPermissions>
</Profile>
`, objectName, fieldName, objectName)
return
}

func (fm *ForceMetadata) UpdateFLSOnProfile(objectName string, fieldName string) (err error) {
res, err := fm.Force.QueryProfile("Id", "Name", "FullName")
profileFullName := fmt.Sprintf("%s", res.Records[0]["FullName"])

/*parts := strings.Split(args[1], ":")
if len(parts) != 2 {
ErrorAndExit("must specify name:type for fields")
}
field := strings.Replace(parts[0], " ", "_", -1) + "__c"
*/

/*if err := force.Metadata.UpdateFLSOnProfile(args[0], field); err != nil {
globalSilencer = "off"
ErrorAndExit(err.Error())
}*/
fm.DeployWithTempFile(
fm.GetFLSUpdateXML(objectName, fieldName),
fmt.Sprintf("%s.profile", profileFullName))
return
}

Expand Down Expand Up @@ -1111,6 +1177,24 @@ func (fm *ForceMetadata) MakeZip(files ForceMetadataFiles) (zipdata []byte, err
return
}

func (fm *ForceMetadata) DeployWithTempFile(soap string, filename string) {
// Create temp file and store the XML for the MD in the file
wd, _ := os.Getwd()
mpath := findMetapathForFile(filename)

tempdir, err := ioutil.TempDir(wd, "md_temp")
tempdir = filepath.Join(tempdir, mpath.path)

if err != nil {
ErrorAndExit(err.Error())
}

os.MkdirAll(tempdir, 0777)
xmlfile := filepath.Join(tempdir, filename)
ioutil.WriteFile(xmlfile, []byte(soap), 0777)
pushByPaths([]string{xmlfile})
}

func (fm *ForceMetadata) Deploy(files ForceMetadataFiles, options ForceDeployOptions) (results ForceCheckDeploymentStatusResult, err error) {
soap := fm.MakeDeploySoap(options)

Expand Down
28 changes: 28 additions & 0 deletions packagebuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type metapath struct {
name string
hasFolder bool
onlyFolder bool
extension string
}

var metapaths = []metapath{
Expand Down Expand Up @@ -86,6 +87,9 @@ var metapaths = []metapath{
metapath{path: "pathAssistants", name: "PathAssistant"},
metapath{path: "permissionsets", name: "PermissionSet"},
metapath{path: "postTemplates", name: "PostTemplate"},
metapath{path: "profiles", name: "Profile", extension: ".profile"},
metapath{path: "postTemplates", name: "PostTemplate"},
metapath{path: "postTemplates", name: "PostTemplate"},
metapath{path: "profiles", name: "Profile"},
metapath{path: "queues", name: "Queue"},
metapath{path: "quickActions", name: "QuickAction"},
Expand Down Expand Up @@ -303,6 +307,30 @@ func getPathForMeta(metaname string) string {
return metaname
}

func findMetapathForFile(file string) (path metapath) {
parentDir := filepath.Dir(file)
parentName := filepath.Base(parentDir)
grandparentName := filepath.Base(filepath.Dir(parentDir))
fileExtension := filepath.Ext(file)

for _, mp := range metapaths {
if mp.hasFolder && grandparentName == mp.path {
return mp
}
if mp.path == parentName {
return mp
}
}

// Hmm, maybe we can use the extension to determine the type
for _, mp := range metapaths {
if mp.extension == fileExtension {
return mp
}
}
return
}

// Gets meta type and name based on a path
func getMetaForPath(path string) (metaName string, objectName string) {
parentDir := filepath.Dir(path)
Expand Down

0 comments on commit adaf6b8

Please sign in to comment.