You need to sign in to do that
Don't have an account?
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?
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.
All Answers
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......
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!!
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.
Thanks for your reply. It is greaty appreciated!!
Truesayer