• Jodi Mahoney 18
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies
Please help!  I am still a novice when it comes to Apex.  I am trying to grab the last activity type and populate it on the lead.  The code is working however I am getting duplicate value errors a few times a day and I would like to clean up my code so that doesn't continue to happen.

Can someone take a look and help me out?
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
trigger LastActivityType on Task (after insert,before update) {
 
 Set<Id> LeadIds = new Set<Id>();
 List<Lead> LeadList = new List<Lead>();

for(Task t :trigger.new)
    {
  if(t.whoId!=null)
  {
        Schema.SObjectType tType= t.whoId.getSObjectType();
        if(tType == Lead.Schema.SObjectType)
        {
            LeadIds.add(t.WhoId);
        }
    }
  }
    {
  //Querying the related Lead based on whoId on Task
  Map<Id,Lead> LeadMap =  new Map<Id,Lead>([select id,Last_Activity_Type__c from Lead where id in:LeadIds]);
  
  for(Task t :Trigger.new)

    for(Lead l : LeadMap.Values())
    {
      l.Last_Activity_Type__c = t.Type;
      LeadList.add(l);
    }
  }
  // updating the Lead
  if(LeadList.size()>0)
  {
    update LeadList;
  }
}

Thank you in advance!
Please help!  I am still a novice when it comes to Apex.  I am trying to grab the last activity type and populate it on the lead.  The code is working however I am getting duplicate value errors a few times a day and I would like to clean up my code so that doesn't continue to happen.

Can someone take a look and help me out?
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
trigger LastActivityType on Task (after insert,before update) {
 
 Set<Id> LeadIds = new Set<Id>();
 List<Lead> LeadList = new List<Lead>();

for(Task t :trigger.new)
    {
  if(t.whoId!=null)
  {
        Schema.SObjectType tType= t.whoId.getSObjectType();
        if(tType == Lead.Schema.SObjectType)
        {
            LeadIds.add(t.WhoId);
        }
    }
  }
    {
  //Querying the related Lead based on whoId on Task
  Map<Id,Lead> LeadMap =  new Map<Id,Lead>([select id,Last_Activity_Type__c from Lead where id in:LeadIds]);
  
  for(Task t :Trigger.new)

    for(Lead l : LeadMap.Values())
    {
      l.Last_Activity_Type__c = t.Type;
      LeadList.add(l);
    }
  }
  // updating the Lead
  if(LeadList.size()>0)
  {
    update LeadList;
  }
}

Thank you in advance!