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
Ruby KandahRuby Kandah 

How do I limit the number of records that can be edited in Salesforce with Validation Rules?

Hi Everyone, 

I'm trying to program a validation rule and needed some help. Essentially, I have 3 fields that I would like to incorporate in this rule: Account Type (Picklist field), Deck Rank (Picklist field) and Lead List (Date field). 

I want the rule logic to be something like this:
IF Account Type = GM Pick, someone can only change Deck Rank to "Ace of Spades" IF PRIORVALUE of Deck Rank was "Ace" AND there should be no more than 5 Ace of Spades for Lead List = 09/01/2016

Can someone help me translate this to Salesforce language?


Thank you!
Best Answer chosen by Ruby Kandah
UC InnovationUC Innovation
Hi Ruby,

Looks like you need to include ISPICKVAL for the PRIORVALUE to work... this time I tested on my own org to make sure it works haha. Give this one a try:

AND( ISPICKVAL( Type , "Prospect") , !ISPICKVAL(PRIORVALUE
(DeckRank__c), "Ace"), ISPICKVAL(DeckRank__c, "Ace of Spades"))

Here's the documentation that helped me:

https://help.salesforce.com/htviewhelpdoc?err=1&id=customize_functions_i_z.htm&siteLang=en_US

All Answers

UC InnovationUC Innovation
Hi Ruby,

Here's a validation rule you can use to handle the first part of your logic:

AND(AccountType = "GM Pick", PRIORVALUE(DeckRank) <> "Ace", DeckRank = "Ace of Spades")

This basically translates to: If AccountType = GM Pick AND the prior value of DeckRank was not "Ace", then DeckRank cannot be changed to "Ace of Spades". This means that the user cannot change the DeckRank to "Ace of Spades" unless the prior value was equal to "Ace". 

You'll probably need a before insert trigger to handle checking for 5 accounts with Lead List = 09/01/2016 and Deck Rank = "Ace of Spades". Let me know if you need help writing that trigger, or if I didn't understand your requirements correctly. I'll be happy to help you walk through it.
Ruby KandahRuby Kandah
Hi UC Innovation, 

This looks great! But I'm getting a syntax error: Error: Field Account_Type__c is a picklist field. Picklist fields are only supported in certain functions.
UC InnovationUC Innovation
Hi Ruby,

Oops! I apologize, you need to use ISPICKVAL for picklist fields. it should actually look like this:

AND( ISPICKVAL(AccountType , "GM Pick") , PRIORVALUE(DeckRank) <> "Ace", ISPICKVAL(DeckRank, "Ace of Spades"))

That should work. Let me know if there's any issues!
Ruby KandahRuby Kandah
Hi! Thanks so much, that formula didn't have any syntax errors. However, when I went to test this out, I chose an account that had an "Ace" deck rank and tried to change it to "Ace of Spades" but it gave me my error message that I created: 
Error:Ace of Spades ranking can only be designated to accounts having a prior deck rank value of Ace
UC InnovationUC Innovation
Hi Ruby,

Looks like you need to include ISPICKVAL for the PRIORVALUE to work... this time I tested on my own org to make sure it works haha. Give this one a try:

AND( ISPICKVAL( Type , "Prospect") , !ISPICKVAL(PRIORVALUE
(DeckRank__c), "Ace"), ISPICKVAL(DeckRank__c, "Ace of Spades"))

Here's the documentation that helped me:

https://help.salesforce.com/htviewhelpdoc?err=1&id=customize_functions_i_z.htm&siteLang=en_US
This was selected as the best answer
Ruby KandahRuby Kandah
You are awesome!! 

It worked perfectly :) And thanks for sending me the link to the documentation!
Ruby KandahRuby Kandah
UC Innovation -- 

Can you guide me through checking for 5 accounts with a lead list of 09/05/2016? This type of rule would be a huge help!


Ruby
UC InnovationUC Innovation
Hi Ruby,

You're very welcome. As for checking for 5 accounts, I believe you will need a before insert, before update trigger. 
 
trigger LimitAccountsTrigger on Account (before insert, before update) {
   
    // Find all the accounts that have LeadList set to 9/1/2016 and DeckRank as 'Ace of Spades'
    List<Account> specificAccList = [SELECT id FROM Account WHERE LeadList__c =: date.parse('09/01/2016') AND DeckRank__c =: 'Ace of Spades'];
    
    for (Account a: trigger.new)
    {
        // Don't insert if there are 5 or more records that meet the criteria
        if (a.LeadList__c == date.parse('09/01/2016') && a.DeckRank__c == 'Ace of Spades' && specificAccList.size() >= 5)
        {
            a.addError('Add your error message here.');
        }
    }
}
Hope this helps!
 
Ruby KandahRuby Kandah
That's pretty cool...I've actually never worked with triggers, so I wanted to see if I could do it through a formula field and then a validation rule on that formula field...here is what I have so far:

IF(ISPICKVAL( Account_Type__c, "GM Pick") && ISPICKVAL(Deck_Rank__c, "Ace of Spades") && Lead_List__c = THIS MONTH,1,0)

Basically saying that if account type = gm pick, deck rank = ace of spades and lead list = this month, assign a 1, else assign a 0...it works fine, however, i want it to total it based on the account owner -- right now, there isnt a common identifier, like an account owner for it to aggregate the total...do you know how I can aggregate the total based on account owner?

So essentially, if Account Owner = Ruby and account type = gm pick, deck rank = ace of spades and lead list = this month, add all of the records that belong to ruby with these conditions specified together...

If there isn't a good way to write this in a formula field, I can revert to making the trigger :) 


Ruby
UC InnovationUC Innovation
Hi Ruby,

Unfortunately, you can't look at other account records to aggregate with a formula field. The only other records you can look at are if the account has any related objects. Here's some documentation regarding formula fields:

https://developer.salesforce.com/page/An_Introduction_to_Formulas

Maybe there is another way, but I think making a trigger is easier :)
Ruby KandahRuby Kandah
Okay! I'll go ahead and make a trigger...does your code segment by account owner by any chance? I wanted to limit it to 5 records per account owner!
UC InnovationUC Innovation
Hi Ruby,

That code does not look at the account owners. It should be a simple change though, you just need to adjust the query to include the owner field, and look through the list to find accounts with the same owner as the account you're inserting/updating. 

Try it out, and let me know if you run into any issues!
Ruby KandahRuby Kandah
I got it to work :) Thanks so much for your help!