Many a times, we come across business use cases that force us to introduce apex customization which obviously comes at a heavy cost. To keep the maintenance costs low and for quick results, the declarative capabilities of the Force.com platform is simply amazing ! However, the declarative capabilities in itself has its own limitations. So, today I will walk you through a use case which is actually relevant for many customers and what’s the best way to implement it which would otherwise require some customization in apex.
Business Case: ABC company uses Accounts and Contacts and they have many contacts associated with an Account. However, for communication purposes they want to identify a primary contact for each Account which will then be their single POC for all the communication purposes. To implement this, they want to ensure that only one contact is selected as a primary contact for each Account at a time.
Implementation: Validation rules ! Yes, indeed they are the first thing that comes into our mind when we want to restrict something. However, is this scenario only possible through a validation rule. How do you check if there are other records with a primary flag selected ? Yes, you could have done that had this been a custom object by using VLOOKUP. However, standard objects don’t support VLOOKUP. So, we will be shaking hands with a workflow rule to build this validation rule.
What we will be doing ? : We would create a custom text field named as “AccountID__c” in Contact and make it a unique field and populate that field with the Account ID every time a contact is selected as primary. So we will create a workflow rule to check if the contact has been selected as a primary contact and then do a field update on the custom field. Pretty Straightforward !
So now if a contact is selected as primary, the custom text field in my case “AccountID__c” would immediately be populated with the Account ID. Again, if we try to select another contact as primary within the same Account, the workflow field update will throw an error saying that “Error: Duplicate value on record”
Step 1
Step 2
Isn’t it interesting that you could actually build a validation rule by being a bit tricky with the workflow rule ?
So, the next time you have a requirement, take a look if there is any possible way to do that with the declarative capabilities. You should probably step into customization only after you are dead sure that it cannot be done with declarative capability !
Remember that you will need to have additional setup if you decide to some day change your primary contact from one contact to another. I would leave that to you to figure it out yourselves. If you have any questions please feel free to comment on this post.
Happy using Workflow rules !
Hello Akhil,
Can you share some other use cases for this? According to the order of operations, Workflows and Processes fire after custom validation rules. I believe that the only reason this scenario works is that AccountId__c must be unique, and because the system runs standard validation again as the last step. I’m implying that you can only use this method of validation if you’re in a situation where standard validation would fire (null check on required fields, duplicate value check, and maybe something else that I’m missing).
Thanks,
Parker
LikeLike
Hi Parker,
You nailed it ! Yes, it works only because the system checks whether the value pushed into the AccoutID__c is a unique value across contacts since we have defined that field as a unique field. The order of workflows or validation rules doesn’t really matter here because we haven’t used any validation rule here at the first place. We are just trying to mimic a validation using the uniqueness property of a field.
I hope that answers your question !
Thanks,
Akhil
LikeLike
Hello Akhil,
Yes that does make some sense. Are there any other use cases for this other than making sure that only one child record gets a special flag? Frankly, I feel that it would make more sense to create a Lookup field from the Parent record to the special child record so that we can use things like cross-object formulas and actually see who the primary contact is from the Account page layout without a lot of logic. Does this make sense?
Regards,
Parker
LikeLike
Hello Akhil ,
If we have two contacts say ‘A’ &’B’ for an account say ‘Apple’ and we have fixed contact named ‘B’ as primary contact but it should be contact ‘A’ , so as per your work flow rule we have to first delete the reference accountid__c field value from contact ‘B’ and make it as non primary contact first , then we have to make contact ‘A’ as primary contact right???
LikeLike
Good point Vamshi ! So, you are right. The value in the AccountID__c field should be flushed out before you can set another contact within the same account as primary. So, this should not be a manual process. You can handle this scenario by having another workflow rule that will be triggered when the primary checkbox is unchecked. This workflow will be responsible for flushing out the values from AccountID__c and then you are good to set another contact as primary !
LikeLike
Hola, good article. Try providing solution for some more variations on the use case to make it an even better article.
LikeLike
Thanks for the feedback Abhijit 🙂
LikeLike