0

Let's say hypothetically an Account has 3 Contacts related to it. Each Contact can have a status of 1 through 5. If the sales reps are doing what they should, that status should always go up, but I want to proof it against accidentally saving with a higher status and then lowering. I want the Account to show the highest of Contact Status across all the contacts that belong to it. Let's call this hypothetically Account_Contact_Highest_Status.

What I've done so far is created a trigger after update/insert on Contact that ratchets Account_Contact_Highest_Status. I've bulkified it and it works fine. The problem is if somebody accidentally saves a contact with a higher status, ratchets the Account_Contact_Highest_Status up, and then re-saves the contact with a lower status.

I have a way to account for this, but it would require DML queries to get the status for all the Contacts on every Account involved. I can't think of a way to go about doing this that would be more efficient that would not violate my governor limits. Is there a better way, either via Trigger or other?

thoughts?

4
  • 2
    I'm not sure I follow how this solution addresses the business problem, but regardless, it sounds to me like this can be done with a simple validation rule. – David Reed May 25 '18 at 0:00
  • Thanks for the response! I'm a bit new to Salesforce, but from my understanding validation rules only validate a user's input prior to saving a record. I'm not sure how that would be helpful in this situation, as it doesn't prevent the situation where a sales user accidentally sets a status higher when he did not mean to (either through accident or through business data that later negates the status change). the business problem is dictated by a CEO request and a bit complicated, so I tried to simplify to the core problem in my question. – AlexC May 26 '18 at 21:31
  • I don't understand the summarizing aspect at all, but you can write a validation rule using PRIORVALUE() to require that the Status field only changes in one direction. – David Reed May 26 '18 at 22:09
  • I'm probably not describing the problem accurately, as the business case is very specific to this business and it probably doesn't make much sense out of context. The reason why I don't think a validation rule would work is because there are situations where a sales rep might either through carelessness or changing business circumstances, have ratcheted the value up, but then need to correct it back down. Forcing the validation rule means that this might sometimes happen, and can only be corrected with admin intervention. I think I found a solution for myself, which I will post as an answer – AlexC May 29 '18 at 21:12
0

I know this question was a bit unclear and vague, but if anybody else stumbles here with a related question, here is what I'm doing now to resolve this. I'm writing a Visualforce page that replicates the Account page functionality, with an added dynamically generated "field" that shows the status, which I use a SOQL query in an Apex class to iterate through all the contacts under that account to find the contact with the highest status. As of this writing, I believe that this will meet the business requirements as described by the CEO, and does not have the cumbersomeness of the trigger solution I previously proposed.

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.