• Imagepath
  • NEWBIE
  • 0 Points
  • Member since 2009

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies

Hello,

I'm extremely frustrated at the moment and I'm hoping someone can help me.  I have posted a few times now in the forums and while people have offered hints/suggestions, no one seems to have an answer for my question.  My question is this:

I am trying to write a trigger that will send an email when a contact is deleted from an organization.  The code works perfectly in the sandbox environment.  However, when I try to deploy to the production side, Salesforce won't allow the deployment because it says that the code has not been tested enough.  

 

Herein lies my problem:  HOW DO I TEST CODE FOR EMAILS???  I have tried everything - I have read the user guides, been through the forums, and even contacted Salesforce directly (they were no help whatsoever).  Every other piece of code tests fine, but the tester refuses to look at code dealing with email.  Here is my code:

 

 

trigger contactDelete on Contact (before delete) { try { for (Contact c : Trigger.old) { Account acct = [SELECT Name, Id FROM Account WHERE Id = :c.AccountID]; Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] tAddresses = new String[] {'tim.andrews@evariant.com'}; mail.setToAddresses(tAddresses); mail.setReplyTo('support@evariant.com'); mail.setSenderDisplayName('Salesforce Support'); mail.setSubject('Contact Deleted: ' + c.FirstName + ' ' + c.LastName); mail.setUseSignature(false); mail.setPlainTextBody( 'Contact ' + c.FirstName + ' ' + c.LastName + ' has been deleted!\n' + 'Contact ID: ' + c.Id + '\n\n' + 'Contact Organization: ' + acct.Name + '\n\n' + 'Organization ID: ' + acct.Id ); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } } catch (NullPointerException e) { System.debug('Exception Thrown!'); Exception e1 = e; }}

 

As I said, the lines dealing with email code refuse to even be tested.  Any ideas/help would be greatly appreciated!

Tim 

 

 

Hello,

I'm trying to deploy a very simple trigger that will fire off an email when a contact is deleted from an Account.  it works fine in the sandbox, but whenever I try to deploy to production I get:

 "Average test coverage across all Apex Classes and Triggers is 38%, at least 75% test coverage is required"

I have created a tester class for the trigger - here is the code for the tester class:

 

@isTest

private class TesterClass {

 

    static testMethod void myUnitTest() {

        // TO DO: implement unit test

}

    public static String getGreeting(){

return ('Test Away!')

}

static testmethod void testGreeting(){ 

String gr = TesterClass.getGreeting();

System.assertEquals('Test Away!',gr);

}

public static Double getNum(Double x, Double y){

return (x+y);

}

static testmethod void testNum(){

Double z = TesterClass.getNum(1, 2);

System.assertEquals(3,z);

}

static testMethod void myTest() {

     Account a = new Account(name='foo');

     Contact c = new Contact(firstname='John', lastname='Doe');

     insert a;

     insert c;

     System.assertEquals('foo', [select name from Account where id=:a.id].name);

     System.assertEquals(1, [select count() from Account where id=:a.id]);

     System.assertEquals('John', [select firstname from Contact where id=:c.id].firstname);

     try {

       delete a;

       delete c;

       TesterClass.testNum(); //call some method

    

     catch (DmlException e) {

       System.assert(true);  // assert that we should never get here

     }

    }

 

and here is the code for the trigger:

 

trigger contactDelete on Contact (before delete) {

    try {

        for (Contact c : Trigger.old) {

            Account acct = [SELECT Name FROM Account WHERE Id = :c.AccountID];

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

            String[] toAddresses = new String[] {'tim.andrews@evariant.com'};

            //String[] ccAddresses = new String[] {'smith@gmail.com'};

            mail.setToAddresses(toAddresses);

            mail.setReplyTo('support@evariant.com');

            mail.setSenderDisplayName('Salesforce Support');

            mail.setSubject('Contact Deleted: ' + c.FirstName + ' ' + c.LastName);

            mail.setUseSignature(false);

            mail.setPlainTextBody(

                'Contact ' + c.FirstName + ' ' + c.LastName + ' has been deleted!\n' + 

                'Contact ID: ' + c.Id + '\n\n' +

                'Contact Organization: ' + acct.Name + '\n\n' +

                'Organization ID: ' + acct.Id

                );

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

        }

    }

    catch (NullPointerException e) {

        Exception e1 = e;

    }

 

 What do I need to do to get this working????  Any help would be appreciated! Thanks!

Message Edited by Imagepath on 02-04-2009 04:55 PM
Message Edited by Imagepath on 02-04-2009 04:58 PM

Is it possible to trigger an e-mail alert when a contact is deleted?  If so, how might this be done?  I'm pretty new to SF, so please forgive me if this is a "noob" question. :smileyhappy:

Thanks!

Hello,

I'm trying to deploy a very simple trigger that will fire off an email when a contact is deleted from an Account.  it works fine in the sandbox, but whenever I try to deploy to production I get:

 "Average test coverage across all Apex Classes and Triggers is 38%, at least 75% test coverage is required"

I have created a tester class for the trigger - here is the code for the tester class:

 

@isTest

private class TesterClass {

 

    static testMethod void myUnitTest() {

        // TO DO: implement unit test

}

    public static String getGreeting(){

return ('Test Away!')

}

static testmethod void testGreeting(){ 

String gr = TesterClass.getGreeting();

System.assertEquals('Test Away!',gr);

}

public static Double getNum(Double x, Double y){

return (x+y);

}

static testmethod void testNum(){

Double z = TesterClass.getNum(1, 2);

System.assertEquals(3,z);

}

static testMethod void myTest() {

     Account a = new Account(name='foo');

     Contact c = new Contact(firstname='John', lastname='Doe');

     insert a;

     insert c;

     System.assertEquals('foo', [select name from Account where id=:a.id].name);

     System.assertEquals(1, [select count() from Account where id=:a.id]);

     System.assertEquals('John', [select firstname from Contact where id=:c.id].firstname);

     try {

       delete a;

       delete c;

       TesterClass.testNum(); //call some method

    

     catch (DmlException e) {

       System.assert(true);  // assert that we should never get here

     }

    }

 

and here is the code for the trigger:

 

trigger contactDelete on Contact (before delete) {

    try {

        for (Contact c : Trigger.old) {

            Account acct = [SELECT Name FROM Account WHERE Id = :c.AccountID];

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

            String[] toAddresses = new String[] {'tim.andrews@evariant.com'};

            //String[] ccAddresses = new String[] {'smith@gmail.com'};

            mail.setToAddresses(toAddresses);

            mail.setReplyTo('support@evariant.com');

            mail.setSenderDisplayName('Salesforce Support');

            mail.setSubject('Contact Deleted: ' + c.FirstName + ' ' + c.LastName);

            mail.setUseSignature(false);

            mail.setPlainTextBody(

                'Contact ' + c.FirstName + ' ' + c.LastName + ' has been deleted!\n' + 

                'Contact ID: ' + c.Id + '\n\n' +

                'Contact Organization: ' + acct.Name + '\n\n' +

                'Organization ID: ' + acct.Id

                );

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

        }

    }

    catch (NullPointerException e) {

        Exception e1 = e;

    }

 

 What do I need to do to get this working????  Any help would be appreciated! Thanks!

Message Edited by Imagepath on 02-04-2009 04:55 PM
Message Edited by Imagepath on 02-04-2009 04:58 PM