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

Create a validation rule to check that a contact is in the zip code of its account.

I am having a hard time figuring this one out. Your suggestions would greatly be appreciated.
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)
Swayam  AroraSwayam Arora
Create a validation rule in Contact 'Contact must be in Account ZIP Code' and
Error Condition Formula:-   MailingPostalCode <> Account.ShippingPostalCode && NOT( ISBLANK( AccountId ) )

Please mark the answer as Best Answer if you find it useful.
Mohit Sachdeva 10Mohit Sachdeva 10
I have got this done, but can u elaborate this a bit , Im still not clear on this!!!
Thanks
Swayam  AroraSwayam Arora
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.
 block the insertion of a contact:- it means you have to write a validation on Contact which will block insertion on a particular condition.

1. if the contact is related to an account:- this statement says Contact should not be inserted if it has an account i.e AccountId should not be null.
Formula:- NOT( ISBLANK( AccountId ) )                       
                                                         AND
2. has a mailing postal code (which has the API Name MailingPostalCode) different from the account's shipping postal code:- if MailingPostalCode of Contact is different from Account ShippingPostalCode
Formula:- MailingPostalCode <> Account.ShippingPostalCode                

I hope the explaination will help you.
Mohit Sachdeva 10Mohit Sachdeva 10
Thanks!! for prompt reply
sfdcdevsfdcdev
Hi William,

Let's take a Look at the problem and then solve it by breaking into smaller components:
  • 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.
           Contact may or may not have associated with the                              Account.Here we need to make sure that Contact has an                  associated Account.In other words,'Account Name' on the                Contact object should not be blank.To check this                              conditon,Salesforce has provided 'ISBLANK' Function:

           ISBLANK(expression)
          Checks whether an expression is blank and returns TRUE or            FALSE

           Account Name should be blank:

           ISBLANK(Account.Name)

           To make the above condition negative,we have 'NOT'                         Function:

            Account Name should not be blank:

            NOT(ISBLANK(Account.Name))
  • 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.
           Account's ShippingPostalCode should not match Contact's              MailingPostalCode.Whenever this condition is true, we have            to give an error to the user.

           Here we get our second part of the formula:
           
           MailingPostalCode <> Account.ShippingPostalCode

           Now,We need to throw a validation error,whenever these two           conditions are true simultaneously.Here 'AND' Function                     comes very handy:

          AND(logical1,logical2,...)
         Checks whether all arguments are true and returns TRUE if all          arguments are true

         AND( 
              (NOT(ISBLANK(Account.Name))), 
              (MailingPostalCode <> Account.ShippingPostalCode) 
          )
NewCoderBoyNewCoderBoy
I tried what is listed below but i get this error:
Challenge not yet complete... here's what's wrong:
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, shipping postal code has to be different: []

can someone tell me where do I begin to look.  I entered this in the validation rule on the contact object:

AND(
              (NOT(ISBLANK(Account.Name))),
              (MailingPostalCode <> Account.ShippingPostalCode)
          )
NewCoderBoyNewCoderBoy
nevermind.  i had a trigger in the way!!
ryg sinryg sin
I am  getting an error "Challenge not yet complete... here's what's wrong: 
The validation rule failed to enforce the business logic"  even though is use the formula "AND( 
              (NOT(ISBLANK( Account.Name ))), 
              ( MailingPostalCode__c  <>  Account.ShippingPostalCode__c ) "
sfdcdevsfdcdev
You have left AND closing bracket-

AND ( 
              (NOT(ISBLANK(Account.Name))), 
              (MailingPostalCode <> Account.ShippingPostalCode) 
          )
William TranWilliam Tran
AND( 
NOT(ISBLANK(AccountId)), 
MailingPostalCode !=Account.ShippingPostalCode)

William, please choose best answer and close this issue.

Thx
Carlos HernandoCarlos Hernando
Hi,

I've tried all of these solutions and none of them work. 

Regards
Rich FiekowskyRich Fiekowsky
I had success with 
AND(
              (NOT(ISBLANK(Account.ShippingPostalCode))),
              (NOT(MailingPostalCode = Account.ShippingPostalCode))
          )
but maybe this would be wrong if : the Contact does have an Account, and that Account has a blank ShippingPostalCode. Since Name is required on the Account record, it seems better to use
AND(
              (NOT(ISBLANK(Account.Name))),
              (NOT(MailingPostalCode = Account.ShippingPostalCode))
          )
But the suggestion by S.Arora worked for me too
AND(
              (NOT(ISBLANK(AccountId))),
              (NOT(MailingPostalCode = Account.ShippingPostalCode))
          )
I prefer to use "Account.Name" because many (all?) objects have a required Name field, so, the technique is easy to remember.
Melinda GentryMelinda Gentry
Thank you, I was pulling my hair out on this one. 
Vincent O'DonnellVincent O'Donnell
I passed the challenge with this code:
 
AND( NOT( ISBLANK( AccountId ) ),
MailingPostalCode <> Account.ShippingPostalCode

However, when I go to 'test' it (I am going into an account record, such as "Edge Communications", zip of 78767, and adding a contact to the account), in BOTH cases I am getting the error, i.e. when I enter the zip 78767, or enter a random zip, the "Contact must be in Account ZIP Code" error comes up! 

What is going on here?  What is the code that will ACTUALLY block it. 

Thanks

 
Manikant prasadManikant prasad
Hi all , 
Please try the following code ,, it must work...


MailingPostalCode <> Account.ShippingPostalCode      &&      NOT( ISBLANK( AccountId ) )
Kapil BhardwajKapil Bhardwaj
Hi Manikant,
It worked perfectly.
Thanks