trigger ContactTrigger on Contact (after insert, after update, after delete, after undelete) {
//---> above handling all states which could see a contact added to or removed from an account
//---> on delete we use Trigger.Old, all else, Trigger.new
List<Contact> contacts = Trigger.isDelete ? Trigger.old : Trigger.new;
//---> the Set class rocks for finding the unique values in a list
Set<Id> acctIds = new Set<Id>();
for (Contact c : contacts) {
//yes, you can have a contact without an account
if (c.AccountId != null) {
acctIds.add(c.AccountId);
}
}
List<Account> acctsToRollup = new List<Account>();
//****** Here is the Aggregate query...don't count in loops, let the DB do it for you*****
for (AggregateResult ar : [SELECT AccountId AcctId, Count(id) ContactCount
FROM Contact
WHERE AccountId in: acctIds
GROUP BY AccountId]){
Account a = new Account();
a.Id = (Id) ar.get('AcctId'); //---> handy trick for updates, set the id and update
a.Contact_Count__c = (Integer) ar.get('ContactCount');
acctsToRollup.add(a);
}
//----> probably you'll want to do a little more error handling than this...but this should work.
update acctsToRollup;
}
3 comments
Hi uniquesymbol.com Webmaster!
Cool website!
My name’s Eric, and I just found your site – uniquesymbol.com – while surfing the net. You showed up at the top of the search results, so I checked you out. Looks like what you’re doing is pretty cool.
But if you don’t mind me asking – after someone like me stumbles across uniquesymbol.com, what usually happens?
Is your site generating leads for your business?
I’m guessing some, but I also bet you’d like more… studies show that 7 out 10 who land on a site wind up leaving without a trace.
Not good.
Here’s a thought – what if there was an easy way for every visitor to “raise their hand” to get a phone call from you INSTANTLY… the second they hit your site and said, “call me now.”
You can –
Web Visitors Into Leads is a software widget that’s works on your site, ready to capture any visitor’s Name, Email address and Phone Number. It lets you know IMMEDIATELY – so that you can talk to that lead while they’re literally looking over your site.
CLICK HERE http://advanceleadgeneration.com to try out a Live Demo with Web Visitors Into Leads now to see exactly how it works.
Time is money when it comes to connecting with leads – the difference between contacting someone within 5 minutes versus 30 minutes later can be huge – like 100 times better!
That’s why we built out our new SMS Text With Lead feature… because once you’ve captured the visitor’s phone number, you can automatically start a text message (SMS) conversation.
Think about the possibilities – even if you don’t close a deal then and there, you can follow up with text messages for new offers, content links, even just “how you doing?” notes to build a relationship.
Wouldn’t that be cool?
CLICK HERE http://advanceleadgeneration.com to discover what Web Visitors Into Leads can do for your business.
You could be converting up to 100X more leads today!
Eric
PS: Web Visitors Into Leads offers a FREE 14 days trial – and it even includes International Long Distance Calling.
You have customers waiting to talk with you right now… don’t keep them waiting.
CLICK HERE http://advanceleadgeneration.com to try Web Visitors Into Leads now.
If you’d like to unsubscribe click here http://advanceleadgeneration.com/unsubscribe.aspx?d=uniquesymbol.com
To the uniquesymbol.com Administrator! Eric here with a quick thought about your website uniquesymbol.com…
I’m on the internet a lot and I look at a lot of business websites.
Like yours, many of them have great content.
But all too often, they come up short when it comes to engaging and connecting with anyone who visits.
I get it – it’s hard. Studies show 7 out of 10 people who land on a site, abandon it in moments without leaving even a trace. You got the eyeball, but nothing else.
Here’s a solution for you…
Web Visitors Into Leads is a software widget that’s works on your site, ready to capture any visitor’s Name, Email address and Phone Number. You’ll know immediately they’re interested and you can call them directly to talk with them literally while they’re still on the web looking at your site.
CLICK HERE https://advanceleadgeneration.com to try out a Live Demo with Web Visitors Into Leads now to see exactly how it works.
It could be huge for your business – and because you’ve got that phone number, with our new SMS Text With Lead feature, you can automatically start a text (SMS) conversation – immediately… and contacting someone in that 5 minute window is 100 times more powerful than reaching out 30 minutes or more later.
Plus, with text messaging you can follow up later with new offers, content links, even just follow up notes to keep the conversation going.
Everything I’ve just described is extremely simple to implement, cost-effective, and profitable.
CLICK HERE https://advanceleadgeneration.com to discover what Web Visitors Into Leads can do for your business.
You could be converting up to 100X more eyeballs into leads today!
Eric
PS: Web Visitors Into Leads offers a FREE 14 days trial – and it even includes International Long Distance Calling.
You have customers waiting to talk with you right now… don’t keep them waiting.
CLICK HERE https://advanceleadgeneration.com to try Web Visitors Into Leads now.
If you’d like to unsubscribe click here http://advanceleadgeneration.com/unsubscribe.aspx?d=uniquesymbol.com
To the uniquesymbol.com Administrator.
My name’s Eric and I just came across your website – uniquesymbol.com – in the search results.
Here’s what that means to me…
Your SEO’s working.
You’re getting eyeballs – mine at least.
Your content’s pretty good, wouldn’t change a thing.
BUT…
Eyeballs don’t pay the bills.
CUSTOMERS do.
And studies show that 7 out of 10 visitors to a site like uniquesymbol.com will drop by, take a gander, and then head for the hills without doing anything else.
It’s like they never were even there.
You can fix this.
You can make it super-simple for them to raise their hand, say, “okay, let’s talk” without requiring them to even pull their cell phone from their pocket… thanks to Web Visitors Into Leads.
Web Visitors Into Leads is a software widget that sits on your site, ready and waiting to capture any visitor’s Name, Email address and Phone Number. It lets you know immediately – so you can talk to that lead immediately… without delay… BEFORE they head for those hills.
CLICK HERE http://jumboleadmagnet.com to try out a Live Demo with Web Visitors Into Leads now to see exactly how it works.
Now it’s also true that when reaching out to hot leads, you MUST act fast – the difference between contacting someone within 5 minutes versus 30 minutes later is huge – like 100 times better!
That’s what makes our new SMS Text With Lead feature so powerful… you’ve got their phone number, so now you can start a text message (SMS) conversation with them… so even if they don’t take you up on your offer right away, you continue to text them new offers, new content, and new reasons to do business with you.
This could change everything for you and your business.
CLICK HERE http://jumboleadmagnet.com to learn more about everything Web Visitors Into Leads can do and start turing eyeballs into money.
Eric
PS: Web Visitors Into Leads offers a FREE 14 days trial – you could be converting up to 100x more leads immediately!
It even includes International Long Distance Calling.
Paying customers are out there waiting.
Starting connecting today by CLICKING HERE http://jumboleadmagnet.com to try Web Visitors Into Leads now.
If you’d like to unsubscribe click here http://jumboleadmagnet.com/unsubscribe.aspx?d=uniquesymbol.com