+ Start a Discussion
Raz RaslanRaz Raslan 

Primary Contact Function

Hi Everyone,

I have the following trigger in play:


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) {
                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; 


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.

The active checkbox is driven using a formula on an "expiration date" field within the contact object, so if the expiration date field is populated, the active checkbox is automatically unchecked. If the expiration field is empty, the active checkbox is checked by default

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

santanu boralsantanu boral
For your last question, you can refer this link: https://developer.salesforce.com/forums#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=ALLQUESTIONS&id=906F0000000kETiIAM

For other requirements, you can learn to write triggers from http://www85.homepage.villanova.edu/timothy.ay/MIS2040/Secure2/force_platform_cookbook.pdf

one clue is that, calculation of formula is done after that record is saved. So, for a contact, if expiration date is updated then you will not get the latest value of is_active indicator in before insert or update trigger. Hope it helps.
Jerun JoseJerun Jose
Hi Raz,

I believe a lookup filter can perform what you have requested without the need to code. The lookup filter can be set to display only active contacts and throw an error when trying to add inactive contacts.
Please note that any validation you enforce on the child object by checking values of the parent object are not recalculated when you edit the parent record. This means that after you have attached a few contacts to a primary contact, you will be able to go to that primary contact record and make it inactive without any errors.
To overcome this, you will need to write an apex trigger on the contact object that will throw an error on deactivation when there are any related contacts. An alternate approach would be to use process builder to update a field which counts the number of related contacts and use a validation rule which only allows you to set the flag as inactive if the counter is 0.

Raz RaslanRaz Raslan
Hi Jerun,

Thank you for your suggestion and it seems to be a more plausible solution. I have tried to add a validaiton rule on the contact object:

AND(Expiration_Date__c != NULL,Primary_Contact__c = TRUE)

For some reason this validation is not trigerring and giving me the desired result. What am I doing wrong here???