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
schmichaelschmichael 

Call Count Trigger

Hello All,

 

I'm trying to create a variable that tracks the number of calls performed on a lead. I thought this would be really simple to do as a formula or with workflow rules, but I couldn't actually figure out a simple way to do it without using Apex. I modified an existing trigger I had created, and I thought I had a functional piece of code, but it doesn't work at all. What I have right now looks like this:

 

trigger callCountUpdate on Task (after insert) {
map<id,lead> leads = new map<id,lead>(), updates = new map<id,lead>();
for(task record:trigger.new)
if(record.whoid!=null&&record.whoid.getsobjecttype()==lead.sobjecttype)
leads.put(record.whoid,null);
leads.putall([select id,call_count__c from lead where id in :leads.keyset()]);
for(task record:trigger.new){
if(record.type == 'Call'){
updates.put(record.whoid,new lead(id=record.whoid,call_count__c = leads.get(record.whoid).call_count__c++));
}
}
update updates.values();
}

 

Can anyone help me figure this out, or just tell me a way to track this without using Apex?

 

Thanks very much!

Sean TanSean Tan

Not sure if this is the complete code, you're not declaring an "updates" variable anywhere so I'm not sure how it would compile.

 

Something like this should work:

 

trigger callCountUpdate on Task (after insert) {
	map<id,lead> leads = new map<id,lead>(), updates = new map<id,lead>();
	
	for(task record:trigger.new)
	{
		if(record.type == 'Call' && record.whoid!=null && record.whoid.getsobjecttype()==lead.sobjecttype)	
		{
			leads.put(record.whoid, null);
		}
	}
			
	if (!leads.isEmpty())
	{
		leads.putall([select id,call_count__c from lead where id in :leads.keyset()]);
		
		for(task record:trigger.new) {
			if(record.type == 'Call') {
				Lead item = leads.get(record.whoid);
				
				//Should never be the case...
				if (item != null)
				{
					if (item.Call_Count__c == null)
					{
						item.Call_Count__c = 0;
					}
					item.Call_Count__c++;
				}						
			}
		}
		update leads.values();
	}
}