Is there a 'send email' method?

Feb 26, 2015 at 9:53 PM
Hi jaimieji,

I followed your example at https://xrmservicetoolkit.codeplex.com/documentation on how to create an email and it's able to create a draft. In one situation this is exactly what I want and I then open the draft by adding
 Xrm.Utility.openEntityForm("email", emailId);
to the end of my code. However, in another situation I want the jscript to send the email without opening it for editing. How would I do that? I've tried setting 'statuscode' and 'statecode' to 3 and 6 for 'Sent' and 'Pending Send' respectively, but the status stays as 'Draft'. I also briefly tried setting 'deliveryattempts' to 0 per something I read in another blog post but it just prevented the code from creating the email.
Feb 26, 2015 at 11:51 PM
Edited Feb 26, 2015 at 11:55 PM
I may have found my answer. From some extra reading online, I found out that I cannot simply set the statuscode and statecode like I do other fields. It requires some SOAP function calls to accomplish that. After a bit more digging I found one reference to XrmServiceToolkit.Soap.SetState() function at https://community.dynamics.com/crm/f/117/t/122205.aspx and see SetState is in the list of SOAP functions on the homepage, https://xrmservicetoolkit.codeplex.com/. It would be nice if there were more examples to look at online, but I will dig into this further when I get back to work tomorrow.
Feb 27, 2015 at 7:06 PM
I've been successful in changing the status of the email to 'Pending Send' via SetState(), however the email stays at 'Pending Send' and never goes out. I've tried setting the deliveryattempts field as shown below, but then the code doesn't create the email. I've tried searching for references of other people setting deliveryattempts using javascript or jscript but all I can find are examples of workflows setting the 'No. of Delivery Attempts' field.
createEmail.attributes["directioncode"] = true;
createEmail.attributes["deliveryattempts"] = 0;
emailId = XrmServiceToolkit.Soap.Create(createEmail);
XrmServiceToolkit.Soap.SetState("email", emailId, 1, 6);
Unless there is some way to set the field in Javascript, all I can think to do is use fireOnChange on a custom field to trigger a workflow that sets 'No. of Delivery Attempts' to 0.
Feb 27, 2015 at 7:16 PM
You will need to make a SOAP call to execute SendEmailRequest. See the code from this blog post:

http://mileyja.blogspot.com/2012/02/send-email-synchronously-in-microsoft.html
Feb 27, 2015 at 9:25 PM
Unfortunately I know very little about SOAP which is why I'm using XrmServiceToolkit. I had seen that blog post before, but I'll take a look at again and see if I can figure out how to use it. As a first guess I'd try to copy all of 'var requestMain' into my own code and then use XrmServiceToolkit.Soap.Execute() to execute it. I'm hopeful then that all I need to change/pass into it is the 'EmailId' which is near to the middle of the blog example.
Mar 2, 2015 at 3:13 PM
Pardon me if I don't understand something, but in the blog example I don't see an email ID variable being passed to the function and I also don't see one being concatenated into requestMain. What I thought was the email ID is just text inside of a string. So I'm assuming this will just Send the email on the form the user already has open. If I wanted to do that I would have used the Send() function shown in http://bogdano-crm.blogspot.com/2013/03/crm-2011-send-email-from-javascript.html . The problem is I am creating the email via javascript first so the form is not open.
Mar 4, 2015 at 8:08 PM
Edited Mar 4, 2015 at 8:13 PM
Never mind my last question. I think I understand now. The example is sending the email with a GUID of 'dc946d36-3e41-e111-9d05-0050569838b7'. I just need to replace this part with the GUID of the email I'm creating. I just didn't understand why someone would hard code the GUID of an email into their code, so I thought it was the GUID ID of something else. I started looking at the C#, plug-in example which explained that I was wrong. How many times does someone need to send the same email? I'm all set now. Thanks dipple.
Mar 5, 2015 at 4:29 PM
Edited Mar 6, 2015 at 1:26 PM
Here's my attempt to make a function out of the blog example using XrmServiceToolkit.Soap.execute(), however it isn't working, and I am at a loss to know how to debug SOAP.
function SendEmail(emailId)
{
    var request = [
        "<request i:type='b:SendEmailRequest'",
        "xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts'",
        "xmlns:b='http://schemas.microsoft.com/crm/2011/Contracts'>",
            "<a:Parameters xmlns:c='http://schemas.datacontract.org/2004/07/System.Collections.Generic'>",
                "<a:KeyValuePairOfstringanyType>",
                    "<c:key>EmailId</c:key>",
                    "<c:value i:type='d:guid' xmlns:d='http://schemas.microsoft.com/2003/10/Serialization/'>",
                        emailId,
                    "</c:value>",
                "</a:KeyValuePairOfstringanyType>",
                "<a:KeyValuePairOfstringanyType>",
                    "<c:key>IssueSend</c:key>",
                    "<c:value i:type='d:boolean' xmlns:d='http://www.w3.org/2001/XMLSchema'>true</c:value>",
                "</a:KeyValuePairOfstringanyType>",
                "<a:KeyValuePairOfstringanyType>",
                    "<c:key>TrackingToken</c:key>",
                    "<c:value i:type='d:string' xmlns:d='http://www.w3.org/2001/XMLSchema' />",
                "</a:KeyValuePairOfstringanyType>",
            "</a:Parameters>",
            "<a:RequestId i:nil='true' />",
            "<a:RequestName>SendEmail</a:RequestName>",
        "</request>"
    ].join(" ");

    XrmServiceToolkit.Soap.Execute(request)
}
** Corrected typos mentioned below. KSG_CRM, 3/6/2015. **
Mar 5, 2015 at 8:21 PM
Use Fiddler to see the requests that are sent when you use crm to send a direct email using web interface. Compare the soap request with the request you are making. I will look for the code I have used in the past. I have done exactly what you are trying to do in the past and I used XrmServiceToolkit to make the soap call.
Mar 6, 2015 at 1:25 PM
I found the problem(s) after a lot of troubleshooting. First, in the join statement I need a space instead of an empty string. Second, the last line needs to be XrmServiceToolkit.Soap.Execute(request) with a capital 'E' even though the function defined in the toolkit with a lower case 'e'.