• Cynthia Hancock
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
Hi All, 

I currently have this trigger to count how many Activities are on Lead:
trigger TaskUpdateLead on Task (after delete, after insert, after undelete, after update) {

Set<ID> LeadIds = new Set<ID>();

//We only care about tasks linked to Leads.

String leadPrefix = Lead.SObjectType.getDescribe().getKeyPrefix();

//Add any Lead ids coming from the new data

if(trigger.new!=null){
    for (Task t : Trigger.new) {
     if (t.WhoId!= null && string.valueof(t.WhoId).startsWith(leadPrefix) ) {

if(!LeadIds.contains(t.WhoId)){
//adding unique lead ids since there can be many tasks with single lead
LeadIds.add(t.WhoId);
}
}
      }
}
 
//Also add any Lead ids coming from the old data (deletes, moving an activity from one Lead to another)

if(trigger.old!=null){
    for (Task t2 : Trigger.old) {
     if (t2.WhoId!= null && string.valueof(t2.WhoId).startsWith(leadPrefix) )
         {
if(!LeadIds.contains(t2.WhoId)){
//adding unique lead ids since there can be many tasks with single lead
LeadIds.add(t2.WhoId);
}
}
      }
}

     if (LeadIds.size() > 0){



List<Lead> leadsWithTasks = [select id,Activity_Count__c,(select id from Tasks) from Lead where Id IN : Leadids];

List<Lead> leadsUpdatable = new List<Lead>();

for(Lead L : leadsWithTasks){

L.Activity_Count__c = L.Tasks.size();
leadsUpdatable.add(L);

}

if(leadsUpdatable.size()>0){

update leadsUpdatable;
//update all the leads with activity count

}

    }
}

I would like to modify this trigger to count the total number of views from the tasks to a custom field View_Sum__c on the lead. Below is the screenshot of activities with View count. I would like to put the sum (in this case, 5) in a field on the lead(View_Sum__c). 

View count in Activities

Thank you for your help!
Hi All, 

New to writing triggers and test classes. Can anyone help me write a test class for the trigger below?
trigger lastactassigned on task(after insert) {
  map<id,contact> contacts = new map<id,contact>();
  map<id,account> accounts = new map<id,account>();
  for(task record:trigger.new) {
    if(record.ownerid.getsobjecttype()==user.sobjecttype) {
      if(record.whoid!=null&&record.whoid.getsobjecttype()==contact.sobjecttype) {
        contacts.put(record.whoid,new contact(id=record.whoid,LastActivityAssignedTo__c=record.ownerid));
      }
      if(record.accountid!=null) {
        accounts.put(record.accountid,new account(id=record.accountid,LastActivityAssignedTo__c=record.ownerid));
      }
    }
  }
  update contacts.values();
  update accounts.values();
}

Thank you!
Hi All, 

New to writing triggers and test classes. Can anyone help me write a test class for the trigger below?
trigger lastactassigned on task(after insert) {
  map<id,contact> contacts = new map<id,contact>();
  map<id,account> accounts = new map<id,account>();
  for(task record:trigger.new) {
    if(record.ownerid.getsobjecttype()==user.sobjecttype) {
      if(record.whoid!=null&&record.whoid.getsobjecttype()==contact.sobjecttype) {
        contacts.put(record.whoid,new contact(id=record.whoid,LastActivityAssignedTo__c=record.ownerid));
      }
      if(record.accountid!=null) {
        accounts.put(record.accountid,new account(id=record.accountid,LastActivityAssignedTo__c=record.ownerid));
      }
    }
  }
  update contacts.values();
  update accounts.values();
}

Thank you!