+ Start a Discussion
Raz RaslanRaz Raslan 

Primary Contact Validation

Hi Everyone,

I have the current trigger set up on the contact object.
User-added image

What this Trigger currently does is only allow one contact to be selected as the primary contact. I am quite new to coding so I am a little stuck with what I want to achieve.

What I want to modify this code to do is ensure that you cannot select a contact as a primary contact, if that contact is not active. So only active contacts can be selected as primary. Also, if a primary active contact becomes inactive there should be some immediate actions to allow you to select another contact as Primary.

Also, I would like to have an error message display if a user tries to add more than one Primary Contact. 

If anyone could help that would be greatly appreciated

Regards,

Raz

RbnRbn
Hi Raz,

Can you please re-post your code .Its not clear enough to go through.

Rgrds,
Rabi
Raz RaslanRaz Raslan
Hi Rabi, Please see below. Hopefully it's a little clearer [image: Inline image 1]
RbnRbn
Hi Raz,

Currently i dont see anything.Instead of pasting it as a iamge.

Copy & Paste the lines of Code.So that its easy enough to debug in personal org.

User-added image



Rgrds,
Rabi
Raz RaslanRaz Raslan
Sorry Rabi! Ok here goes.. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 trigger PrimaryContact on Contact (before insert, before update) { set getid = new set(); string contactId; List conList = new List(); if(Trigger.isInsert || Trigger.isUpdate) { for(Contact cont: Trigger.New) { if(cont.Primary_Contact__c == true) { getid.add(cont.AccountId); contactId = cont.id; } } } List cList = [select id, Primary_Contact__c from contact where accountid IN: getid AND Primary_Contact__c = true]; if(cList.size() > 0) { for(Contact newClst: cList) { if(newClst.id != contactId) { newClst.Primary_Contact__c = false; conList .add(newClst); } } } update conList; }
RbnRbn
Raz,

 IsPrimaryContact & Is Active are two fields of Datatype Checkbox on ContactObject

Is my understandfing correct?
Raz RaslanRaz Raslan
Yes they are. IsPrimaryContact is just a checkbox, while IsActive is driven from an "expiration date" field. If the expiration date field is empty, the contact is active. If it is populated, the contact is not active
Raz RaslanRaz Raslan
trigger PrimaryContact on Contact (before insert, before update) {
  
   set<id> getid = new set<id>();
    string contactId;
    List<Contact> conList = new List<Contact>();
  
    if(Trigger.isInsert || Trigger.isUpdate) {
      
        for(Contact cont: Trigger.New) {
          
            if(cont.Primary_Contact__c == true) {
              
                getid.add(cont.AccountId);
                contactId = cont.id;
            }
        }
    }
  
    List<contact> cList = [select id, Primary_Contact__c from contact where accountid IN: getid                                                   AND Primary_Contact__c = true];
  
    if(cList.size() > 0) {
      
        for(Contact newClst: cList) {
          
            if(newClst.id != contactId) {
              
                newClst.Primary_Contact__c = false;
                conList .add(newClst);
            }
        }
    } 
    update conList; 
  }