• Thomas Graves 1
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 9
    Replies
Hi Everyone,

This is the first trigger that I've written for one of our teams. I've reviewed other related community posts which have helped me get to this point but now I'm at a wall and would like some assistance. 

*When customers are emailed and the sales rep includes the email-to-salesforce bcc address, the email is added to the 'Activity History' of the 'Account' as a 'Task'. The trigger will look at all new Tasks and if they are 1) of type 'Email' and 2) related to customer accounts then it will update a custom field 'CS Last Contact Date' with the created date of the task.

Things I've noticed from the logs:
A) Tasks created through the email-to-salesforce feature and assigned to Accounts don't have whatIds. Anyone know why this is?
B) The trigger doesn't throw any errors
trigger UpdateAccountLastCSContactDate on Task (after insert, after update) {

    
    /*Set of whatIds for objects that have tasks */
    Set<String> whatIDs = new Set<String>();
    for (Task t : Trigger.new) {
            whatIDs.add(t.whatID);
    }

    /*List of customer accounts that have tasks */
    List<Account> customerAccounts = [SELECT Id, Type, CS_Last_Contact_Date__c FROM Account WHERE Type = 'Customer'];
    
    /*Create map of Email Task <-> Related Object*/
    Map<String, Task> taskMap = new Map<String, Task>();
    for (Task t : Trigger.new){
        if (t.type == 'Email'){
            taskMap.put(t.whatID, t);   
        }
    }
    
    for (Account customerAcct : customerAccounts){
        /*if customer account id is in the taskmap then see if last contact date is less than task created date */
        if (taskMap.containsKey(customerAcct.Id)){ 
            if (taskMap.get(customerAcct.Id).CS_Contact_Date__c != NULL){
                if (customerAcct.CS_Last_Contact_Date__c < taskMap.get(customerAcct.Id).CS_Contact_Date__c){
                    /*sets last contact date to the created date of the task */
                    customerAcct.CS_Last_Contact_Date__c = taskMap.get(customerAcct.Id).CS_Contact_Date__c;
                }   
            }
        }
    }
    update customerAccounts;
}

Any help is GREATLY appreciated. Thanks,
Thomas
 
Hi Everyone,

I have a custom Quote Field, lets call it 'Count'. I want the value of Count to come from looking at all the names of the products in the Quote Line Items and generating a value. I've figured a few things out on my own but my resources are running dry. Below is everything I've figured out

QuoteLineItem - the name of the object containing each product
Product2 - the product name that is displayed in QuoteLineItem
Count - this should be a custom formula field. the formula should look something like what I have below ->

count = 0;
For (all QuoteLineItem:x in Quote object){
    If (x.Product2 = 'ABC')
        count = count + 5;
    If (x.Product2 = 'DEF')
        count = count + 10;
     If (x.Product2 = 'GHI')
        count = count + 5;
}

Am I thinking about this right? Can someone help me pull this thing together.

Thanks!
T




 
Hi Everyone,

This is the first trigger that I've written for one of our teams. I've reviewed other related community posts which have helped me get to this point but now I'm at a wall and would like some assistance. 

*When customers are emailed and the sales rep includes the email-to-salesforce bcc address, the email is added to the 'Activity History' of the 'Account' as a 'Task'. The trigger will look at all new Tasks and if they are 1) of type 'Email' and 2) related to customer accounts then it will update a custom field 'CS Last Contact Date' with the created date of the task.

Things I've noticed from the logs:
A) Tasks created through the email-to-salesforce feature and assigned to Accounts don't have whatIds. Anyone know why this is?
B) The trigger doesn't throw any errors
trigger UpdateAccountLastCSContactDate on Task (after insert, after update) {

    
    /*Set of whatIds for objects that have tasks */
    Set<String> whatIDs = new Set<String>();
    for (Task t : Trigger.new) {
            whatIDs.add(t.whatID);
    }

    /*List of customer accounts that have tasks */
    List<Account> customerAccounts = [SELECT Id, Type, CS_Last_Contact_Date__c FROM Account WHERE Type = 'Customer'];
    
    /*Create map of Email Task <-> Related Object*/
    Map<String, Task> taskMap = new Map<String, Task>();
    for (Task t : Trigger.new){
        if (t.type == 'Email'){
            taskMap.put(t.whatID, t);   
        }
    }
    
    for (Account customerAcct : customerAccounts){
        /*if customer account id is in the taskmap then see if last contact date is less than task created date */
        if (taskMap.containsKey(customerAcct.Id)){ 
            if (taskMap.get(customerAcct.Id).CS_Contact_Date__c != NULL){
                if (customerAcct.CS_Last_Contact_Date__c < taskMap.get(customerAcct.Id).CS_Contact_Date__c){
                    /*sets last contact date to the created date of the task */
                    customerAcct.CS_Last_Contact_Date__c = taskMap.get(customerAcct.Id).CS_Contact_Date__c;
                }   
            }
        }
    }
    update customerAccounts;
}

Any help is GREATLY appreciated. Thanks,
Thomas
 
Hey, hope someone could help debugging this code. Every email we sent to customers is bcc'd to Salesforce, so they are being attached to the relevant account/contact. For one specific email, we would like to display the time the email has been sent to the customer. This works fine in any simple/manual test, but fails in production on a significant big amount of customer who receive the email (30%+), in which case the datestamp is just missing.
I'm not sure if we exceed any limits (no error message) and don't know how to debug, as the trigger works in theory. Thanks for the help, all if, else if conditions are mutual exclusive.
 
trigger Trigger_Task2Account on Task (after insert, after update) { //trigger name, on which object, when does it fire 
  Task[] tasks = Trigger.New;
  
  Map<String, Task> accountMap = new Map<String, Task>();
  
  for(Task task: tasks)
  {
    String accountId = task.AccountId; // maps object where triggers fire to object where result resides
    if (accountId != null)
      accountMap.put(task.WhatId, task);
  }
  List<Account> accounts = new List<Account>([Select Id, Doc_Chaser__c, Last_Call_Purpose__c, Last_Call_Outcome__c , Last_Call_Date__c, Latest_Opportunity__c, Required_Documents_Email__c, Last_Call_Result__c, Customer_Process__c FROM Account WHERE Id in :accountMap.keySet()]);
  
  for(Account account: accounts)
  {   
        Task task = accountMap.get(account.Id);
        if(task.Purpose__c != null || task.Call_Outcome__c != null) //conditions 
        {
            account.Last_Call_Purpose__c = task.Purpose__c;  //what: object.field__c, true, false, 'string', datetime.function
            account.Last_Call_Outcome__c = task.Call_Outcome__c;
            account.Last_Call_Date__c = task.CreatedDate;
        }
        
        else if(task.Subject == 'Email: Completing your iwoca application...')
        {
          account.Required_Documents_Email__c = datetime.NOW();
        }
        
        else if(task.Subject == 'Telesales Call')
        {
          if(task.CallDisposition == 'Sale - Application Review' && account.Customer_Process__c == '5 - Live in Dialler')
          {
            account.Last_Call_Result__c = task.CallDisposition; //CallDisposistion is API name for Call Result
            account.Last_Call_Date__c = task.CreatedDate;
            account.Customer_Process__c = '1 - Application Review';
            update account;
          }
          else
          {
          account.Last_Call_Result__c = task.CallDisposition; //CallDisposistion is API name for Call Result
          account.Last_Call_Date__c = task.CreatedDate;
          }
        }
    }
    update accounts;
}

 
using bcc to salesforce, and i have to create a new task when mail comes with specified subject. I had written a trigger on Task. In case when i am sending bcc to lead its working fine but in case of Contact whoId is always coming as null, i tried with both after insert and after update.

This is trigger I had written:
trigger bccTrigger on Task (after insert, after update) {

    //TaskTriggerHelper.createNewTask(Trigger.new);
    List<Task> tnew=trigger.new;
    List<Task> tc=[Select Id,Subject,Dummy_Number__c,WhoId from Task where id in:tnew];
    List<Task> tk=new List<Task>();
    If(Trigger.isInsert){
        if(Trigger.isAfter){
        for(Task t:tc){
        system.debug('............Insert.......'+t.WhoId+'.....'+t.Id);
            if((t.Subject).equals('Email: GroupBy / Searchandiser Introduction')){
                t.Dummy_Number__c=t.Dummy_Number__c+1;
                }
            }
            update tc;
        }
    }

    if(Trigger.isUpdate){
        if(Trigger.isAfter){
            for(Task t:tc){
            system.debug('............Update.......'+t.WhoId+'.....'+t.Id);
            if((t.Subject).equals('Email: GroupBy / Searchandiser Introduction')){
                Task tadd=new Task();
                tadd.Subject='Test1';
                tadd.WhoId=t.WhoId;
                tk.add(tadd);
                }
            }
            insert tk;
        }
    }

}
Hi Everyone,

I have a custom Quote Field, lets call it 'Count'. I want the value of Count to come from looking at all the names of the products in the Quote Line Items and generating a value. I've figured a few things out on my own but my resources are running dry. Below is everything I've figured out

QuoteLineItem - the name of the object containing each product
Product2 - the product name that is displayed in QuoteLineItem
Count - this should be a custom formula field. the formula should look something like what I have below ->

count = 0;
For (all QuoteLineItem:x in Quote object){
    If (x.Product2 = 'ABC')
        count = count + 5;
    If (x.Product2 = 'DEF')
        count = count + 10;
     If (x.Product2 = 'GHI')
        count = count + 5;
}

Am I thinking about this right? Can someone help me pull this thing together.

Thanks!
T




 
Hey everyone,

I have a task trigger that depends entirely upon the value in the WhoID field upon creation. I noticed that if a task is created via a BCC'd email, the trigger doesn't work. After putting in some debug statements, I saw that WhoID isn't populated upon creation when the task if from a BCC'd email.

Any idea what I should do?

Thanks,
Greg
Hey Everyone,

I have a task trigger that creates a bunch of records once a task is inserted. This trigger works fine when I manually create a task in Salesforce. But if we use the BCC-to-Salesforce address to log an email as a task, the trigger doesn't seem to run. I've tried manually creating tasks that have the same exact same values as the tasks created via BCC, and the manuallly created tasks will run the trigger while the BCC-created tasks do not.

Is there something I need to take into account? Does BCCing simply not support triggers?


Here is the class I'm testing:

public class ClassRelatedActivitiesInsert {
    
    public void insertRelatedActivities(List<Task> tasks){
        
        Set<String> accountsInTrigger = new Set<String>();
        Set<String> contactsInTrigger = new Set<String>();
        Set<String> parentSet = new Set<String>();
        Set<String> clientSet = new Set<String>();
        Set<String> agencySet = new Set<String>();
        Set<String> siblingAccounts = new Set<String>();
        
        Map<String,String> contactMap = new Map<String,String>();
        Map<String,String> parentMap = new Map<String,String>();
        Map<String,String> siblingParentMap = new Map<String,String>();
        Map<String,String> clientAgencyMap = new Map<String,String>();
        Map<String,String> agencyClientMap = new Map<String,String>();
        
        List<Account> childAccounts = new List<Account>();
        List<Client_Agency_Relationship__c> clientAgencies = new List<Client_Agency_Relationship__c>();
        List<Related_Activity__c> relatedActivitiesToAdd = new List<Related_Activity__c>();
        
        
    FOR(Task t : tasks){        
                IF(t.WhatId != NULL && string.valueOf(t.WhatId).startsWith('001')){
                    accountsInTrigger.add(t.WhatId);
                }
                ELSE IF(t.WhatId == NULL && t.WhoId != NULL && string.valueOf(t.WhoId).startsWith('003')){
                        contactsInTrigger.add(t.WhoId);
                    }       
        }
        
        System.debug(LoggingLevel.ERROR,'INSERT: Heap size after initial task loop is ' + Limits.getHeapSize() + '/' + Limits.getLimitHeapSize());
        System.debug(LoggingLevel.ERROR,'INSERT: CPU time after initial task loop is ' + Limits.getCpuTime() + '/' + Limits.getLimitCpuTime());
        
        IF(contactsInTrigger.size() > 0){
            FOR(Contact c : [SELECT
                                Id,AccountId
                             FROM
                                Contact
                             WHERE
                                Id In :contactsInTrigger
                             AND
                              AccountId != NULL]){
                                 
                                     contactMap.put(c.Id,c.AccountId);
                                     accountsInTrigger.add(c.AccountId);
                                 
                                }
        }
        
        System.debug(LoggingLevel.ERROR,'INSERT: Heap size after account contacts SOQL is ' + Limits.getHeapSize() + '/' + Limits.getLimitHeapSize());
        System.debug(LoggingLevel.ERROR,'INSERT: CPU time after account contacts SOQL is ' + Limits.getCpuTime() + '/' + Limits.getLimitCpuTime());
        
        IF(accountsInTrigger.size() > 0){
            FOR(Account ca : [SELECT 
                                 Id,ParentId
                              FROM 
                                 Account
                              WHERE
                                 ParentId In :accountsInTrigger
                              OR
                                 (ParentId != NULL
                              AND
                                  Id In :accountsInTrigger)]){
                                      
                                      IF(accountsInTrigger.contains(ca.ParentId)){
                                          childAccounts.add(ca);
                                      }
                                      IF(accountsInTrigger.contains(ca.Id) && ca.ParentId != NULL){
                                          parentMap.put(ca.Id,ca.ParentId);
                                          parentSet.add(ca.ParentId);
                                      }
                                      
                                  }
        }
        
        System.debug(LoggingLevel.ERROR,'INSERT: Heap size after parent/child account SOQL is ' + Limits.getHeapSize() + '/' + Limits.getLimitHeapSize());
        System.debug(LoggingLevel.ERROR,'INSERT: CPU time after parent/child account SOQL is ' + Limits.getCpuTime() + '/' + Limits.getLimitCpuTime());
        
        IF(parentSet.size() > 0){
            FOR(Account sa : [SELECT
                                 Id, ParentId
                              FROM
                                 Account
                              WHERE
                                 ParentId In :parentSet]){
                                  
                                    siblingParentMap.put(sa.Id,sa.ParentId);
                                    siblingAccounts.add(sa.Id);
                                  
                                }
        }
        
        System.debug(LoggingLevel.ERROR,'INSERT: Heap size after sibling SOQL is ' + Limits.getHeapSize() + '/' + Limits.getLimitHeapSize());
        System.debug(LoggingLevel.ERROR,'INSERT: CPU time after sibling SOQL is ' + Limits.getCpuTime() + '/' + Limits.getLimitCpuTime());
        
        IF(accountsInTrigger.size() > 0){
            clientAgencies = [SELECT
                                 Id,Client__c,Agency__c
                              FROM
                                 Client_Agency_Relationship__c
                              WHERE
                                 Client__c In :accountsInTrigger
                              OR
                                 Agency__c In :accountsInTrigger];
        }
        
        
        System.debug(LoggingLevel.ERROR,'INSERT: Heap size after clientAgencies SOQL is ' + Limits.getHeapSize() + '/' + Limits.getLimitHeapSize());
        System.debug(LoggingLevel.ERROR,'INSERT: CPU time after clientAgencies SOQL is ' + Limits.getCpuTime() + '/' + Limits.getLimitCpuTime());
            
        FOR(Task t0 : tasks){
            
            String accountID0;
            String parentID0;
                
                  IF(t0.WhatId != NULL){
                          accountID0 = t0.WhatId;
                          parentID0 = parentMap.get(t0.WhatId);
                    }
                                        
                    ELSE IF(t0.WhatId == NULL && t0.WhoId != NULL && contactMap.get(t0.WhoId) != NULL){
                          accountID0 = contactMap.get(t0.WhoId);
                          parentID0 = parentMap.get(contactMap.get(t0.WhoId));
                    }
        
            FOR(Account a : childAccounts){
                
                IF(a.ParentId == accountID0){
                    
                    Related_Activity__c ra0 = new Related_Activity__c();
                    
                    IF(t0.Subject.length() > 80){
                        ra0.Name = t0.Subject.SubString(0,80);
                    }
                    ELSE{
                        ra0.Name = t0.Subject;
                    }
                    
                    IF(t0.WhoId != NULL && string.valueOf(t0.WhoId).startsWith('003')){
                        ra0.Name__c = t0.WhoId;
                    }
                    
                        ra0.Date__c = t0.ActivityDate;
                        ra0.Description__c = t0.Description;
                        ra0.Assigned_To__c = t0.OwnerId;
                        ra0.Activity_ID__c = t0.Id;
                        ra0.Relationship__c = 'Parent';
                        ra0.Account__c = a.Id;
                        ra0.Related_To__c = accountID0;
                
                    relatedActivitiesToAdd.add(ra0);
                    
                }
                
            }
            
            FOR(String a1 : siblingAccounts){
                    
                    IF(siblingParentMap.get(a1) == parentID0 && a1 != accountID0){
                    
                    Related_Activity__c ra1 = new Related_Activity__c();
                        
                        IF(t0.Subject.length() > 80){
                            ra1.Name = t0.Subject.SubString(0,80);
                        }
                        ELSE{
                            ra1.Name = t0.Subject;
                        }
                        
                        IF(t0.WhoId != NULL && string.valueOf(t0.WhoId).startsWith('003')){
                          ra1.Name__c = t0.WhoId;
                      }
                        
                    ra1.Date__c = t0.ActivityDate;
                    ra1.Description__c = t0.Description;
                    ra1.Assigned_To__c = t0.OwnerId;
                  ra1.Activity_ID__c = t0.Id;
                    ra1.Name__c = t0.WhoId;
                    ra1.Relationship__c = 'Sibling';
                    ra1.Account__c = a1;
                    ra1.Related_To__c = accountID0;
                    
                    relatedActivitiesToAdd.add(ra1);
                        
                    }
                }
            
            FOR(Client_Agency_Relationship__c car0 : clientAgencies){
                
                IF(car0.Agency__c == accountID0){
                
                    Related_Activity__c ra2 = new Related_Activity__c();
                    
                    IF(t0.Subject.length() > 80){
                        ra2.Name = t0.Subject.SubString(0,80);
                    }
                    ELSE{
                        ra2.Name = t0.Subject;
                    }
                    
                    IF(t0.WhoId != NULL && string.valueOf(t0.WhoId).startsWith('003')){
                          ra2.Name__c = t0.WhoId;
                      }
                    
                    ra2.Date__c = t0.ActivityDate;
                    ra2.Description__c = t0.Description;
                    ra2.Assigned_To__c = t0.OwnerId;
                    ra2.Activity_ID__c = t0.Id;
                    ra2.Relationship__c = 'Agency';
                    ra2.Account__c = car0.Client__c;
                    ra2.Related_To__c = accountID0;
                
                    relatedActivitiesToAdd.add(ra2);
                    
                }
            }
            
            FOR(Client_Agency_Relationship__c car1 : clientAgencies){
                
                IF(car1.Client__c == accountID0){
                
                    Related_Activity__c ra3 = new Related_Activity__c();
                    
                    IF(t0.Subject.length() > 80){
                        ra3.Name = t0.Subject.SubString(0,80);
                    }
                    ELSE{
                        ra3.Name = t0.Subject;
                    }
                    
                    IF(t0.WhoId != NULL && string.valueOf(t0.WhoId).startsWith('003')){
                          ra3.Name__c = t0.WhoId;
                      }
                    
                    ra3.Date__c = t0.ActivityDate;
                    ra3.Description__c = t0.Description;
                    ra3.Assigned_To__c = t0.OwnerId;
                    ra3.Activity_ID__c = t0.Id;
                    ra3.Relationship__c = 'Client';
                    ra3.Account__c = car1.Agency__c;
                    ra3.Related_To__c = accountID0;
               
                    relatedActivitiesToAdd.add(ra3);
                    
                }
            }
            
            FOR(String a4 : parentSet){
                
                IF(parentID0 == a4){
                
                    Related_Activity__c ra4 = new Related_Activity__c();
                    
                    IF(t0.Subject.length() > 80){
                        ra4.Name = t0.Subject.SubString(0,80);
                    }
                    ELSE{
                        ra4.Name = t0.Subject;
                    }
                    
                    IF(t0.WhoId != NULL && string.valueOf(t0.WhoId).startsWith('003')){
                          ra4.Name__c = t0.WhoId;
                      }
                    
                    ra4.Date__c = t0.ActivityDate;
                    ra4.Description__c = t0.Description;
                    ra4.Assigned_To__c = t0.OwnerId;
                    ra4.Activity_ID__c = t0.Id;
                    ra4.Relationship__c = 'Child';
                    ra4.Account__c = a4;
                    ra4.Related_To__c = accountID0;
               
                    relatedActivitiesToAdd.add(ra4);
                    
                }
          }
       
      } 
           
        System.debug(LoggingLevel.ERROR,'INSERT: Heap size after related activities creation is ' + Limits.getHeapSize() + '/' + Limits.getLimitHeapSize());
        System.debug(LoggingLevel.ERROR,'INSERT: CPU time after related activities creation is ' + Limits.getCpuTime() + '/' + Limits.getLimitCpuTime());
        
        
        IF(relatedActivitiesToAdd.size() > 0){
          INSERT relatedActivitiesToAdd;
        }
        
        System.debug(LoggingLevel.ERROR,'INSERT: Heap size after trigger completion is ' + Limits.getHeapSize() + '/' + Limits.getLimitHeapSize());
        System.debug(LoggingLevel.ERROR,'INSERT: CPU time after trigger completion is ' + Limits.getCpuTime() + '/' + Limits.getLimitCpuTime());
        
    }

}

And here is our task trigger, which calls all the classes:

trigger MainTriggerTask on Task (after insert, after update) {
    
    IF(Trigger.IsAfter){
        IF(Trigger.IsInsert){
            
            if(checkRecursive.runAfterInsertOnce()){
                
                ClassLeadContacted updater = new ClassLeadContacted();
                updater.updateLastContacted(Trigger.new);
                
                ClassLibraryTaskEmail updater1 = new ClassLibraryTaskEmail();
                updater1.sendEmail(Trigger.new);
                
                ClassRelatedActivitiesInsert updater2 = new ClassRelatedActivitiesInsert();
                updater2.insertRelatedActivities(Trigger.new);
                
            }
        }
        IF(Trigger.IsUpdate){
            
            if(checkRecursive.runAfterUpdateOnce()){
                
                ClassLeadContacted updater1 = new ClassLeadContacted();
                updater1.updateLastContacted(Trigger.new);
                
                ClassRelatedActivities updater2 = new ClassRelatedActivities();
                updater2.addRelatedActivities(Trigger.new,Trigger.oldMap);
                
                ClassTaskUpdateRelatedActivity updater3 = new ClassTaskUpdateRelatedActivity();
                updater3.updateRelatedActivity(Trigger.new,Trigger.oldMap);
                
            }
        }
    }
}

Let me know if you need any other info from me.

Thanks!
-Greg