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
Del_SantosDel_Santos 

Validation to verify if the value is valid

Hi,

 

I am thinking if this would be possible in validation rules.

 

I would like to create an object which will store a list of codes, let say, promo codes object.

Now, in the lead page, i would like to create a validation rule if the code entered by the user in the promo_code__c field  is included in list of promo codes created under PROMO CODES object.

 

The only approach i can think is to hardcode all the codes in the rule.

 

ex.

 

OR

(
NOT(promo_code__c ="01566"), 

NOT(promo_code__c ="03454"), 

NOT(promo_code__c ="04546"), 

NOT(promo_code__c ="04637"), 

 

 

Err 

 

 

 

Thanks,

Del

 

 

 

 

 

matermortsmatermorts
To my knowledge, there isn't a way to use declarative means to validate data entered into one field against a separate object's collection of data across several records. I'm sure you could do it with Apex but it's above my skill level. And if you're going to hardcode the values in a validation rule, you might as well just use a picklist field instead of free form text. That way, you'd at least save users the trouble of having to deal with an error message, and it's just as easy to maintain, if not easier. Sorry, I know that's not terribly helpful.
Del_SantosDel_Santos

Thanks, matermorts for this information, however i am new to Apex.

 

Could someone help me on how to  create this using Apex? :(

 

Basically, the requirement is to check the value being entered to promo_code__c field if existing from the collections of the promo code object..Sorry i know i should not suppose to let somebody do the homeowrk for me, but i could just not begin with the approach. :(

 

Thanks in advance

Manos SpanoudakisManos Spanoudakis

Hi there, 

 

You need a trigger on the Lead object (before Insert).

In the trigger

  • you will select all values of the Promo Codes and add them to a Set
  • For each lead you will check if the Lead.Promo_code__c value exists in the above Set. If Not add an error to the Field Lead.Promo_code__c

I don't know your business rules, maybe you need to do this also for Updates.

Shouldn't be a big deal 

 

Cheers,

M