• Tom Caesar
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
A custom field named Calls_Made__c is numeric and increments by 1 each time an outgoing call task is completed.

This happens in two areas, on the Lead and on the Account (opportunity) - (seperate counts for each)

The Lead update works fine, but am having trouble with the acount count incrementing on the opportunity task completion.
 
trigger UpdateLeadOpenTasks on Task (after delete, after insert, after undelete, after update) {
 
System.Debug('Runnning UpdateLeadOpenTasks');
 
// Declare the variables
 
public set<Id> LeadIDs = new Set<Id>();
public list<Lead> LeadsToUpdate = new List<Lead>();
public set<Id> AccountIDs = new Set<Id>();
public list<Account> AccountsToUpdate = new List<Account>();
 
// Build the list of Leads and Accounts to update
 
if(Trigger.isInsert || Trigger.isUnDelete || Trigger.isUpdate){
 
System.Debug('Creating a list of IDs to update');
 
    for(Task t: Trigger.new){       
    System.Debug(t.WhoId);
  
        if(t.WhoId !=null) {
        //We need to put something here to handle a fail if the user doesn't link an activity to a Account or Lead

            if(string.valueof(t.WhoId).startsWith('003')) {
                // nothing with contact
                System.Debug('No Task WhoID set for Contact');
            } else if(string.valueof(t.WhoId).startsWith('00Q')) {
                LeadIDs.add(t.WhoId);
            } else {
                AccountIDs.add(t.WhoId);
            }

    
        }
        else {
        System.Debug('No Task WhoID set');
        }
 
    } //end for
}
if(Trigger.isDelete){
    for(Task t: Trigger.old){
        if(string.valueOf(t.WhoId).startsWith('00Q')){
            // leads
            LeadIDs.add(t.WhoId);
         }else if(string.valueOf(t.WhoId).startsWith('003')){
            // nothing for contact
            System.Debug('No Task Trigger set for Contacts'); 
        } else {
            // account
            AccountIDs.add(t.WhoId);
        }
    }
}
 
// Update the Leads
 
if(LeadIDs.size()>0){
   System.Debug('Lead has at least 1 record - doing something');
   for(Lead l: [Select l.Id, l.Calls_Made__c,
   (Select Id From Tasks where CallType='Outbound' and Status='Completed') //Change   select statement to just be NVM Tasks
   From Lead l where Id in :LeadIDs])
   LeadsToUpdate.add(new Lead(Id=l.Id, Calls_Made__c = l.Tasks.size()));
   update LeadsToUpdate;
 
}
 
// Update the Accounts
if(AccountIDs.size()>0){
System.Debug('Account has at least 1 record - doing something');
//We need to put in code to handle it if we don't know who the Account is
 
for(Account c: [Select c.Id, c.Calls_Made__c,
(Select Id From Tasks where CallType='Outbound' and Status='Completed')//Change select statement to just be NVM Tasks
From Account c where Id in :AccountIDs])
AccountsToUpdate.add(new Account(Id=c.Id, Calls_Made__c = c.Tasks.size()));
update AccountsToUpdate;
}
 
}

 
Leads currently have 4 custom objects.

When converted and an oppurtunity created, all entries in each of the 4 custom object entries need to be mapped to the account:

{Object1}Lead->Applicant_2_Lead__c [mapped to] {Object1}PersonAccount->Applicant_2__c
{Object2}Lead->Employee_lead__c [mapped to] {Object2}PersonAccount->Employee__c
{Object3}Lead->Liability_Lead__c [mapped to] {Object3}PersonAccount->Liability__c
{Object4}Lead->Reference_Lead__c [mapped to] {Object4}PersonAccount->Reference__c

I am only learning SF but do have a background in php & mysql.

If people could aim in the direction so I can learn the process I need to understand or tutorials etc, that be appreciated. 
Leads currently have 4 custom objects.

When converted and an oppurtunity created, all entries in each of the 4 custom object entries need to be mapped to the account:

{Object1}Lead->Applicant_2_Lead__c [mapped to] {Object1}PersonAccount->Applicant_2__c
{Object2}Lead->Employee_lead__c [mapped to] {Object2}PersonAccount->Employee__c
{Object3}Lead->Liability_Lead__c [mapped to] {Object3}PersonAccount->Liability__c
{Object4}Lead->Reference_Lead__c [mapped to] {Object4}PersonAccount->Reference__c

I am only learning SF but do have a background in php & mysql.

If people could aim in the direction so I can learn the process I need to understand or tutorials etc, that be appreciated.