+ Start a Discussion
Gdickens3Gdickens3 

Trigger Error

I have a custom object with a trigger that was created before my time - I am just learning triggers.  I have pasted my code below.  My Users are getting the error when saving "Error:Apex trigger chassis_notification caused an unexpected exception, contact your administrator: chassis_notification: execution of BeforeUpdate caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing target address (target, to, cc, bcc): []: Trigger.chassis_notification: line 149, column 1"

 

 

trigger chassis_notification on Warranty__c (before update) 
{
  for(Warranty__c w : Trigger.new) 
  {
     // If this is a Chassis, send email to notify accounting
     if(w.Chassis_Unit__c == True && 
        w.Warranty_Status__c == 'Approved' && 
        w.ChassisNotificationSent__c != True &&
        w.Replace_with_vRecovery__c == False
        )
     {
        Account company = [select Id, Name from Account where Id = :w.Account_Name__c];
        Contact contact = [select Id, Name from Contact where Id = :w.Contact_Name__c];
        User caseOwner = [select Id, Email from User where Id = :w.OwnerId];
        Asset asset = [select Id, name from Asset where Id = :w.Asset_Tag__c];
        Case myCase = [select Id, CaseNumber from Case where Id = :w.Associated_Case__c];
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses;
        mail.setReplyTo('support@unitrends.com');
        mail.setSenderDisplayName('Unitrends Support');
        if(w.Is_GE__c == True)
        {
            toAddresses = new String[] {'dcrosby@unitrends.com', 
                                        'briant@unitrends.com',
                                        'luke@unitrends.com',
                                        'twallick@unitrends.com', 
                                        'dsapp@unitrends.com',
                                        'geisenhower@unitrends.com', 
                                         caseOwner.Email, 
                                        'dmccraw@unitrends.com',
                                        'jrast@unitrends.com',
                                        'accounting@unitrends.com'
                                        };
            mail.setSubject('GE Customer: A New Warranty Request has been Approved & Includes a Chassis or Unit Replacement');
            mail.setPlainTextBody
                        ('This customer warranty to be fulfilled by RAVE.' + '\n\n\n' +
                         'The following Warranty Request has been approved and may require' + '\n' +
                         'your attention as it includes a chassis or unit replacement:'+ '\n\n' +
                         'Company:\t\t\t' + company.Name + '\n' +
                         'Contact\t\t\t' + contact.Name + '\n' +                         
                         'Asset Tag:\t\t\t' + asset.Name + '\n' +
                         'Associated Case:\t\t' + myCase.CaseNumber + '\n' +
                         'Warranty Shipment Number:\t' + w.Name + '\n\n' +
                         'Click on the link to access the Warranty:\n' + 'https://na2.salesforce.com/' + w.Id
                        );
        }
        else if(w.Platinum_Support__c == True)
        {
            if(w.Manufacturer__c == 'Rave') {
                toAddresses = new String[] {'dcrosby@unitrends.com', 
                                            'briant@unitrends.com',
                                            'luke@unitrends.com',
                                            'unitrendsccd@chipcocomputer.com', 
                                            'twallick@unitrends.com', 
                                            'dsapp@unitrends.com',
                                            'geisenhower@unitrends.com', 
                                             caseOwner.Email, 
                                            'dmccraw@unitrends.com',
                                            'jrast@unitrends.com',
                                            'accounting@unitrends.com'
                                            };
            }
            else if(w.Manufacturer__c == 'CHIPCO') {
                toAddresses = new String[] {'dcrosby@unitrends.com', 
                                            'briant@unitrends.com',
                                            'luke@unitrends.com',
                                            'unitrendsccd@chipcocomputer.com', 
                                            'twallick@unitrends.com', 
                                            'dsapp@unitrends.com',
                                            'geisenhower@unitrends.com', 
                                             caseOwner.Email, 
                                            'dmccraw@unitrends.com',
                                            'jrast@unitrends.com',
                                            'accounting@unitrends.com'
                                            };
            }
            else if(w.Manufacturer__c == 'MBX') {
                toAddresses = new String[] {'dcrosby@unitrends.com', 
                                            'briant@unitrends.com',
                                            'luke@unitrends.com',
                                            'support@mbx.com', 
                                            'twallick@unitrends.com', 
                                            'dsapp@unitrends.com',
                                            'geisenhower@unitrends.com', 
                                             caseOwner.Email, 
                                            'dmccraw@unitrends.com',
                                            'jrast@unitrends.com',
                                            'accounting@unitrends.com'
                                            };
        
            }
            mail.setSubject('Platinum Customer: A New Warranty Request has been Approved & Includes a Chassis or Unit Replacement');
            mail.setPlainTextBody
                        ('This customer has platinum support and requires next day shipping!!!' + '\n\n\n' +
                         'The following Warranty Request has been approved and may require' + '\n' +
                         'your attention as it includes a chassis or unit replacement:'+ '\n\n' +
                         'Company:\t\t\t' + company.Name + '\n' +
                         'Contact\t\t\t' + contact.Name + '\n' +                         
                         'Asset Tag:\t\t\t' + asset.Name + '\n' +
                         'Associated Case:\t\t' + myCase.CaseNumber + '\n' +
                         'Warranty Shipment Number:\t' + w.Name + '\n\n' +
                         'Click on the link to access the Warranty:\n' + 'https://na2.salesforce.com/' + w.Id
                        );
        }
        else
        {
            if(w.Manufacturer__c == 'CHIPCO') {
                toAddresses = new String[] {'dcrosby@unitrends.com', 
                                            'briant@unitrends.com',
                                            'luke@unitrends.com',
                                            'unitrendsccd@chipcocomputer.com', 
                                            'twallick@unitrends.com', 
                                            'dsapp@unitrends.com',
                                            'geisenhower@unitrends.com', 
                                             caseOwner.Email, 
                                            'dmccraw@unitrends.com',
                                            'jrast@unitrends.com',
                                            'accounting@unitrends.com'
                                            };
            }
            else if(w.Manufacturer__c == 'MBX') {
                toAddresses = new String[] {'dcrosby@unitrends.com', 
                                            'briant@unitrends.com',
                                            'luke@unitrends.com',
                                            'support@mbx.com', 
                                            'twallick@unitrends.com', 
                                            'dsapp@unitrends.com',
                                            'geisenhower@unitrends.com', 
                                             caseOwner.Email, 
                                            'dmccraw@unitrends.com',
                                            'jrast@unitrends.com',
                                            'accounting@unitrends.com'
                                            };
        
            }
            mail.setSubject('A New Warranty Request has been Approved & Includes a Chassis or Unit Replacement');
            mail.setPlainTextBody
                        ('The following Warranty Request has been approved and may require' + '\n' +
                         'your attention as it includes a chassis or unit replacement:'+ '\n\n' +
                         'Company:\t\t\t' + company.Name + '\n' +
                         'Contact\t\t\t' + contact.Name + '\n' +                         
                         'Asset Tag:\t\t\t' + asset.Name + '\n' +
                         'Associated Case:\t\t' + myCase.CaseNumber + '\n' +
                         'Warranty Shipment Number:\t' + w.Name + '\n\n' +
                         'Click on the link to access the Warranty:\n' + 'https://na2.salesforce.com/' + w.Id
                        );
        }
        mail.setToAddresses(toAddresses);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        w.ChassisNotificationSent__c = True;
     }
  }
}

 

 

 

jd123jd123

hi

 

Can you highlight 149 line no with some color then i can help out you. 

Gdickens3Gdickens3

Line 149 = (sorry couldn't figure out how to color the code)

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 

Richie DRichie D

Hi,

 

Use the debug logs to make sure you are populating the toAddress collection before trying to send an email. It looks like there are several ways to go through your code without doing so. This would throw your error.

 

Hope this helps.

Rich.

 

p.s. I hope you have anonymised your email addresses - plenty of tools out there to grab email addresses from pages like this. Just a small piece of advice.

R.

HariDineshHariDinesh

Hi,

 

System.debug('toAddresses'+toAddresses);

 

Use above Statement before using "toAddresses" in that code to make sure that one of IF condition is satasisfied and some value came into "toAddresses" string.

like below....

 

System.debug('toAddresses'+toAddresses);
mail.setToAddresses(toAddresses);

 

If you find there is error with this make sure that "toAddress" is not null by modifying your code.

 

If this is not the Issue check below links which might be useful.

 http://boards.developerforce.com/t5/Apex-Code-Development/REQUIRED-FIELD-MISSING-Missing-target-address-target-to-cc-bcc/td-p/313181

http://boards.developerforce.com/t5/Apex-Code-Development/Outbound-email-class-error-REQUIRED-FIELD-MISSING-Missing-target/td-p/307223

 

FYI

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_single.htm

 

 

 

 

TejTej

since the setToAddresses are being set inside an if loop, probably the users that are getting this error are not entering into any loop, and the setToAddresses are coming null at the end.

 

and send mail is out side if loop, and the setToAddresses null, they are getting that error.

 

as quick fix : you can avoid the error by putting If ( setToAddresses != null) { // Send email logic}

 

and then u can put debug's inside the if statements and see when errored out, it is entering any if loop logic or not.

jd123jd123

Hi

 

I think the toadress is going as null value. If below code is not giving any error then I can modify the code and I will explain the where have done the mistake.

 

Please let me know the below code is Giving any error 

 

Please try the below code

 

trigger chassis_notification on Warranty__c (before update) 
{
  for(Warranty__c w : Trigger.new) 
  {
     // If this is a Chassis, send email to notify accounting
     if(w.Chassis_Unit__c == True && 
        w.Warranty_Status__c == 'Approved' && 
        w.ChassisNotificationSent__c != True &&
        w.Replace_with_vRecovery__c == False
        )
     {
        Account company = [select Id, Name from Account where Id = :w.Account_Name__c];
        Contact contact = [select Id, Name from Contact where Id = :w.Contact_Name__c];
        User caseOwner = [select Id, Email from User where Id = :w.OwnerId];
        Asset asset = [select Id, name from Asset where Id = :w.Asset_Tag__c];
        Case myCase = [select Id, CaseNumber from Case where Id = :w.Associated_Case__c];
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses;
        mail.setReplyTo('support@unitrends.com');
        mail.setSenderDisplayName('Unitrends Support');
w.Is_GE__c=true; if(w.Is_GE__c == True) { toAddresses = new String[] {'dcrosby@unitrends.com', 'briant@unitrends.com', 'luke@unitrends.com', 'twallick@unitrends.com', 'dsapp@unitrends.com', 'geisenhower@unitrends.com', caseOwner.Email, 'dmccraw@unitrends.com', 'jrast@unitrends.com', 'accounting@unitrends.com' }; mail.setSubject('GE Customer: A New Warranty Request has been Approved & Includes a Chassis or Unit Replacement'); mail.setPlainTextBody ('This customer warranty to be fulfilled by RAVE.' + '\n\n\n' + 'The following Warranty Request has been approved and may require' + '\n' + 'your attention as it includes a chassis or unit replacement:'+ '\n\n' + 'Company:\t\t\t' + company.Name + '\n' + 'Contact\t\t\t' + contact.Name + '\n' + 'Asset Tag:\t\t\t' + asset.Name + '\n' + 'Associated Case:\t\t' + myCase.CaseNumber + '\n' + 'Warranty Shipment Number:\t' + w.Name + '\n\n' + 'Click on the link to access the Warranty:\n' + 'https://na2.salesforce.com/' + w.Id ); } else if(w.Platinum_Support__c == True) { if(w.Manufacturer__c == 'Rave') { toAddresses = new String[] {'dcrosby@unitrends.com', 'briant@unitrends.com', 'luke@unitrends.com', 'unitrendsccd@chipcocomputer.com', 'twallick@unitrends.com', 'dsapp@unitrends.com', 'geisenhower@unitrends.com', caseOwner.Email, 'dmccraw@unitrends.com', 'jrast@unitrends.com', 'accounting@unitrends.com' }; } else if(w.Manufacturer__c == 'CHIPCO') { toAddresses = new String[] {'dcrosby@unitrends.com', 'briant@unitrends.com', 'luke@unitrends.com', 'unitrendsccd@chipcocomputer.com', 'twallick@unitrends.com', 'dsapp@unitrends.com', 'geisenhower@unitrends.com', caseOwner.Email, 'dmccraw@unitrends.com', 'jrast@unitrends.com', 'accounting@unitrends.com' }; } else if(w.Manufacturer__c == 'MBX') { toAddresses = new String[] {'dcrosby@unitrends.com', 'briant@unitrends.com', 'luke@unitrends.com', 'support@mbx.com', 'twallick@unitrends.com', 'dsapp@unitrends.com', 'geisenhower@unitrends.com', caseOwner.Email, 'dmccraw@unitrends.com', 'jrast@unitrends.com', 'accounting@unitrends.com' }; } mail.setSubject('Platinum Customer: A New Warranty Request has been Approved & Includes a Chassis or Unit Replacement'); mail.setPlainTextBody ('This customer has platinum support and requires next day shipping!!!' + '\n\n\n' + 'The following Warranty Request has been approved and may require' + '\n' + 'your attention as it includes a chassis or unit replacement:'+ '\n\n' + 'Company:\t\t\t' + company.Name + '\n' + 'Contact\t\t\t' + contact.Name + '\n' + 'Asset Tag:\t\t\t' + asset.Name + '\n' + 'Associated Case:\t\t' + myCase.CaseNumber + '\n' + 'Warranty Shipment Number:\t' + w.Name + '\n\n' + 'Click on the link to access the Warranty:\n' + 'https://na2.salesforce.com/' + w.Id ); } else { if(w.Manufacturer__c == 'CHIPCO') { toAddresses = new String[] {'dcrosby@unitrends.com', 'briant@unitrends.com', 'luke@unitrends.com', 'unitrendsccd@chipcocomputer.com', 'twallick@unitrends.com', 'dsapp@unitrends.com', 'geisenhower@unitrends.com', caseOwner.Email, 'dmccraw@unitrends.com', 'jrast@unitrends.com', 'accounting@unitrends.com' }; } else if(w.Manufacturer__c == 'MBX') { toAddresses = new String[] {'dcrosby@unitrends.com', 'briant@unitrends.com', 'luke@unitrends.com', 'support@mbx.com', 'twallick@unitrends.com', 'dsapp@unitrends.com', 'geisenhower@unitrends.com', caseOwner.Email, 'dmccraw@unitrends.com', 'jrast@unitrends.com', 'accounting@unitrends.com' }; } mail.setSubject('A New Warranty Request has been Approved & Includes a Chassis or Unit Replacement'); mail.setPlainTextBody ('The following Warranty Request has been approved and may require' + '\n' + 'your attention as it includes a chassis or unit replacement:'+ '\n\n' + 'Company:\t\t\t' + company.Name + '\n' + 'Contact\t\t\t' + contact.Name + '\n' + 'Asset Tag:\t\t\t' + asset.Name + '\n' + 'Associated Case:\t\t' + myCase.CaseNumber + '\n' + 'Warranty Shipment Number:\t' + w.Name + '\n\n' + 'Click on the link to access the Warranty:\n' + 'https://na2.salesforce.com/' + w.Id ); } mail.setToAddresses(toAddresses); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); w.ChassisNotificationSent__c = True; } } }