LoginSignup
12
7

More than 5 years have passed since last update.

【Salesforce初心者】入力規則の作成について

Last updated at Posted at 2017-03-31

Salesforceの練習で、入力チェックのロジックをformulaで書いて見ました。
リクエストは以下の通り:

Trailheadの原文は以下:

To complete this challenge, add a validation rule which will block the insertion of a contact if the contact is related to an account and has a mailing postal code (which has the API Name MailingPostalCode) different from the account's shipping postal code (which has the API Name ShippingPostalCode).

Name the validation rule 'Contact must be in Account ZIP Code'.
A contact with a MailingPostalCode that has an account and does not match the associated Account ShippingPostalCode should return with a validation error and not be inserted.
The validation rule should ONLY apply to contact records with an associated account. Contact records with no associated parent account can be added with any MailingPostalCode value. (Hint: you can use the ISBLANK function for this check)

日本語でいうと、下記のようになります:
概要:ContactsにMailingZipCodeを入力する時のチェックメソッド
①.ContactsにAccountが紐づいてない場合、MailingPostalCodeは何を入力しても良い(特にチェックしない)
②.ContactsにAccountが紐づいている場合、MailingPostalCodeはAccountのShippingPostalCodeに一致しないといけない

ロジックとしては難しくないと思いますが、一個引っかかりやすい「ワナ」(ワナでもないかw)があります。
Salesforceで用意されているValidation(チェックメソッド)は、判定の結果がTrueのときに発動する。
つまり、判定式がTrueの場合、Error Messageを出すと言う仕組みです。
普通のプログラムだと、Trueが正常系で、Falseの場合がエラーを返すというのが一般的な感じがしますね。

では、上記二つの条件を分岐にしました:

分岐1:ContactにAccountが紐づいていない → エラーを出さない(Falseを返す)
分岐2:ContactにAccountが紐づいている、かつ
          Contact.MailingPostalCode=Account.ShippingPostalCode 
          → エラーを出さない(Falseを返す)
分岐3:ContactにAccountが紐づいている、かつ
          Contact.MailingPostalCode≠Account.ShippingPostalCode
          → エラーを出す(Trueを返す)

上記の分岐に基づいて、下記のロジックを組みました:

ロジック1:
IF((ISBLANK(AccountId),false,
IF(MailingPostalCode <> Account.ShippingPostalCode, , )
)

ロジック2:
AND(
NOT (ISBLANK(AccountId)) ,
(MailingPostalCode <> Account.ShippingPostalCode)
)

両方ともテスト済みで機能するので、好きな方を使えばいいと思います。
自分は2の方がいいです^^

12
7
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
12
7