1

I have a zoo__c object. On Zoo__c , I have a field called firstName__c. I want to prevent insertion/updating of records if firstName__c has any of these characters at the end - "op" , "gp" , "kp". If any of these characters are at the end of the firstName__c value then ignore them and do not insert. What is the best way to achieve this. I have a lot of zoo__c records in xls sheet which I will be uploading using Dataloader. I do not have option to remove the records that are not needed before inserting them.

What I think:

a. I thought, I can write a Validation rule to prevent the insertion of the record whose name ends with "op" , "gp" , "kp" but I do not see a formula which can check only the last two characters of a text in formula.

b. Should I go with a trigger and check in before insert using string methods. Throw an error if the last two characters match with the text.

Any suggestions on the options for this?

3

You can indeed use validation rule:

1=CASE(LOWER(RIGHT(firstName__c,2)),"op",1,"gp",1,"kp",1,0)

A trigger is also possible, but not necessary.

  • Can you please Explain what is 1=Case – cloudZigZag Aug 30 '17 at 7:01
  • 1
    @cloudZigZag Case returns a value based on the input (the first parameter). 1= simply means we're comparing the case's return value to the number 1. So if the last two characters is "op", case returns 1, and the formula evaluates to 1=1, which is true (the validation triggers). If the last two characters are "nz", it does not match any value, so case returns the last parameter, and the formula evaluates to 1=0, which is false (the validation does not trigger). – sfdcfox Aug 30 '17 at 11:59
0

@sfdcfox Awesome!! I just tried like this though.

OR(
    CONTAINS(LOWER(RIGHT(firstName__c,2)), "op"),
    CONTAINS(LOWER(RIGHT(firstName__c,2)), "gp"),
    CONTAINS(LOWER(RIGHT(firstName__c,2)), "kp")
)
  • Both means same. the first answer is one liner and has less repetitive texts. That's all. – SfdcBat Aug 30 '17 at 1:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.