I am trying to limit an Account to have only one Primary Contact (with a small twist).

What I would like to do is, If Contact A is already Primary, and a user goes in selects Contact B as Primary, then I want to have Contact B as Primary, and no longer have Contact A as Primary.

How could I go about doing that?

I have this following code:

trigger checkaccount on contact(before insert, before update){

    List<Id> accID = new List<Id>();
    for(contact c : trigger.new){
        if(c.accountId != null){

    List<account> acc = [select id, (select id, Primary__c from contacts where Primary__c = true) from Account WHERE Id In: accID];
 Map<id, boolean> bool = new map<id,boolean>();
    for(account a : acc){
        bool.put(a.id, a.contacts.size()>0 ? true : false);

    for(Contact c : trigger.new){
        if(bool.get(c.AccountId) == true){
            c.addError('no more primary fields');

  • did you try to put your logic in the code? Apr 27, 2017 at 19:11
  • In the code above? No I am not a code myself, I found the code above and just altered a little bit of it. The code above gives works well in the sense that it gives an error when someone tries to select a second primary contact, but it a lot of steps to uncheck Primary contact then select another one..
    – B Wayne
    Apr 27, 2017 at 19:14
  • so, it will be best time to practice and learn coding... go through trailheads Apr 27, 2017 at 19:17
  • That's part of the plan...signed up for David Liu's course on Pluralsight..
    – B Wayne
    Apr 27, 2017 at 19:39

1 Answer 1


Flow and Process builderI did a quick test with the assumption that all the contacts with the primary flag will be unchecked except the newly updated record with the below approach:

  1. Create a flow with Type = Autolaunched Flow

    • Define two variables varAccountId and varNewContactId with input/output type = input
    • Drag and drop update record
    • Give name Update PC
    • Under Filter and Assignment section
    • Update = Contact that meets the following criteria
    • Field

    • i. AccountId equals varAccountId

    • ii. Id not equals varNewContactId
    • iii. Primary__c equals true
    • Under Update records fields with variables, constant, input or other values
    • i. Field: Primary__c value: {!$GlobalConstant.False}

      1. Set it as start element, Save the flow and activate

      2. Create a process flow with the condition Primary__c equals true and Primary__c ischanged equals true

        • a. Immediate Action
        • b. Action Type = Flows
        • c. Select the flow you defined
        • d. Under set variables section
        • i. AccountId Reference [Contact].AccountId
        • ii. Id Reference [Contact].Id
      3. Save and Activate
      4. Enjoy the coding and test class free world!
  • Awesome! Thank you so much!! My only concern is if there's a mass update on the primary field would process builder be able to handle it?
    – B Wayne
    Apr 28, 2017 at 17:41
  • Sorry, no prior experience with bulk update behavior. Hopefully it should work. You may want to test it out in your full sandbox to see how it works.
    – snathan
    Apr 28, 2017 at 18:31

Not the answer you're looking for? Browse other questions tagged .