+ Start a Discussion
Mike HelvyMike Helvy 

Validation Rule Challenge :The validation rule failed to enforce the business logic

This challenge seems simple enough but I'm stuck and any help would be appreciated. 

So the Challenge is Create a validation rule to check that a contact is in the zip code of its account. here is the question below:
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 (hint: you can use the ISBLANK function for this check) can be added with any MailingPostalCode value

Here is my  work.. Any help would be appreciated.. thanks, 

Rule Name Contact_must_be_in_Account_ZIP_Code Active [Checked]
Error Condition Formula AND( BillingPostalCode = ShippingPostalCode )
Error Message Billing zipcode does not match the Shipping Zipcode Error Location Billing Zip/Postal Code



 
Best Answer chosen by Mike Helvy
Frédéric TrébuchetFrédéric Trébuchet
Hi Michael,

I can't see your formula code.
Never mind, because you've used an "AND", your formula should have 2 conditions:
- 1st one have to verify if "AccoundId" is populated (The validation rule should ONLY apply to contact records with an associated account).
- 2nd one have to verify the equality between "MailingPostalCode" and "Account.ShippingPostalCode".

Your condition should looks like this:
AND( 
NOT( ISBLANK( AccountId ) ), 
MailingPostalCode <> Account.ShippingPostalCode 
)
Hope this helps,
Fred

All Answers

Frédéric TrébuchetFrédéric Trébuchet
Hi Michael,

I can't see your formula code.
Never mind, because you've used an "AND", your formula should have 2 conditions:
- 1st one have to verify if "AccoundId" is populated (The validation rule should ONLY apply to contact records with an associated account).
- 2nd one have to verify the equality between "MailingPostalCode" and "Account.ShippingPostalCode".

Your condition should looks like this:
AND( 
NOT( ISBLANK( AccountId ) ), 
MailingPostalCode <> Account.ShippingPostalCode 
)
Hope this helps,
Fred
This was selected as the best answer
Mike HelvyMike Helvy
Got it Thanks!
SattibabuSattibabu
Hi Frédéric Trébuchet .. Thanks for your simple way of soliving the challenge.
Frédéric TrébuchetFrédéric Trébuchet
You're welcome
Gaurav Jain 67Gaurav Jain 67
Hi Frédéric Trébuchet,

I also received the same issue but my Error condition was as following:

NOT( ISBLANK( AccountId ) )
&&
MailingPostalCode <> Account.ShippingPostalCode

What is the difference between the error condition you mentioned and the one i used ? I am also using && expression between both my conditions.. ?
 
Frédéric TrébuchetFrédéric Trébuchet
Hi Gurav,

Not sure but I think the formula can only contain one expression.
So, if you have a combined condition, you have to write something like that:
AND(expression1, expression2, ...)
Hope this helps,
Fred
CNishantCNishant
for above requirement Validation Rule can be writtem in multiple ways 
One of the simplest solution is:
AND( ISNEW() , AccountId <> null, MailingPostalCode <> Account.ShippingPostalCode )
Jarrett LichtensteinJarrett Lichtenstein
This one works for me. However, it is billing zip code, the MailingPostalCode API does not exisit. I have tried to create the field and the syntax says field does not exist. 
Satyanarayana PusuluriSatyanarayana Pusuluri
Hi,

It's working fine for this Challenge with below code

User-added image

Thanks & Regards,
Satya P
Andrew Mitchell 14Andrew Mitchell 14

This was so complicated. Anytime i tried to put in the MailingPostalCode it said it didnt exist. Any time i tried to put the account. before anything it said it didnt exist. I finally just wrote it myself.

 

AND(( BillingPostalCode <> ShippingPostalCode),
NOT(ISBLANK(ShippingPostalCode)))

 

And it worked perfectly, Challenge Accepted it

Andrew Mitchell 14Andrew Mitchell 14
Had someone clarify. Because i was in the Account Object and not the Contact object to begin with referencing it caused errors in the code.
Nejib EssouriNejib Essouri
Hi guys, I wrote the same code but it doesn't work for me. Help please..
User-added image
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: Delete failed. First exception on row 0 with id 0032400000IrrmOAAR; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, MadMimi.onDeleteContact: execution of BeforeDelete caused by: System.NullPointerException: Argument cannot be null. (System Code) : []
Frédéric TrébuchetFrédéric Trébuchet
Hi Nejib,

Replace "Account.Id" by "AccountId" (remove the dot) as you must refer to the Contact's field.
It should be ok.

Regards,
Fred
Nejib EssouriNejib Essouri
Hi Frédéric,
Even if I change the field "Account.Id" with "AccountId", the same error appears.
Kapil Bhatia 8Kapil Bhatia 8
The suggested code doesn't work - what am I doing wrong?

AND(
NOT( ISBLANK( AccountId ) ),
MailingPostalCode <> Account.ShippingPostalCode
)


Error message- 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, Mailing and Shipping postal code don't match: [AccountId]
Ken Lynch 1Ken Lynch 1
Kapil,

Here's how I got passed this:
Turn on logging and focus on the last test run.

MailingPostalCode needs to be something.






 
Paul OsnesPaul Osnes
In my case the Challenge was attempting to use a null value for Account.ShippingPostalCode that prevented the answers above from working.  It seems like the error message "Insert Failed. First Exception.....blah blah" is caused by trying to post a null value instead of a blank one.  I managed to work around it by testing for a blank account.shippingpostalcode (oddly testing for a null did'nt work) like so:

User-added image

I found it using the debug log as you can see here in case you get the same message:
User-added image
 
Tejas GawaliTejas Gawali
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, error please enter appropriate zip code: [OtherPostalCode]



help me in this 
Ken Lynch 1Ken Lynch 1
The question states "has a mailing postal code ...". They are testing with a null value. Make sure you handle this correctly. Create a debug log to see the error more clearly.
Srishti GoyalSrishti Goyal
I created the below formula which satisfied the following conditions- (1) Inserting a new Contact record (2) Contact is associated with an Account (3) Contact has a mailing postal code (4) Contact's MailingPostalCode does not match with the associated Account's ShippingPostalCode. Although this fulfils the requirement but I was still getting the error. I wonder why?!!!

AND(ISNEW(),NOT(ISBLANK(Account.Id)),NOT(ISBLANK(MailingPostalCode)), MailingPostalCode<> Account.ShippingPostalCode)
Although this 

However it worked for me when I gave the below formula.
AND(NOT(ISBLANK(Account.Id)),MailingPostalCode<> Account.ShippingPostalCode)
Biks07Biks07
Below one worked for me. :)
AND(NOT(ISBLANK(AccountId)),MailingPostalCode<> Account.ShippingPostalCode)
Izaaz GaffarIzaaz Gaffar
Try this.

NOT(ISBLANK(AccountId)) && 
MailingPostalCode<>Account.ShippingPostalCode
Ryan Wheeler 10Ryan Wheeler 10
I tried every suggesstion on this feed and nothing worked. Do you think I have some settings on that dont allow the business logic to execute?
Benjy HuntBenjy Hunt
I had the same error, but my code was correct, i logged out of my dev edition and trailhead logged back into both and then the check went through sucessfully.
SBisht08SBisht08
Did you activate the rule..????
John MatthewsJohn Matthews
'Challenge Not yet complete... here's what's wrong:
The validation rule does not reference the account Shipping Postal Code field (ShippingPostalCode)'

using this code:

AND(NOT(ISBLANK(Account.Id)),MailingPostalCode<> Account.ShippingPostalCode)

I thought that 'account.ShippingPostalCode' references this?
 
pragyanshu kukretipragyanshu kukreti
yes this should work !! 
AND(MailingPostalCode    <>     Account.ShippingPostalCode, NOT(ISBLANK(  Account.Name )))
Samuel ThomasSamuel Thomas
I tried many ways. But no difference in error "Challenge Not yet complete... here's what's wrong: 
The validation rule failed to enforce the business logic";

I have created this Validation Rule under Standard "Account" Object.

Could someone help me on this, where i did the mistake or have to create this rule in any other specific object?

Advance Thanks!

User-added image
John MatthewsJohn Matthews
I managed to pass this challenge. I was using the same code as I used before ony difference is I deleted any other validation rules from my developer edition in case they were conflicting.
Samuel ThomasSamuel Thomas
Thanks John. I didn't find anyother Rules under Account Object. If i am looking for wrong rule, could you please help me to locate the rules from developer edition. so that i can remove that rule & try it. Thanks!
Sahil NandaSahil Nanda
AND(NOT(ISBLANK(AccountId)),MailingPostalCode<> Account.ShippingPostalCode)
This works perfectly fine.
Andrew Mitchell 14Andrew Mitchell 14
All Fixed up thanks!
Evelien MeuldersEvelien Meulders
Thanks for some ideas/helps for the syntax. This was not easy. :-) 
Mark Roddy 7Mark Roddy 7
I'd like to add the point that if you have the excercise were you used the Process Builder to modify contacts you may be getting the Failed insert error.  Go in to Process Builder and deactivate the process you have if you are receiving that error and try again. 
Bella Patel 14Bella Patel 14
This worked for me:

AND( 
BillingPostalCode <> ShippingPostalCode, 
NOT(ISBLANK( Name )) 
)
Natia Iremashvili NIRNatia Iremashvili NIR
Hi,
I tried many ways. But no difference in error : Challenge Not yet complete... here's what's wrong: 
A contact with no parent account could not be inserted. The validation rule should only apply to contact records with an associated account.

I tried every suggesstion on this feed and nothing worked. Do you think I have some settings on that dont allow the business logic to execute?
(ISBLANK(Account.Id)) 
|| 
AND( 
NOT( ISBLANK( AccountId ) ) , 
NOT(ISBLANK( MailingPostalCode )), 
(MailingPostalCode <> Account.ShippingPostalCode) 
)

Thanks in advance for your help
 
Donald LapinDonald Lapin
Sorry to wake up a sleeping thread.

Paul O. had the solution which worked for me,

  AND( 
    NOT(ISBLANK(AccountId)), 
    NOT(ISBLANK(Account.ShippingPostalCode)), 
    MailingPostalCode<>Account.ShippingPostalCode 
  )

But I tried other solutions on this and other advice pages which failed, such as:

  AND(NOT(ISBLANK(Account.Id)),MailingPostalCode<> Account.ShippingPostalCode)

and variations such as:

  AND(NOT(ISBLANK(Account.ShippingPostalCode)),MailingPostalCode<> Account.ShippingPostalCode)

It seems to me that both of these should work --??
Maarten MeijerMaarten Meijer
For future users, I did it steps. So first created the AND part of this formula and later added an IF statement.

IF( AND(MailingPostalCode <> Account.ShippingPostalCode,  ISBLANK( Account.ShippingPostalCode ) ),False, True)

Alos used the insert buttons to avoid small typo's.
Jayapriya SomuJayapriya Somu
@Maarten, Following ur IF statement, it worked finally. It didnt work for me without the IF. What difference does the IF make ? Please clarify.
Ian RashkinIan Rashkin
Wow, this seems convoluted. I used this formula:
ISBLANK( Account.Id ) == FALSE && ISBLANK(MailingPostalCode) == false && MailingPostalCode <> Account.ShippingPostalCode
And in my own tests, it works fine.Note that the instructions require "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 above examples do not check for MailingPostalCode, as far as I can tell. Note too that the create contact form - at least in my instance - does not even acccept an address on insert, only after, on update, so I am only able to full test on update, not insert.

At any rate, I am able to add and edit contacts with correct blocking only when I have both an account and mailing address, and their postal codes don't match. But, trailhead tells me that "The validation rule failed to enforce the business logic".

So, I changed to the logic given in Paul Osnes' answer, and it works the same in real life, but now the challenge passes too.
AND( 
    NOT(ISBLANK(AccountId)), 
    NOT(ISBLANK(Account.ShippingPostalCode)), 
    MailingPostalCode<>Account.ShippingPostalCode 
  )
I suppose that means that the challenge is looking for what it thinks is the right answer, not actually testing that the logic works? Sorry to tag a question onto another - it's really more for benefit of others in my boat, though I'd be able for confirmation that this is what is going on.
 
Rex MorrowRex Morrow
Okay. This challenge is tough. I used the formula Ian Rashikin had posted and I am still unable to pass the challenge. I get this message everytime: 

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: REQUIRED_FIELD_MISSING, Required fields are missing: [Start__c, Property__c]: [Start__c, Property__c]

I need help fast please.
Mujahid Islam KhanMujahid Islam Khan

Error Condition Formula:

AND(
NOT( ISBLANK( AccountId ) ),
MailingPostalCode <> Account.ShippingPostalCode
)


I am getting below error:
Challenge Not yet complete... here's what's wrong:
The validation rule failed to enforce the business logic
Sumeet Das 8Sumeet Das 8
I tried this formula as get some other error
 
NOT(ISBLANK(Account.Id )) && (MailingPostalCode <> Account.ShippingPostalCode)

Challenge Not yet complete... here's what's wrong: 
The validation rule was not found. Make sure that you created a Validation rule with the name 'Contact must be in Account ZIP Code'
rajyalakshmi jampanirajyalakshmi jampani
Please check if any Trigger is active for fields involved in the validation rule, then try below code. It worked for me.

AND(
NOT(ISBLANK(AccountId)),
MailingPostalCode != Account.ShippingPostalCode
)
rajyalakshmi jampanirajyalakshmi jampani
Please check if any Trigger is active for fields involved in the validation rule, then try below code. It worked for me.

AND(
NOT(ISBLANK(AccountId)),
MailingPostalCode != Account.ShippingPostalCode
)
Rushika DebadwarRushika Debadwar
error in traiheadi cant resolve this trailhead task, please help me out
Nidhusha Rachamadugu 4Nidhusha Rachamadugu 4
try with the below code,it is working fine ( to solve the validation rule business logic error )

AND(
NOT( ISBLANK( AccountId ) ),
MailingPostalCode <> Account.ShippingPostalCode
 
Akhilesh MallickAkhilesh Mallick
Hi @Rushika,
Try to do the same process in different Trial head playground if you are facing hte same issue.
You will pass the exam with not error.
P.S- I alson faced the same issue.
harith vemunuriharith vemunuri
The validation rule does not reference the account Shipping Postal Code field (ShippingPostalCode)
I have a problem with this ...can any one help me...???