• Syed Ahmed 10
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 0
    Replies
I have created a custom task trigger to update Custom number field on Task Object which counts number of time a task has been created for a lead record with the task record type. This is working fine. Now i want to add standard status field to Update a custom text field on the lead record with the last updated status with the same record type. I am stumped i cannot figure how to do it ! Need some help
The Task trigger is below:

 
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,Dialler_Status__c,Dialler_Count__c,(select id from Tasks where recordtype.name LIKE '%Dialler%') from Lead where Id IN : Leadids];

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

for(Lead L : leadsWithTasks){

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

}

if(leadsUpdatable.size()>0){

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

}

    }
}

 
I have made a custom field Contact ID to capture the Contact ID for person accounts. So to acheive this i have made a trigger, the trigger is working fine on insert but on before update its not working. Please let me know what am i doing wrong.

trigger UpdateContactID on Account (after insert, before update) 

{
List<Contact> contactList;
 if ((trigger.isInsert)  || (trigger.isUpdate && trigger.new[0].Contact_ID__c == null))
 {
     List<String> newAccountIDList = new List<String>();
     

     // Taking all account IDs in collection
     for(Account acct: Trigger.new)
     {  
      newAccountIDList.add(acct.ID); 
     }

     // Fetching contacts against the account IDs
     try {
         contactList = [SELECT Id, Account.Id FROM Contact WHERE Account.ID in :newAccountIDList and Account.IsPersonAccount = true];      
     } catch(exception ex){
            system.debug('UpdateContactID error - cant find Contact record:' + ex);
     }
                         


     // Adding contacts in a map with relation to Account ID
     Map<String, Contact> mapContact = new Map<String, Contact>();
     for(Contact cont : contactList)
     {
      mapContact.put(cont.Account.Id, cont) ;
     }

     if(!mapContact.values().isEmpty())
     {

     
       // Updating Contact_ID__c from Map to new Account list to update
       List<Account> toUpdate = new List<Account>();
       for(Contact con: contactList)
       {  
       
          toUpdate.add(new Account(
           id = con.Account.Id,
           Contact_ID__c = con.Id
          ));
       }
        if (trigger.isInsert)
        {
           update toUpdate;
        }
     }
  }
}