• Raz Raslan
  • NEWBIE
  • 25 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 11
    Replies
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) {
              
                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; 
  }

                                                 ---------------------------------------------------------------------------------------------

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

Regards,
Raz

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

Hi There,

I followed the instructions as precisely as I could for this challenge in Trailhead but I got the following error message. Please help!

User-added image
Hi Everyone,

I am having a problem very similar to some people above. I have followed instructions and I am getting an error message:

 Challenge not yet complete... here's what's wrong: 
A predefined value is not defined for the 'Type' field or the 'Type' field was not added to the compact layout

User-added image

From all the previous communication on this thread it seems like I have done everything right but I am still getting this error.



Please help!!

Thanks,

​Raz Raslan
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) {
              
                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; 
  }

                                                 ---------------------------------------------------------------------------------------------

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

Regards,
Raz

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

Hi Everyone,

I am having a problem very similar to some people above. I have followed instructions and I am getting an error message:

 Challenge not yet complete... here's what's wrong: 
A predefined value is not defined for the 'Type' field or the 'Type' field was not added to the compact layout

User-added image

From all the previous communication on this thread it seems like I have done everything right but I am still getting this error.



Please help!!

Thanks,

​Raz Raslan