function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dan BroussardDan Broussard 

Validation rules for Trailhead Quiz

I am working on a Trailhead Quiz for Formulas & Validations Creating Validation Rules
https://developer.salesforce.com/trailhead/point_click_business_logic/validation_rules
The quiz wants to validate a zip code rule for a contact to match zip code with account UNLESS this a a contact with no account associated.
Error from Trailhead that I receive is: The validation rule failed to enforce the business logic.
My "logic" was that if accountid in contact is blank there is no account associated ; if not blank check against account zip. A blank accountid is a NOTTRUE result for the first condition and the second condition is null. An accountid is NOT blank, a TRUE and second condition would be testd.
My validation code is: 
AND (
NOT(ISBLANK( AccountId )), 
(MailingPostalCode  <> Account.ShippingPostalCode)
Any help is appreciated. I am used to the more classical (IF( TRUE) AND IF (TRUE)) ELSE....format.
Thank You!
Best Answer chosen by Dan Broussard
Amit Chaudhary 8Amit Chaudhary 8
Hi Dan Broussard,

Please try below validation rule. I got all 500 Point with below validation rule
AND( 
NOT(ISBLANK(AccountId)), 
MailingPostalCode != Account.ShippingPostalCode 
)
Please mark this as solution is this will help you

Thanks
Amit Chaudhary
amit.salesforce21@gmail.com

 

All Answers

SarfarajSarfaraj
This is the validation rule you need,
NOT(ISBLANK(AccountId)) && Account.ShippingPostalCode != MailingPostalCode
--Akram
SarfarajSarfaraj
On a second thought, your validation rule is same as mine. Have you verified that the name of the validation rule is 'Contact must be in Account ZIP Code', the rule is active. Also you have missed a bracket, it should be,
AND (
NOT(ISBLANK( AccountId )), 
(MailingPostalCode  <> Account.ShippingPostalCode))
Amit Chaudhary 8Amit Chaudhary 8
Hi Dan Broussard,

Please try below validation rule. I got all 500 Point with below validation rule
AND( 
NOT(ISBLANK(AccountId)), 
MailingPostalCode != Account.ShippingPostalCode 
)
Please mark this as solution is this will help you

Thanks
Amit Chaudhary
amit.salesforce21@gmail.com

 
This was selected as the best answer
Dan BroussardDan Broussard
Amit and Akram,
Thank you all for taking the time to help me with this puzzle.
I tried all of your answers and I still received the same error. I did turn activate on and off to see if that would generate a different error. Same error message. I also used the insert field pull down which added the _c to field names; that generated same error when I used all of your and my code. I believe that all of our code is correct, all pased syntax checks and seem true logic to me. I just do not know what answer the quiz wants. 
If any ideas come to mind, please let me know. It has got to be something I am doing that is really simple and dumb!
Dan BroussardDan Broussard
Amit and Akram,
I rebooted my system, went back to SFDC, went back to the quiz, and it passed!
Go figure!
Robert RobinsonRobert Robinson

Thanks guys...

I was going nuts...I had all the right fields; my syntax just sucked. Thanks for the correction.

Andrew R KaedingAndrew R Kaeding
One thing I've noticed in the responses here is that they are expressing "not equal" in two ways.

I've seen: 
!=

and:
<>

The formula expression dropdown lists it as:
<>

Not sure if Salesforce will care about that or not...but if you're running into problems that's a place to check!
Joe GilliganJoe Gilligan
I scored 500 points as well, but with the following validation, which I believe is incorrect as it ignores matching the Mailing and Shipping codes.
I tested it by setting up a new contact and and the test confirmed that it is not matching the business case. The rule I used was:

AND(
NOT(ISBLANK(Account.Name)),
NOT(MailingPostalCode__c <> Account.ShippingPostalCode__c)
)

My previous attempt of the validation, successfully passed my test of the business case but failed Trailheads challenge. That rule was

AND(
NOT(ISBLANK(Account.Name)),
(MailingPostalCode__c <> Account.ShippingPostalCode__c)
)

Any feedback welcome.
 
Paul ErlingPaul Erling
Why wouldn't

ISBLANK(Account.Name) II ((MailingPostalCode__c <> Account.ShippingPostalCode__c)

work equally well?

I.e. if the account.Name is blank, then a contact isn't tied to an account, so MailingPostalCode can be anything.  Alternately, any case where the two postal codes match works. 

There is some kind of bug here, since it isn't consistently displaying Account.PostalCode fields in my system. They show up when you do Add Field, but they don't show up if you open up Accounts and look at the list of fields there.

It isn't what the challenge is looking for, but is there any case where it wouldn't produce the same outcome?
Ellen HEllen H
I had trouble with this as well. I wrote the code multiple ways, wrote it our on paper to check my logic, then finally came to the forums to find I was not alone. I passed with the code below, which I believe is incorrect.

AND( 
    NOT( ISBLANK( Account.Id ) ),   
   Account.ShippingPostalCode__c  =  MailingPostalCode__c 
)

Here is how I translate that into English:

If all the following are true, return a validation error:
     1. The account Id of the contact is not blank  
          AND
     2. The account Shipping Postal Code matches the Contact Mailing Postal Code.

The challange states that the Account shipping postal code and Contact Miling Postal code should not match to return a validation error. So I passed, but with what I think is incorrect business logic. On the upside, having to sort through that problem made me learn more about writing formulas than I've ever tried to before. So Trailhead still rocks!
Savita NarukaSavita Naruka
AND(
2NOT(ISBLANK(AccountId)),
3MailingPostalCode != Account.ShippingPostalCode
4)
Rachit AgrawalRachit Agrawal
Try this in the Contact object, worked for me
(( Account.ShippingPostalCode <> MailingPostalCode ) && NOT(ISBLANK( AccountId )))