+ Start a Discussion
Shauna Davies 72Shauna Davies 72 

I am working on a "count" trigger for tasks in a Contact. I want to count tasks initiated by User Profile. Here is the code:

trigger UpdateAEContactActivities on Task (after update) {

set<ID> ContactIds= new Set<ID>();
for(Task tt:trigger.new){
if(UserInfo.getProfileId()=='Standard Sales User'){
    if(tt.Subject.containsIgnoreCase('Call (Completed)') || tt.Subject.containsIgnoreCase('Call (Left Message)') || tt.Subject.containsIgnoreCase('Call (No Answer)') || tt.Subject.containsIgnoreCase('Call (Incoming)') || tt.Subject.containsIgnoreCase('Email - &') || tt.Subject.containsIgnoreCase('LinkedIn') || tt.Subject.containsIgnoreCase('[Sendbloom]')){
        if(tt.Status.equals('Completed')){
            ContactIDs.add(tt.whoID);
        }
    }
}
}

if(ContactIds.isEmpty())
return;

 if(stoprecurssionupdate.runonce()){
List<Contact> cnts= new List<Contact>();
Integer number1=1;
for(Contact ct:[select id, name, AE_Activities_Counter__c  from Contact where id in:Contactids])
{

if(ct.AE_Activities_Counter__c ==null)
ct.AE_Activities_Counter__c=0;
ct.AE_Activities_Counter__c=ct.AE_Activities_Counter__c+number1;
cnts.add(ct);
}

if(cnts.size()>0)
{
update cnts;
}
}}

The code keeps returning an error on line 5...  I know I don't have the syntax correct....
Best Answer chosen by Shauna Davies 72
@anilbathula@@anilbathula@
Hi Shauna Davies 72,

You are checking the profileid with name .
change this condition if(UserInfo.getProfileId()=='Standard Sales User'){  in place of Profile name put profileid.

Thanks
Anil.B

All Answers

@anilbathula@@anilbathula@
Hi Shauna Davies 72,

You are checking the profileid with name .
change this condition if(UserInfo.getProfileId()=='Standard Sales User'){  in place of Profile name put profileid.

Thanks
Anil.B
This was selected as the best answer
Shauna Davies 72Shauna Davies 72
I fixed that, thank you!
It still does not update the field by an increment of 1, when I add a Task....    I am not sure why....
trigger UpdateAEContactActivities on Task (after update) {

set<ID> ContactIds= new Set<ID>();
for(Task tt:trigger.new){
if(UserInfo.getProfileId()=='00e00000006t1rl'){
    if(tt.Subject.containsIgnoreCase('Call (Completed)') || tt.Subject.containsIgnoreCase('Call (Left Message)') || tt.Subject.containsIgnoreCase('Call (No Answer)') || tt.Subject.containsIgnoreCase('Call (Incoming)') || tt.Subject.containsIgnoreCase('Email - &') || tt.Subject.containsIgnoreCase('LinkedIn') || tt.Subject.containsIgnoreCase('[Sendbloom]')){
        if(tt.Status.equals('Completed')){
            ContactIDs.add(tt.whoID);
        }
    }
}
}

if(ContactIds.isEmpty())
return;

 if(stoprecurssionupdate.runonce()){
List<Contact> cnts= new List<Contact>();
Integer number1=1;
for(Contact ct:[select id, name, AE_Activities_Counter__c  from Contact where id in:Contactids])
{

if(ct.AE_Activities_Counter__c ==null)
ct.AE_Activities_Counter__c=0;
ct.AE_Activities_Counter__c=ct.AE_Activities_Counter__c+number1;
cnts.add(ct);
}

if(cnts.size()>0)
{
update cnts;
}
}}
 
@anilbathula@@anilbathula@
Hi Shauna Davies 72,

Change the trigger to after insert and try once.

Thanks
Anil.B
Shauna Davies 72Shauna Davies 72
Nope that didn't do it......  I am lost on this one....
@anilbathula@@anilbathula@
Hi Shauna Davies 72,

Try these code,there might be some typo errors :-
 
trigger UpdateAEContactActivities on Task (after insert,after update) {

set<ID> ContactIds= new Set<ID>();
for(Task tt:trigger.new){
	if(UserInfo.getProfileId()=='Standard Sales User'){
		if(tt.Subject.containsIgnoreCase('Call (Completed)') || 
		tt.Subject.containsIgnoreCase('Call (Left Message)') ||
		tt.Subject.containsIgnoreCase('Call (No Answer)') || 
		tt.Subject.containsIgnoreCase('Call (Incoming)') || 
		tt.Subject.containsIgnoreCase('Email - &') || 
		tt.Subject.containsIgnoreCase('LinkedIn') || 
		tt.Subject.containsIgnoreCase('[Sendbloom]')){
			if(tt.Status.equals('Completed')){
				ContactIDs.add(tt.whoID);
			}
		}
	}
}

if(ContactIds.isEmpty())
return;

	if(stoprecurssionupdate.runonce()){
		List<Contact> cnts= new List<Contact>();
		Integer number1=1;
		for(Contact ct:[select id, name,(select id,Subject,RecordTypeId,createdDate from Tasks), AE_Activities_Counter__c  from Contact where id in:Contactids])
		{
		ct.AE_Activities_Counter__c=ct.tasks.size();
		cnts.add(ct);
		}

		if(cnts.size()>0)
		{
		update cnts;
	    }
    }
}

Thanks
Anil.B
Shauna Davies 72Shauna Davies 72
Hello Anil,

The code did not work, and I am also receiving errors on the Profile ID:  " duplicate values on record with ID"......I tired it with the Profile ID set to "Standard Sales User" and " 00e00000006t1rl".

Shauna
@anilbathula@@anilbathula@
Hi Shauna Davies,

Can you paste the full error message and your code.

Thanks
Anil.B
Shauna Davies 72Shauna Davies 72
User-added image