function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Eric SantiagoEric Santiago 

Sending email to contact from Scontrol

    I'm writing a function to send an email to a contact referenced in a custom object. I was trying to use the SendEmail call defined in the API and the 9.0 Ajax kit.

Code:
  function sendEmail() {
var message = new sforce.SObject("SingleEmailMessage");
//message.set("CcAddresses", "me@ml4t.org");
message.set("BccSender",true);
//message.set("EmailPriority",EmailPriority.High);
message.set("ReplyTo", "{!$User.Email}");
message.set("SaveAsActivity", true);
message.set("Subject", "This is how you use the sendEmail call.");
//We can also just use an id for an implicit to address
message.set("TargetObjectId","0033000000Ew25A");
message.set("UseSignature",true);
message.set("PlainTextBody", "This is the body of the message.");

try
{
var sendResult = new sforce.SObject("SendEmailResult");
sendResult = sforce.connection.sendEmail(message);
}
catch(error)
{
//sforce.debug.log(error.faultcode);
//sforce.debug.log(error.faultstring);
errorMsg = sendResult.SendEmailError.Message;
txt="There was an error sending an email.\n\n";
txt+="Error description: " + errorMsg + "\n\n";
txt+="Click OK to continue.\n\n";
alert(txt);
}
}
Unfortunately, when I run this I get the error "An error has occurred: TypeError: sendResult.SendEmailError has no properties" So obviously the SendEmail call is unsuccessfull in creating a SendEmailResult object for me to get an error from. The sforce.debug log just reads Null. Not much to help me troubleshoot. Any one have some suggestions?
 

kfosskfoss

I am trying to use the MassEmailMessage and I get the following message back from the sendEmail method:

Code:

{faultcode:'soapenv:Client', faultstring:'Must specify a {http://www.w3.org/2001/XMLSchema-instance}type attribute value for the {sobject.partner.soap.sforce.com}messages element', }


 I am at a loss at to what this means.  I am sending an array of messages with the following (slightly edited):

 
Code:
function doSend()
{
 var message = new sforce.SObject("MassEmailMessage");
 message.set("targetObjectIds", "{!Contact.Email}");
 message.set("templateId", "00X3000000rbtEEAQ");

 // wrap up messages into an array
 var msgs = [];
 msgs[0] = message;

 var sendResult = new sforce.SObject("SendEMailResult");
 sendResult = sforce.connection.sendEmail(msgs, {onSuccess: emailSuccess, onFailure: emailError});
}

 
Control transfers to the emailError function with the error text I mentioned above.
 
Am I missing something here?
Ron HessRon Hess
no need to create an object to get the return

Code:
 try
   {
    var sendResult = new sforce.SObject("SendEmailResult");
    sendResult = sforce.connection.sendEmail(message);
   }

should be
Code:
 try
   {
    var sendResult = sforce.connection.sendEmail(message);
   }

 
sendEmail() will construct the return object, also this is not a valid SObject() anyway, it's got it's own type, in general you never have to construct a new result object , the toolkit does that for you


Ron HessRon Hess
for TargetObjectIds you are not passing an array of ID's, this looks like you are passing a string, and it's not an id

should look more like this ( i think, did not test)

message.set("targetObjectIds",["003000000rbtEEAQ"]);

Message Edited by Ron Hess on 04-01-2007 12:31 PM

Ron HessRon Hess
so, here is a sample from the online doc

you can see that it allows fields from both single and mass to be passed into the call to send email

Code:
// single mail request
var singleRequest = new sforce.SingleEmailMessage();
singleRequest.replyTo = "jsmith@acme.com";
singleRequest.subject = "sent through ajax test driver";

singleRequest.plainTextBody = "this test went through ajax";
singleRequest.toAddresses = ["noone@nowhere.com"];

// mass mail request - need to get email template ID

var queryResponse = sforce.connection.query("select id from emailtemplate");
var templatedId = queryResponse.getArray("records")[0].Id;
var massRequest = new sforce.MassEmailMessage();
massRequest.targetObjectIds = [globalContact.id];
massRequest.replyTo = "jsmith@acme.com";
massRequest.subject = "sent through ajax test driver";
massRequest.templateId = templateId;

var sendMailRes = sforce.connection.sendEmail([singleRequest, massRequest]);

 

kfosskfoss

Thank you for your help Ron, you solved my issues.

I don't know how I missed the code sample from the APEX docs.

Ken

Eric SantiagoEric Santiago
Ron, Thanks for all your help. One thing that I also need to correct was the capitalization of all the properties (ie targetObjectId not TargetObjectID)

Now that I've got things working I did have some questions around mass email limits. The documentation says an organization has a limit of 1,000 mass emails per day. Is there a way to determine the current count programatically? I didn't notice it in the merge fields available.