Search code examples
validationtimesalesforceapex-codepicklist

How to validate following in picklists in apex salesforce?


I'm just starting with salesforce and using picklists in following way: see img: enter image description here

See, I have four picklists. I want to validate when From Time and To Time is AM and AM; I want to do : To time should not be grater than from time I am not able to validate it. I'm trying to solve it with validations nad apex code but I don't know what's going wrong. please provide some insights, any help will be appreciated.


Solution

  • Suppose your four picklist fields are FromTime, FromAMPM, ToTime, ToAMPM, the "Error Condition Formula" for the Validation Rule should be:

    TEXT(FromAMPM__c) == TEXT(ToAMPM__c) &&
    VALUE(LEFT(TEXT(FromTime__c),LEN(TEXT(FromTime__c))-3)) >
    VALUE(LEFT(TEXT(ToTime__c), LEN(TEXT(ToTime__c))-3))
    

    If the user picks (AM, AM) or (PM, PM), we convert the text before ":00" (the last 3 characters) to numbers and compare them.

    • TEXT() converts a picklist value into a text: TEXT()
    • VALUE() converts a text string to a number: VALUE()