LoginSignup
2
2

More than 5 years have passed since last update.

Force.com CLIでカスタムフィールドを作成

Last updated at Posted at 2017-03-19

はじめに

SalesForceのAPIをCLIで使うためのツールが提供されています。
公式サイトから微妙に辿り辛い (https://developer.salesforce.com/tools/forcecli) ので、SalesForceを使い始める人に気づきづらいという罠にはまってしまいました...

CLI自体はgo言語で実装されているので、windows,Linuxの双方で使用できます。
https://github.com/heroku/force

カスタムフィールドを作成

たとえば、基幹システムにあるデータを複数のSalesForceインスタンスに渡そうとした場合、そのためのカスタムフィールド、カスタムオブジェクトを、インスタンスの数だけ作成する、といったことはままあると思います。

項目の数が少なければSalesForceのGUIからでもいいですが、入力間違い・漏れもでてくるので、なにがしかで自動化したところです。

Force.com IDE や antで実行するMigration tool でも実現できるのですが、CLIならもうすこし敷居を下げたやり方になりえます。

使い方

インストールはwindows版ならforce.exeを、CドライブのProgram files¥force以下などにいれて、環境変数にそこまでパスを追加すればいいです。

カスタムフィールドを作り時のコマンドの形式は以下になります

force field create [object_name] [my_number_field]:[type] label:[my_label]

# テキスト型
force field create Account sample_text:string label:サンプルテキスト length:12

# 数値型
force field create Account sample_number:number label:サンプルナンバー precision:2
force field create Account sample_double:double label:サンプルダブル precision:2 scale:1

# 日付型
force field create Account sample_date:datetime label:サンプル日付

# 必須項目
force field create Account sample_required:string required:true

force field type で指定できる型と初期値などの情報が以下のように一覧表示されます。


    FIELD                                    DEFAULTS
    =========================================================================
  text/string            (length = 255)
  textarea               (length = 255)
  longtextarea           (length = 32768, visibleLines = 5)
  richtextarea           (length = 32768, visibleLines = 5)
  checkbox/bool/boolean  (defaultValue = false)
  datetime               ()
  email                  ()
  url                    ()
  float/double/currency  (precision = 16, scale = 2)
  number/int             (precision = 18, scale = 0)
  autonumber             (displayFormat = "AN {00000}", startingNumber = 0)
  geolocation            (displayLocationInDecimal = true, scale = 5)
  lookup                 (will be prompted for Object and label)
  masterdetail           (will be prompted for Object and label)
  picklist               ()

以下のように作成した項目の作成コマンドをひとつのシェルスクリプトにまとめておきます。

create_fields.sh
force field create Account sample_text1:string label:サンプルテキスト1 length:10
force field create Account sample_text2:string label:サンプルテキスト2 length:20
force field create Account sample_text3:string label:サンプルテキスト3 length:30 required true

その後は以下のようなことを、インスタンス毎に実行すれば、同じフィールドを複数のインスタンスに作成できます。

force login
← ブラウザが起動するので作成したいインスタンスのアカウントでログイン
./create_fields.sh
force logout

難点なのは、現時点では一回のコマンドで一個のフィールドしかつくれず、それが数秒かかります。API使用回数も使うので、項目が多いとそこそこ時間がかかってしまいます。速さだけなら、force.com IDEやmigration toolの方がいいです。

また、フィールドのアクセス権限はforce loginでログインしたアカウントのプロファイルのみに設定されます。複数のプロファイルにアクセス権限するのはまだ無理のようです。

まとめ

まだ使える場面は限定されているかもしれませんが、force.com cliでqueryも実行できるので、開発の補助としては十分な機能があると思います。

参考サイト

Force.com IDE

Migration tool

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2