Showing posts with label Formula Field. Show all posts
Showing posts with label Formula Field. Show all posts

Sunday, September 2, 2018

Salesforce Trailhead Challenge: Create a validation rule to check that a contact is in the zip code of its account

Salesforce Trailhead Challenge: Create a validation rule to check that a contact is in the zip code of its account




Error Condition Formula:

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

)

Friday, March 9, 2012

Calculating Age From Birth Date

Answer for Calculating Age From Birth Date :
Formula Code:
IF(MONTH(TODAY())>MONTH(DOB_API),YEAR(TODAY())-YEAR(DOB_API),IF(AND(MONTH(TODAY())=MONTH(DOB_API),DAY(TODAY())>=DAY(DOB_API)),YEAR(TODAY())-YEAR(DOB_API),(YEAR(TODAY())-YEAR(DOB_API))-1)) 
First simply if the month of present year is greater than date of birth than age will be calculated as the difference between present year and year of birth.

Secondly if the first scenario fails two options comes in my mind, if the month of present year is same then is day of birth is same or not so if day of birth is greater than or equals to day of birth than age is calculated as we have done in the first scenario.

Thirdly if above both scenarios fails that means month of present year and month of birth date is same but day of birth is smaller than day of present day. So age is calculated as the difference between present year and year of birth -1

Using REGEX in Validations

Example 1 : Validate Credit Card Number
Description :

Validates that a custom text field called Credit_Card_Number is formatted in 9999-9999-9999-9999 or 9999999999999999 number format when it is not blank.The pattern specifies:
• Four digits (0-9) followed by a dash: \\d{4}-
• The aforementioned pattern is repeated three times by wrapping it in () {3}
• Four digits (0-9)
• The OR character (|) allows an alternative pattern of 16 digits of zero through nine with no dashes: \\d{16}
Validation Formula :
NOT( REGEX( Credit_Card_Number__c ,  "(((\\d{4}-){3}\\d{4})|\\d{16})?"))  
Error Message : Credit Card Number must be in this format: 9999-9999-9999-9999 or 9999999999999999.


Example 2 : Valid IP Address
Description :
Ensures that a custom field called IP Address is in the correct format, four 3-digit numbers (0-255) separated  by periods.

Validation Formula :

NOT( 
REGEX( IP_Address__c, 
"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" 
)) 
Error Message : IP Address must be in form 999.999.999.999 where each part is between 0 and 255.


Example 3 : US Phone Number Has Ten Digits
Description :
Validates that the Phone number is in (999) 999-9999 format. This works by using the

REGEX function to check that the number has ten digits in the (999) 999-9999 format.


Validation Formula : 
NOT(REGEX(Phone, "\\D*?(\\d\\D*?){10}"))  
Error Message : US phone numbers should be in this format: (999) 999-9999.


Example 4 : Social Security Number Format
Description :
Validates that a custom text field called SSN is formatted in 999-99-9999 number format

(if it is not blank). The pattern specifies:
• Three single digits (0-9):\\d{3}
• A dash
• Two single digits (0-9):\\d{2}
• A dash
• Four single digits (0-9):\\d{4}


Validation Formula :
NOT( 
OR( 
LEN (Social_Security_Number__c) = 0, 
REGEX( Social_Security_Number__c , "[0-9]{3}-[0-9]{2}-[0-9]{4}") 
))
 

 Error Message : SSN must be in this format: 999-99-9999.