1

I am a newbie when it comes to Apex development, but I am trying to deploy a trigger that sends a task and an email when an opportunity is marked as won. Half of the trigger is working properly, but the most important half is not. Any help is greatly appreciated!

I have a custom field on the Account level named "Customer_Success_Owner_Name__c". The field is a formula that takes another field and returns the name "Brandon", "Kyle", or "Oz". Based on the name in that field, the trigger should create a task and send an email to the correct user, as well as do the same for our finance officer.

The "Open a Payment Record" task is sending correctly. The "Open a Subscription" is acting sporadic and not sending to the correct user when it does actually send.


Trigger:

/*Open a Task on Account on a closed won Opportunity */

trigger OpenAccountTask on Opportunity (after update){

    List<Id> AccountId = new List<Id>();

    for(Integer i = 0; i < trigger.size; i++){
        if(trigger.new[i].StageName == 'Closed Won' && trigger.new[i].StageName != trigger.old[i].StageName)
            AccountId.add(trigger.new[i].AccountId);
    }

    if(!AccountId.isEmpty()){
        Task[] tasksToInsert = new Task[]{};
        Map<Id,Account> GetAccountById = new Map<Id,Account>();
        for(Account a : [SELECT Id, Name, Customer_Success_Owner_Name__c, OwnerId FROM Account WHERE Id IN :AccountId]){
            GetAccountById.put(a.Id,a);
        }
        for(Integer i = 0; i < trigger.size; i++){

            /*US Task for Brandon */
            if(trigger.new[i].StageName == 'Closed Won' && trigger.new[i].StageName != trigger.old[i].StageName && (GetAccountById.get(trigger.new[i].AccountId).Customer_Success_Owner_Name__c == 'Brandon')){
                tasksToInsert.add(new Task(WhatId=trigger.new[i].AccountId, OwnerID='005700000023zFI', Subject='Open a Subscription',ActivityDate=Date.Today()));
                tasksToInsert.add(new Task(WhatId=trigger.new[i].AccountId, OwnerID='005700000026xbg', Subject='Open a Payment record',ActivityDate=Date.Today()));
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();              
                mail.setTargetObjectId('005700000023zFI');
                mail.setSubject('Open a Subscription for ' + GetAccountById.get(trigger.new[i].AccountId).Name);
                mail.setPlainTextBody('You can view the details here:\n' + URL.getSalesforceBaseUrl().toExternalForm() + '/' + trigger.new[i].AccountId);
                mail.setSaveAsActivity(false);
                Messaging.SingleEmailMessage mail2 = new Messaging.SingleEmailMessage();
                mail2.setTargetObjectId('005700000026xbg');
                mail2.setSaveAsActivity(false);
                mail2.setSubject('Open a Payment record for ' + GetAccountById.get(trigger.new[i].AccountId).Name);
                mail2.setPlainTextBody('You can view the details here:\n' + URL.getSalesforceBaseUrl().toExternalForm() + '/' + trigger.new[i].AccountId);
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail,mail2 });
            }

            /*US & EMEA Task for Kyle*/
            else if(trigger.new[i].StageName == 'Closed Won' && trigger.new[i].StageName != trigger.old[i].StageName && (GetAccountById.get(trigger.new[i].AccountId).Customer_Success_Owner_Name__c == 'Kyle')){
                tasksToInsert.add(new Task(WhatId=trigger.new[i].AccountId, OwnerID='00570000003DXJb', Subject='Open a Subscription',ActivityDate=Date.Today()));
                tasksToInsert.add(new Task(WhatId=trigger.new[i].AccountId, OwnerID='005700000026xbg', Subject='Open a Payment record',ActivityDate=Date.Today()));
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setTargetObjectId('00570000003DXJb');
                mail.setSaveAsActivity(false);
                mail.setSubject('Open a Subscription for ' + GetAccountById.get(trigger.new[i].AccountId).Name);
                mail.setPlainTextBody('You can view the details here:\n' + URL.getSalesforceBaseUrl().toExternalForm() + '/' + trigger.new[i].AccountId);
                Messaging.SingleEmailMessage mail2 = new Messaging.SingleEmailMessage();
                mail2.setTargetObjectId('005700000026xbg');
                mail2.setSaveAsActivity(false);
                mail2.setSubject('Open a Payment record for ' + GetAccountById.get(trigger.new[i].AccountId).Name);
                mail2.setPlainTextBody('You can view the details here:\n' + URL.getSalesforceBaseUrl().toExternalForm() + '/' + trigger.new[i].AccountId);
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail,mail2 });
            }       

            /*AP & Enterprise Task for Oz*/
            else if(trigger.new[i].StageName == 'Closed Won' && trigger.new[i].StageName != trigger.old[i].StageName && (GetAccountById.get(trigger.new[i].AccountId).Customer_Success_Owner_Name__c == 'Oz')){
                tasksToInsert.add(new Task(WhatId=trigger.new[i].AccountId, OwnerID='00570000001XWfY', Subject='Open a Subscription',ActivityDate=Date.Today()));
                tasksToInsert.add(new Task(WhatId=trigger.new[i].AccountId, OwnerID='005700000026xbg', Subject='Open a Payment record',ActivityDate=Date.Today()));
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setTargetObjectId('00570000001XWfY');
                mail.setSaveAsActivity(false);
                mail.setSubject('Open a Subscription for ' + GetAccountById.get(trigger.new[i].AccountId).Name);
                mail.setPlainTextBody('You can view the details here:\n' + URL.getSalesforceBaseUrl().toExternalForm() + '/' + trigger.new[i].AccountId);
                Messaging.SingleEmailMessage mail2 = new Messaging.SingleEmailMessage();
                mail2.setTargetObjectId('005700000026xbg');
                mail2.setSaveAsActivity(false);
                mail2.setSubject('Open a Payment record for ' + GetAccountById.get(trigger.new[i].AccountId).Name);
                mail2.setPlainTextBody('You can view the details here:\n' + URL.getSalesforceBaseUrl().toExternalForm() + '/' + trigger.new[i].AccountId);
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail,mail2 });
            }
        }
        if(!tasksToInsert.isEmpty())
            Database.insert(tasksToInsert);     
    }
}
Brandon
  • 11
  • 1
  • 1
    Welcome to SF.SE Brandon. To begin troubleshooting these kinds of problems, you'll first want to add debug statements to your code so you can see where it's not flowing as you expect it to. See How do I start to debug my own Apex code?. – crmprogdev May 06 '15 at 19:02
  • Update triggers have both trigger.new and trigger.old but i am not sure if the both these list contains elements in same order. I would rather use trigger.oldmap to compare new and old values. – the_phantom May 06 '15 at 19:19
  • 1
    @RajeevShekhar Yes, Trigger.old and Trigger.new are guaranteed to have the same records in the same order (assuming the trigger event defines both Trigger.old and Trigger.new), such that index n on either array refer to the same index n on the other array. Insert and Undelete don't have Trigger.old, and Delete doesn't have Trigger.new. – sfdcfox May 06 '15 at 19:49
  • 1
    Can you please explain why are you using setTargetObjectId method when you are manually creating the email? This method should be used when you are using a template to send email and this method only accepta LeadId, ContactId or UserId. The ID you specify sets the context and ensures that merge fields in the template contain the correct data. – the_phantom May 06 '15 at 20:02
  • I notice you have hard coded User Ids, such as 00570000001XWfY and 005700000026xbg. Generally this should be avoided. How certain are you that those are are correct ID's? – Daniel Ballinger May 06 '15 at 20:22
  • @RajeevShekhar I used the setTargetObjectId method simply because I knew which hard coded User IDs I wanted to use and it seemed to be the cleanest method from my limited experience. Essentially, because I wasn't sure what a better way to write it was. – Brandon May 06 '15 at 20:27
  • @DanielBallinger I know that I should avoid hard coded IDs, I will definitely be making changes to clean up the code. But I am certain that the IDs are correct. – Brandon May 06 '15 at 20:28

0 Answers0