3

The scenario for the validation rule is: -if one lookup field is empty, then check that other n fields are not empty (if a lookup is empty, then make sure that the fields that will be used to create the new object are filled with something)

It should be as easy as this:

AND(
    ISNULL(MyLookup__c),
    OR(
        ISBLANK(MyText__c),
        ISBLANK(MyText2__c),
        ISBLANK(MyText3__c),
        ISBLANK(TEXT(MyPicklist__c)),
        ISBLANK(TEXT(MyPicklist2__c)),
        ISBLANK(TEXT(MyPicklist3__c))
    )
)

EDIT: MyText__c and MyText2__c are text fields, MyText3__c is Long Text.. if I remove the long text and the picklists the condition works as expected.. :s

EDIT2: this is the code I've used in a trigger to perform the check and it works as expected:

 if(
      r.MyLookup__c==null && (
           r.MyText__c==null ||
           r.MyText2__c==null ||
           r.MyText3__c==null ||
           r.MyPicklist__c==null ||
           r.MyPicklist2__c==null ||
           r.MyPicklist3__c==null
      )
 ){
      r.addError('Fill the data to...blabla');

 }
  • 1
    if lookup is empty then all fields should be filled ? – Ratan Paul May 4 '16 at 2:54
  • 1
    Yes, I think though there is something wrong with LongText.. please don't tell me that ISBLANK won't work on Long Text fields... – T. Rossi May 4 '16 at 13:05
  • 1
    I tried that with Ratan but it won't work on longtext anyway – T. Rossi May 4 '16 at 14:35
  • 2
    You might upvote this idea – Chance May 4 '16 at 14:47
  • 1
    Also, look in the comments at Eva Mave Ng's suggestion. Maybe that could work? I know it seems like a long work around... – Chance May 4 '16 at 14:49
0

You can achive that with a validation rule or a before trigger is you want a validation rule you need change the

ISNULL(MyLookup__c) to ISBLANK(MyLookup__c).

If you want to use a trigger I recomend you that you use the next example.

if( String.isBlank(r.MyLookup__c) && (
  String.isBlank(r.MyText__c) ||
  String.isBlank(r.MyText2__c) ||
  String.isBlank(r.MyText3__c) ||
  String.isBlank(r.MyPicklist__c) ||
  String.isBlank(r.MyPicklist2__c) ||
  String.isBlank(r.MyPicklist3__c)
)){
  r.addError('Fill the data to...blabla');
}

All this is because the Ids are String fields and in the string null value is diferente a blank value.

| improve this answer | |
  • Thx for the code suggestion, the lookup part is working properly I can use ISBLANK if it's a best practice, but it was not the encountered problem. The problem is what ISBLANK id doing on longtext fields, I can't figure it out, looks like it's always false. – T. Rossi May 4 '16 at 14:35
  • then use the isblank in the trigger because the long text could be empty or be a space chain – Torsen May 4 '16 at 14:41
0

You can use this. Hope this help

AND(ISBLANK(MyLookup__c),



    OR(

        NOT(ISBLANK(MyText__c)),

        NOT(ISBLANK(MyText2__c)),

        NOT(ISBLANK(MyText3__c)),

        NOT(ISBLANK(TEXT(MyPicklist__c))),

        NOT(ISBLANK(TEXT(MyPicklist2__c))),

        NOT(ISBLANK(TEXT(MyPicklist3__c)))
   )
)
| improve this answer | |
  • Ankit -- when posting code/formulas - please format the code using the {} toolbar button – cropredy Jun 8 '16 at 6:41
  • Sure Ratan,Noted – Ankit Tomar Jun 8 '16 at 6:47
  • Ankit : Ratan kindly formatted this for you – cropredy Jun 8 '16 at 6:48
-1

I think Mr Frodo is right here.
Your formula returns true if the lookup is null OR ANY of tested fields are not blank. I think you want to test that all the the fields to be filled in. You need to use an AND for that. Try this...

OR(
    ISNULL(MyLookup__c),
    AND(
        NOT(ISBLANK(MyText__c)),
        NOT(ISBLANK(MyText2__c)),
        NOT(ISBLANK(MyText3__c)),
        NOT(ISBLANK(TEXT(MyPicklist__c))),
        NOT(ISBLANK(TEXT(MyPicklist2__c))),
        NOT(ISBLANK(TEXT(MyPicklist3__c)))
   )
)
| improve this answer | |
  • say my lookup is empy, no matter what I do in the other fields, I'll always get the error OR(true, anything) = true... :/ and once again: AND (NOT(A), NOT(B),..) = OR(A, B, C) .... – T. Rossi May 4 '16 at 14:22

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.