function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
trueSayertrueSayer 

Contact Relationship Problem

I have a requirement from a client that states the following:

 

In a salesforce.com environment, the client wants to ensure the following:


● Each account may have multiple contacts but only one marked “Primary”
● Each account must have 1 primary contact.
● The account should display the name of its primary contact on the account detail screen (i.e. in a field).

 

How do I start this requirement I am thinking of doing the following:

 

Creating trigger on the Account object (after update, after create) that will run thru  the list of related contacts to make sure atleast one is created with Primary checkbox field is marked true.

 

Also create a Contact Trigger (before update, before create)  that will:

 

Find current Primary contact for the Account.

                   If current contact is primary do nothing

                   If current contact is new Primary.  Update old Contact Primary check box to False.  Update current Contact to True.

                                     And Update Primary Contact Name field on Account record with primary contact name

 

 

 

Does this make sense?

 

Best Answer chosen by Admin (Salesforce Developers) 
craigmhcraigmh

Change the Select query to a List where primaryContact__c = true.

 

If the new Contact is the primary, you can just set primaryContact__c = false on all of them and run a batch update.

 

List<Contact> Cs = [Select Id, primaryContact__c From Contact Where primaryContact__c = true And account.AccountId = :trigger.new[i].AccountId Limit 10000];
for(Contact c: Cs) {
	c.primaryContact__c = false;
}
update Cs;

 

All Answers

Starz26Starz26

Close...My logic would be

 

1. If a new/update contact check to see if primary is checked

     1a. if it is, search for an existing primary and remove the flag from existing

2. If it is not marked as primary

     2a. check for existing primary if none, mark current as primary (or other logic for accounts without a current primary)

3. update account field appropriatly

 

very similar......

trueSayertrueSayer

Here is the code I created:

 

trigger UpdatePrimaryContact on Contact (before insert, before update) {

  
for (Integer i = 0; i < Trigger.new.size(); i++) {

if (Trigger.new[i].primaryContact__c == true){

// Find an existing Contact already set to Primary  ** HAVING PROBLEM HERE ***
Contact c = [SELECT id, accountId, primaryContact__c, name FROM contact WHERE primaryContact__c = TRUE and account.AccountId = Trigger.new[i].AccountId LIMIT 1];

if (c <> null) {
c.primaryContact__c = false;
update c;
}

}
if (Trigger.new[i].primaryContact__c == false){

// Find an existing Contact already set to Primary
Contact c = [SELECT id, accountId, primaryContact__c, name FROM contact WHERE primaryContact__c = true LIMIT 1];

if (c == null) {
Trigger.new[i].account.primary_Contact__c = Trigger.new[i].name;
update Trigger.new[i].account;

}

}


}
}

 

 

I am getting a problem building the select for the search.  I am trying to find a Contact belonging the the same Account as the Contact being

created of updated in the Trigger.

 

 [SELECT id, accountId, primaryContact__c, name FROM contact WHERE primaryContact__c = TRUE and account.AccountId =Trigger.new[i].AccountId LIMIT 1];

 

Any help will be appreciated!!


 

craigmhcraigmh

Change the Select query to a List where primaryContact__c = true.

 

If the new Contact is the primary, you can just set primaryContact__c = false on all of them and run a batch update.

 

List<Contact> Cs = [Select Id, primaryContact__c From Contact Where primaryContact__c = true And account.AccountId = :trigger.new[i].AccountId Limit 10000];
for(Contact c: Cs) {
	c.primaryContact__c = false;
}
update Cs;

 

This was selected as the best answer
trueSayertrueSayer

Thanks for your reply.  It is greaty appreciated!!

 

Truesayer

sudharani2561.3929872187343342E12sudharani2561.3929872187343342E12
true sayer did u get the answer,i have same requirement could u help me,
trueSayertrueSayer
The answer the admin approved is correct.