Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- Apex Code Development
- :
- Account checkbox marks true when at least one Cont...
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Account checkbox marks true when at least one Contact's checkbox is true
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-05-2013 07:15 AM
We currently have a custom checkbox field in contacts that we have set to true if they are the best person to call for that particular account. I want to create a checkbox field in accounts that will automatically mark true if it contains a contact with that particular checkbox marked true.
Essentially, I want an account checkbox called "Contains a best contact" that marks true when at least one of its contacts has the "Best person to contact" checkbox marked true.
Is this possible?
Thanks in advance!
Solved! Go to Solution.
Re: Account checkbox marks true when at least one Contact's checkbox is true
[ Edited ]- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-05-2013 07:36 AM - edited 02-05-2013 07:37 AM
You can write a trigger on the contact object, when a record with Best person to contact is checked or unchecked, update the account record's Contains a best contact field.
Naidu
Re: Account checkbox marks true when at least one Contact's checkbox is true
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-05-2013 07:42 AM
Re: Account checkbox marks true when at least one Contact's checkbox is true
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-05-2013 08:38 AM
Trigger ContactTrigger on Contact(after insert, after update)
{
if(Trigger.isAfter)
{
if(Trigger.isInsert)
{
List<Id> accIds = new List<Id>();
for(Contact con : Trigger.new)
{
if(con.Best_person_to_contact__c)
{
accIds.add(con.AccountId);
}
}
List<Account> accts = [SELECT Id, Name, Contains_a_best_contact__c FROM Account WHERE Id IN :accIds];
for(Account acc : accts)
{
acc.Contains_a_best_contact__c = true;
}
update accts;
}
if(Trigger.isUpdate)
{
List<Id> updateAccs = new List<Id>();
List<Account> accts = new List<Account>();
for(Integer i =0; i < Trigger.new.size(); i++)
{
if(Trigger.new[i].Best_person_to_contact__c <> Trigger.Old[i].Best_person_to_contact__c)
{
updateAccs.add(Trigger.new[i].AccountId);
}
}
Map<Id, AggregateResult> accMap = new Map<Id, AggregateResult>([SELECT AccountId Id, Count(Id) cnt FROM Contact WHERE AccountId IN :updateAccs AND Best_person_to_contact__c = true GROUP BY AccountId]);
for(Account acc : [SELECT Id, Name, Contains_a_best_contact__c FROM Account WHERE Id IN :updateAccs])
{
AggregateResult ar = accMap.get(acc.Id);
if(ar <> null)
{
if(Integer.valueOf(ar.get('cnt')) > 0)
{
acc.Contains_a_best_contact__c = true;
}
else
{
acc.Contains_a_best_contact__c = false;
}
}
else
{
acc.Contains_a_best_contact__c = false;
}
accts.add(acc);
}
update accts;
}
}
}Try this.
Naidu
Re: Account checkbox marks true when at least one Contact's checkbox is true
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-05-2013 08:40 AM
trigger updateRelatedAccounts on Contact( after insert, after update, after delete, after undelete ) {
Map< id, account > accs = new map< id, account >( );
if( trigger.old != null )
for( contact record: trigger.old )
accs.put( record.accountid, new account( id = record.accountid ) );
if( trigger.new != null )
for( contact record: trigger.new )
accs.put( record.accountid, new account( id = record.accountid ) );
accs.remove( null );
update accs;
}
trigger updateBestContactField on Account ( before update ) {
for( account record: trigger.new )
record.has_best_contact__c = false;
for( contact record: [select accountid from contact where best_contact__c = true and accountid in :trigger.new )
trigger.newmap.get( record.accountid ).has_best_contact__c = true;
}
~ sfdcfox ~
I am a sandwich. That is all.
Re: Account checkbox marks true when at least one Contact's checkbox is true
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-05-2013 08:41 AM
Re: Account checkbox marks true when at least one Contact's checkbox is true
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-05-2013 08:44 AM
Re: Account checkbox marks true when at least one Contact's checkbox is true
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-05-2013 08:47 AM
~ sfdcfox ~
I am a sandwich. That is all.

