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
StardustStardust 

A validation rule enforcing the hierarchy between Accounts´ record types

Hi there,

 

we need a validation rule on accounts which would make sure that the newly created account is connected to a parent account with the hierarchically higher level.

This is the record type hierarchy:

1st (highest level)= consolidated BP

2nd= BP

3rd= invoice acc

4th= delivery acc

 

and this is the formula I am trying to play around:

IF(
   AND(
       NOT(ISNULL(ParentId)),
       RecordType.Name = "Invoice Acc",
       Parent.RecordType.Name = "Delivery Acc"
   ), true,
       IF(
           AND(
               NOT(ISNULL(ParentId)),
               RecordType.Name = "Delivery Acc",
               Parent.RecordType.Name= "BP"
           ), true,
               IF(
                   AND(
                       NOT(ISNULL(ParentId)),
                       RecordType.Name = "BP",
                       Parent.RecordType.Name= "Consolidated BP"
                   ), true, false
               )
       )
)

But it is not working...there is always a syntax error...

 

Could you help me? Is this possible at all, or is there a workaround?

 

thanks!

Best Answer chosen by Admin (Salesforce Developers) 
Shannon HaleShannon Hale

We're going to get this, I promise!

 

First, I realized I made a mistake earlier when I suggested Name should be the API name. When I double-checked, it turns out that unlike most other metadata, RecordType.Name is the label and RecordType.DeveloperName is the API name (with the underscores). I apologize for leading you astray there.

 

Second, I noticed that in your first description of the problem, your hierarchy was as follows:

  1. Consolidated BP (highest)
  2. BP
  3. Invoice Acc
  4. Delivery Acc (lowest)

but it looks like you've got Invoice Acc and Delivery Acc switched around in your formula.

 

With all that in mind I tried this, and it seems to work:

 

IF(
  ISBLANK( ParentId ),
  false, /* Having no parent is always valid */
  OR( /* If any of these returns true, trigger the validation rule */
    AND(
      RecordType.DeveloperName = "Delivery_Acc",
      NOT( Parent.RecordType.DeveloperName = "Invoice_Acc" )
    ),
    AND(
      RecordType.DeveloperName = "Invoice_Acc",
      NOT (Parent.RecordType.DeveloperName = "BP" )
    ),
    AND(
      RecordType.DeveloperName = "BP",
      NOT( Parent.RecordType.DeveloperName = "Consolidated_BP" )
    )
  )
)

 

Let me know how that works for you.

 

 

 

All Answers

Jeff MayJeff May

The error is because you can't get to the Parent Record type that way.   Here is an Answers post by @SteveMo that describes a way to accomplish this.  https://success.salesforce.com/questionDetail?qId=087300000006xBYAAY

Shannon HaleShannon Hale

As of the Spring '13 release, $RecordType is deprecated and we support RecordType as a cross-object reference, even in validation rules (rendering Steve's solution obsolete).

 

I don't get a syntax error when I paste your formula into a validation rule. However, I notice that you are referencing RecordType.Name but the value you're comparing it to looks like the label -- names don't have spaces in them.

 

Can you post the content of your syntax error?

StardustStardust

Hi again,

 

I am actually not getting a syntax error any more...

After I put the name in the inverted commas, instead of labels (thanks @shale), the validation rule still does not work...

 

IF(
AND(
NOT(ISNULL(ParentId)),
RecordType.Name = "Invoice_Acc",
Parent.RecordType.Name = "Delivery_Acc"
), true,
IF(
AND(
NOT(ISNULL(ParentId)),
RecordType.Name = "Delivery_Acc",
Parent.RecordType.Name= "BP"
), true,
IF(
AND(
NOT(ISNULL(ParentId)),
RecordType.Name = "BP",
Parent.RecordType.Name= "Consolidated_BP"
), true, false
)
)
)

 

so I guess it is the logic that is not correct.

 

Does anyone have an idea of what should be changed?

 

Thanks!

Shannon HaleShannon Hale

Try replacing ISNULL() with ISBLANK(). ISNULL() doesn't have the behavior you'd expect with strings (they are not "null" when empty), so I recommend always using ISBLANK() instead.

 

Let me know if that doesn't work and I'll take a deeper look later this morning.

 

 

 

StardustStardust

Thanks @shale!

 

I have continued working on this and the latest version is:

IF(OR 

AND( 
NOT(ISBLANK(ParentId)), 
NOT(AND(RecordType.Name = "Invoice_Acc", 
Parent.RecordType.Name = "Delivery_Acc" 
))), 
AND( 
NOT(ISBLANK(ParentId)), 
NOT(AND(RecordType.Name = "Delivery_Acc", 
Parent.RecordType.Name= "BP" 
))), 
AND( 
NOT(ISBLANK(ParentId)), 
NOT(AND(RecordType.Name = "BP", 
Parent.RecordType.Name= "Consolidated_BP" 
))) 
), false, IF (ISBLANK(ParentId), false, true))

 

unfortunatey, it still does not work as it should...

Shannon HaleShannon Hale

We're going to get this, I promise!

 

First, I realized I made a mistake earlier when I suggested Name should be the API name. When I double-checked, it turns out that unlike most other metadata, RecordType.Name is the label and RecordType.DeveloperName is the API name (with the underscores). I apologize for leading you astray there.

 

Second, I noticed that in your first description of the problem, your hierarchy was as follows:

  1. Consolidated BP (highest)
  2. BP
  3. Invoice Acc
  4. Delivery Acc (lowest)

but it looks like you've got Invoice Acc and Delivery Acc switched around in your formula.

 

With all that in mind I tried this, and it seems to work:

 

IF(
  ISBLANK( ParentId ),
  false, /* Having no parent is always valid */
  OR( /* If any of these returns true, trigger the validation rule */
    AND(
      RecordType.DeveloperName = "Delivery_Acc",
      NOT( Parent.RecordType.DeveloperName = "Invoice_Acc" )
    ),
    AND(
      RecordType.DeveloperName = "Invoice_Acc",
      NOT (Parent.RecordType.DeveloperName = "BP" )
    ),
    AND(
      RecordType.DeveloperName = "BP",
      NOT( Parent.RecordType.DeveloperName = "Consolidated_BP" )
    )
  )
)

 

Let me know how that works for you.

 

 

 

This was selected as the best answer
StardustStardust

Thanks @shale, both for resolving this and explaining it so clearly. I´ve learned a lot!

Shannon HaleShannon Hale

You're welcome! Glad I could help.