• fixi
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 4
    Replies

I am writing a class that sends an email everytime a record is submitted.  I want the email to actually be sent as a text.  I found that texts can be sent from email by sending the message to (the phone #)@maildomain.com (for example, 8012345823@vtext.colm).  

 

However, when I run the code, it shows that it was successful, but the message never arrives.  I tried the same code with a normal email address and it worked fine.  Why would it be that these email to text messagesg don't send?  Thanks.

Please help I am not a developer but am trying to learn.  After taking code samples from the board and manuals I was able to get this apex class do what I wanted but then I ran into the issue of unit test.  I had a hard enough time figuring out how to get the class to work.  I feel like I am banging my head against a wall with the unit test process it just does not make sense to me.  But I have figured out how to get 80% coverage but it is failing and I am not sure how to fix it.  Any help would be appreciated.

Thanks

Eric

 

Message: System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Missing target object ids (contact, lead or user): []

stack trace: Class.MNEmail2.send: line 56, column 1 Class.MNEmail2.testMNEmail2: line 89, column 32 External entry point

 

 

public class MNEmail2 {
public String Template = [select MN_Email_Template__c from Case where id = :ApexPages.currentPage().getParameters().get('id') limit 1].MN_Email_Template__c;
public list <Contact> Test;
Contact[] contactList = new Contact[]{};
Id[] targetObjectIds = new Id[] {};
Id[] whatids = new Id[] {}; 
public id mn = [select ID from Case where id = :ApexPages.currentPage().getParameters().get('id')].id; 
public String custaff = [select MN_Customers_Affected__c from Case where id = :ApexPages.currentPage().getParameters().get('id') limit 1].MN_Customers_Affected__c;
String[] actnames = new List<String>();

public MNEmail2() {
actnames = custaff.split(';');
//System.debug ('***** List: ' + actnames + ', size: ' + actnames.size ());
         //Loop thrugh the whole ist of contacts an their emails
          for (Contact c : [Select account.name, name, email from Contact where Contact.Account.name = :actnames and Receive_support_notifications__c = True]) {
            targetObjectIds.add(c.Id);
            contactlist.add(c);
            whatIds.add(mn);
           }
}

//public Account getAccount() {
//return account;
//}
public string getTemplate() {
    return Template;
}
public id getmn() {
    return mn;
} 
 public list<Contact> getcontacts(){ 
        return ContactList;  
}


public PageReference cancel() {
    PageReference pageRef = new PageReference('/'+ mn);
    pageRef.setRedirect(true);
    return pageRef;
}

public PageReference send() {
// Define the email
Messaging.MassEmailMessage email = new Messaging.MassEmailMessage();
// Sets the paramaters of the email
email.setTargetObjectIds (targetObjectIds);
email.setWhatIds (whatids);
email.setTemplateId ([select id from EmailTemplate where Name = :template].id);
email.setreplyto ('noreply@demandtec.com');
email.setSenderDisplayName ('DemandTec Support No-Reply');
//email.setSubject( subject );
//email.setToAddresses( toAddresses );
//email.setPlainTextBody( body );
// Sends the email
Messaging.SendEmailResult [] r =
Messaging.sendEmail(new Messaging.MassEmailMessage[] {email});
PageReference pageRef = new PageReference('/'+ mn);
    pageRef.setRedirect(true);
    return pageRef;
return null;
}
static testMethod void testMNEmail2(){
   
        //make test data
        Account a = new Account(Name = 'Testing');
        Insert a;
        Contact con = new Contact(Firstname = 'testing',lastname = 'test',email = 'eric.wc@gmail.com',Receive_support_notifications__c = True,Account = a);
        Insert con;
        Case c = new Case(Subject = 'Test case',MN_Email_Template__c = 'Scheduled Maintenance',MN_Customers_Affected__c = 'Testing');
        Insert c;
       
        CaseComment cmt1 = new CaseComment(
            ParentId = c.id,
            IsPublished = false,
            CommentBody = 'A private comment from a user.'
        );
        CaseComment cmt2 = new CaseComment(
            ParentId = c.id,
            IsPublished = true,
            CommentBody = 'A public comment from a client.'
        );
        Insert cmt1;
        Insert cmt2;
       
         // Initiate class createOpsTicketClass with reference to created Support Case
             System.currentPageReference().getParameters().put('id', c.id);

             MNemail2 ee = new MNemail2();
             String nextPage = ee.send().getUrl();
             nextPage = ee.cancel().getUrl();
             System.assert(ee.mn == c.id);
}

}