web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Send Email using javascript in MS Dynamic CRM

(0) ShareShare
ReportReport
Posted on by

How to send email using javascript in MS Dynamic CRM

 

Thanks in Advance

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    M I Mostafa Profile Picture
    4,257 on at

    Here is some sample JavaScript code to send out an email:

    if (typeof (SDK) == "undefined")

      { SDK = { __namespace: true }; }

          //This will establish a more unique namespace for functions in this library. This will reduce the

          // potential for functions to be overwritten due to a duplicate name when the library is loaded.

          SDK.SAMPLES = {

              _getServerUrl: function () {

                  ///<summary>

                  /// Returns the URL for the SOAP endpoint using the context information available in the form

                  /// or HTML Web resource.

                  ///</summary>

                  var ServicePath = "/XRMServices/2011/Organization.svc/web";

                  var serverUrl = "";

                  if (typeof GetGlobalContext == "function") {

                      var context = GetGlobalContext();

                      serverUrl = context.getServerUrl();

                  }

                  else {

                      if (typeof Xrm.Page.context == "object") {

                            serverUrl = Xrm.Page.context.getServerUrl();

                      }

                      else

                      { throw new Error("Unable to access the server URL"); }

                      }

                     if (serverUrl.match(/\/$/)) {

                          serverUrl = serverUrl.substring(0, serverUrl.length - 1);

                      }

                      return serverUrl + ServicePath;

                  },

              SendEmailRequest: function () {

                  var requestMain = ""

                  requestMain += "<s:Envelope xmlns:s=\"schemas.xmlsoap.org/.../envelope\">";

                  requestMain += "  <s:Body>";

                  requestMain += "    <Execute xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\">";

                  requestMain += "      <request i:type=\"b:SendEmailRequest\" xmlns:a=\"schemas.microsoft.com/.../Contracts\" xmlns:b=\"schemas.microsoft.com/.../Contracts\">";

                  requestMain += "        <a:Parameters xmlns:c=\"schemas.datacontract.org/.../System.Collections.Generic\">";

                  requestMain += "          <a:KeyValuePairOfstringanyType>";

                  requestMain += "            <c:key>EmailId</c:key>";

                  requestMain += "            <c:value i:type=\"d:guid\" xmlns:d=\"schemas.microsoft.com/.../Serialization\">dc946d36-3e41-e111-9d05-0050569838b7</c:value>";

                  requestMain += "          </a:KeyValuePairOfstringanyType>";

                  requestMain += "          <a:KeyValuePairOfstringanyType>";

                  requestMain += "            <c:key>IssueSend</c:key>";

                  requestMain += "            <c:value i:type=\"d:boolean\" xmlns:d=\"www.w3.org/.../XMLSchema\">true</c:value>";

                  requestMain += "          </a:KeyValuePairOfstringanyType>";

                  requestMain += "          <a:KeyValuePairOfstringanyType>";

                  requestMain += "            <c:key>TrackingToken</c:key>";

                  requestMain += "            <c:value i:type=\"d:string\" xmlns:d=\"www.w3.org/.../XMLSchema\" />";

                  requestMain += "          </a:KeyValuePairOfstringanyType>";

                  requestMain += "        </a:Parameters>";

                  requestMain += "        <a:RequestId i:nil=\"true\" />";

                  requestMain += "        <a:RequestName>SendEmail</a:RequestName>";

                  requestMain += "      </request>";

                  requestMain += "    </Execute>";

                  requestMain += "  </s:Body>";

                  requestMain += "</s:Envelope>";

                  var req = new XMLHttpRequest();

                  req.open("POST", SDK.SAMPLES._getServerUrl(), false)

                  req.setRequestHeader("Accept", "application/xml, text/xml, */*");

                  req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

                  req.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute&quot;);

                  req.send(requestMain);

                  //work with the response here

                  //var strResponse = req.responseXML.xml;

                  //alert(strResponse.toString());

              },

    __namespace: true

    };

    Source: mileyja.blogspot.co.uk/.../send-email-synchronously-in-microsoft.html

    Also look at this:

    mileyja.blogspot.co.uk/.../create-email-activity-in-microsoft.html

    ankit.inkeysolutions.com/.../dynamics-crm-2011-creating-e-mail.html

    Hope this helps! Please click on "yes" to verify the answer if you found this response helpful.

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi Kashyap Diwan,

    The following links provides the Javascript code to create email activity and send it synchronously in Microsoft Dynamics CRM.

    mileyja.blogspot.in/.../create-email-activity-in-microsoft.html

    mileyja.blogspot.in/.../send-email-synchronously-in-microsoft.html

  • Community Member Profile Picture
    on at

    Thanks for reply.

    Please guide

    how to call this javascript on button click to send email

    how to pass param into this js

  • Community Member Profile Picture
    on at

    Thanks for your reply

    how to call this javascript on button click.

    Thanks

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi Kashyap,

    You could use ribbon workbench for the same.

    mahenderpal.wordpress.com/.../calling-java-script-on-click-of-command-button-ms-crm-2015-step-by-step

  • Community Member Profile Picture
    on at

    Thanks for replying.

    I created button and put this javascript into the fucntion and calling it on button click.

    In the function debugger does not go into this function  SDK.SAMPLES = {

    It goes out and does not happen anythings

  • Nithya Gopinath Profile Picture
    17,078 on at

    Where have you put debugger keyword in the function? Please share the code.

  • Community Member Profile Picture
    on at

    Hi...

    This is my code

    function sendSMSPluginCall()

    {

    debugger;

    if (typeof (SDK) == "undefined")

      { SDK = { __namespace: true }; }

          //This will establish a more unique namespace for functions in this library. This will reduce the

          // potential for functions to be overwritten due to a duplicate name when the library is loaded.

          SDK.SAMPLES = {

              _getServerUrl: function () {

                  ///<summary>

                  /// Returns the URL for the SOAP endpoint using the context information available in the form

                  /// or HTML Web resource.

                  ///</summary>

                  var ServicePath = "/XRMServices/2011/Organization.svc/web";

                  var serverUrl = "";

                  if (typeof GetGlobalContext == "function") {

                      var context = GetGlobalContext();

                      serverUrl = context.getServerUrl();

                  }

                  else {

                      if (typeof Xrm.Page.context == "object") {

                            serverUrl = Xrm.Page.context.getServerUrl();

                      }

                      else

                      { throw new Error("Unable to access the server URL"); }

                      }

                     if (serverUrl.match(/\/$/)) {

                          serverUrl = serverUrl.substring(0, serverUrl.length - 1);

                      }

                      return serverUrl + ServicePath;

                  },

              SendEmailRequest: function () {

                  var requestMain = ""

                  requestMain += "<s:Envelope xmlns:s=\"schemas.xmlsoap.org/.../envelope\">";

                  requestMain += "  <s:Body>";

                  requestMain += "    <Execute xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\">";

                  requestMain += "      <request i:type=\"b:SendEmailRequest\" xmlns:a=\"schemas.microsoft.com/.../Contracts\" xmlns:b=\"schemas.microsoft.com/.../Contracts\">";

                  requestMain += "        <a:Parameters xmlns:c=\"schemas.datacontract.org/.../System.Collections.Generic\">";

                  requestMain += "          <a:KeyValuePairOfstringanyType>";

                  requestMain += "            <c:key>EmailId</c:key>";

                  requestMain += "            <c:value i:type=\"d:guid\" xmlns:d=\"schemas.microsoft.com/.../Serialization\">dc946d36-3e41-e111-9d05-0050569838b7</c:value>";

                  requestMain += "          </a:KeyValuePairOfstringanyType>";

                  requestMain += "          <a:KeyValuePairOfstringanyType>";

                  requestMain += "            <c:key>IssueSend</c:key>";

                  requestMain += "            <c:value i:type=\"d:boolean\" xmlns:d=\"www.w3.org/.../XMLSchema\">true</c:value>";

                  requestMain += "          </a:KeyValuePairOfstringanyType>";

                  requestMain += "          <a:KeyValuePairOfstringanyType>";

                  requestMain += "            <c:key>TrackingToken</c:key>";

                  requestMain += "            <c:value i:type=\"d:string\" xmlns:d=\"www.w3.org/.../XMLSchema\" />";

                  requestMain += "          </a:KeyValuePairOfstringanyType>";

                  requestMain += "        </a:Parameters>";

                  requestMain += "        <a:RequestId i:nil=\"true\" />";

                  requestMain += "        <a:RequestName>SendEmail</a:RequestName>";

                  requestMain += "      </request>";

                  requestMain += "    </Execute>";

                  requestMain += "  </s:Body>";

                  requestMain += "</s:Envelope>";

                  var req = new XMLHttpRequest();

                  req.open("POST", SDK.SAMPLES._getServerUrl(), false)

                  req.setRequestHeader("Accept", "application/xml, text/xml, */*");

                  req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

                  req.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute&quot;);

                  req.send(requestMain);

                  //work with the response here

                  //var strResponse = req.responseXML.xml;

                  //alert(strResponse.toString());

              },

    __namespace: true

    };

    }

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi,

    Replace the code below

    req.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute&quot;);
    req.send(requestMain);

              as

    req.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute&quot");
    req.send(requestMain);

    See if it works.

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Also, you could replace the code as such without writing any function and while giving the function name, give it as SDK.SAMPLES.SendEmailRequest.

    if (typeof (SDK) == "undefined")
    { SDK = { __namespace: true }; }
    //This will establish a more unique namespace for functions in this library. This will reduce the
    // potential for functions to be overwritten due to a duplicate name when the library is loaded.
    SDK.SAMPLES = {
        _getServerUrl: function () {
            ///<summary>
            /// Returns the URL for the SOAP endpoint using the context information available in the form
            /// or HTML Web resource.
            ///</summary>
            var ServicePath = "/XRMServices/2011/Organization.svc/web";
            var serverUrl = "";
            if (typeof GetGlobalContext == "function") {
                var context = GetGlobalContext();
                serverUrl = context.getServerUrl();
            }
            else {
                if (typeof Xrm.Page.context == "object") {
                    serverUrl = Xrm.Page.context.getServerUrl();
                }
                else { throw new Error("Unable to access the server URL"); }
            }
            if (serverUrl.match(/\/$/)) {
                serverUrl = serverUrl.substring(0, serverUrl.length - 1);
            }
            return serverUrl + ServicePath;
        },
        SendEmailRequest: function () {
            debugger;
            var requestMain = ""
            requestMain += "<s:Envelope xmlns:s=\"schemas.xmlsoap.org/.../envelope\">";
            requestMain += "  <s:Body>";
            requestMain += "    <Execute xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\">";
            requestMain += "      <request i:type=\"b:SendEmailRequest\" xmlns:a=\"schemas.microsoft.com/.../Contracts\" xmlns:b=\"schemas.microsoft.com/.../Contracts\">";
            requestMain += "        <a:Parameters xmlns:c=\"schemas.datacontract.org/.../System.Collections.Generic\">";
            requestMain += "          <a:KeyValuePairOfstringanyType>";
            requestMain += "            <c:key>EmailId</c:key>";
            requestMain += "            <c:value i:type=\"d:guid\" xmlns:d=\"schemas.microsoft.com/.../Serialization\">dc946d36-3e41-e111-9d05-0050569838b7</c:value>";
            requestMain += "          </a:KeyValuePairOfstringanyType>";
            requestMain += "          <a:KeyValuePairOfstringanyType>";
            requestMain += "            <c:key>IssueSend</c:key>";
            requestMain += "            <c:value i:type=\"d:boolean\" xmlns:d=\"www.w3.org/.../XMLSchema\">true</c:value>";
            requestMain += "          </a:KeyValuePairOfstringanyType>";
            requestMain += "          <a:KeyValuePairOfstringanyType>";
            requestMain += "            <c:key>TrackingToken</c:key>";
            requestMain += "            <c:value i:type=\"d:string\" xmlns:d=\"www.w3.org/.../XMLSchema\" />";
            requestMain += "          </a:KeyValuePairOfstringanyType>";
            requestMain += "        </a:Parameters>";
            requestMain += "        <a:RequestId i:nil=\"true\" />";
            requestMain += "        <a:RequestName>SendEmail</a:RequestName>";
            requestMain += "      </request>";
            requestMain += "    </Execute>";
            requestMain += "  </s:Body>";
            requestMain += "</s:Envelope>";
            var req = new XMLHttpRequest();
            req.open("POST", SDK.SAMPLES._getServerUrl(), false)
            req.setRequestHeader("Accept", "application/xml, text/xml, */*");
            req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
            req.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute&quot");
    
            req.send(requestMain);
            //work with the response here
            //var strResponse = req.responseXML.xml;
            //alert(strResponse.toString());
        },
        __namespace: true
    };

    Hope this helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans