Dynamic CRM 2013 Online如何执行报告,生成PDF和电子邮件

时间:2014-05-01 08:53:48

标签: dynamics-crm-online

我在线使用Dynamic CRM 2013。为了引用,我有工作流和对话流程进行审核。批准后,我希望系统生成PDF报价报告,附上PDF并通过电子邮件发送给客户。

更好我想,当批准者点击批准按钮时,系统应自动生成PDF报价报告,附上PDF并通过电子邮件发送给客户,无需用户进一步输入。如果不可能,我可能需要在报价单上添加按钮。

我正在使用附加的代码,但面临各种问题。

  1. 在准备SOAP Message编码部分时,我不确定CRM 2013 Online的以下URL应该是什么?

    xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    
  2. 这里的emailid应该是什么?它是Recepient Contact id(Guid)吗?

    var emailid = resultXml.selectSingleNode(“// CreateResult”)。nodeTypedValue;

  3. alert(“emailid”+ emailid.toString());

    1. 使用此代码,无法为“ActivityMimeAttachment”创建实体,我将newEntity视为未定义。
    2. 以下是我正在使用的代码。请检查并帮助我,我出错了。让我知道是否有更好的方法来实现它。目前,我在报价表上放了一个按钮,点击事件,下面的代码将被执行。

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      

      <title></title>
      
      <script type="text/javascript">
      var Xrm;      
      
      if (window.opener) { Xrm = window.opener.Xrm; }
      else if (window.parent) { Xrm = window.parent.Xrm; }     
      
      function getReportingSession() {
      var reportName = "Quotation_Report"; //set this to the report you are trying to download
      
      var reportId = "7C39D18F-1DC6-E311-8986-D89D6765B238"; //set this to the guid of the report you are trying to download
      
      var recordid = Xrm.Page.data.entity.getId();
      
      // recordid = recordid.substring(1, 37); //getting rid of curly brackets
                  alert(recordid);
      
      var pth = Xrm.Page.context.getServerUrl() + "/CRMReports/rsviewer/reportviewer.aspx";
      var retrieveEntityReq = new XMLHttpRequest();
                  retrieveEntityReq.open("POST", pth, false);
                  retrieveEntityReq.setRequestHeader("Accept", "*/*");
                  retrieveEntityReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                  retrieveEntityReq.send("id=%7B" + reportId + "%7D&uniquename=" + Xrm.Page.context.getOrgUniqueName() + "&iscustomreport=true&reportnameonsrs=&reportName=" + reportName + "&isScheduledReport=false");
                  var x = retrieveEntityReq.responseText.indexOf("ReportSession=");
                  var ret = new Array();
                  ret[0] = retrieveEntityReq.responseText.substr(x + 14, retrieveEntityReq.responseText.indexOf("&", x) - x - 14); //the session id
                  x = retrieveEntityReq.responseText.indexOf("ControlID=");
                  ret[1] = retrieveEntityReq.responseText.substr(x + 10, retrieveEntityReq.responseText.indexOf("&", x) - x - 10); //the control id
                  return ret;
              }
      
      function createEntity(ent, entName, upd) {
      
      var jsonEntity = JSON.stringify(ent);
      var createEntityReq = new XMLHttpRequest();
      var ODataPath = Xrm.Page.context.getServerUrl() + "XRMServices/2011/OrganizationData.svc";
                  createEntityReq.open("POST", ODataPath + "/" + entName + "Set" + upd, false);
                  createEntityReq.setRequestHeader("Accept", "application/json");
                  createEntityReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                  createEntityReq.send(jsonEntity);
      var newEntity = JSON.parse(createEntityReq.responseText).d;
                  alert("new entity" + newEntity);
      return newEntity;
              }
      
      function createAttachment() {
      
      var params = getReportingSession();
      var recordid = Xrm.Page.data.entity.getId();
                 alert("recordid " + recordid);
      var orgName = Xrm.Page.context.getOrgUniqueName();
      var userID = Xrm.Page.context.getUserId();
      
      //create email record
      // Prepare the SOAP message.
      
      var xml = "<?xml version='1.0' encoding='utf-8'?>" +"<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'" +         
      " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +
      " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
      "<soap:Header>" +
      "</soap:Header>" +
      "<soap:Body>" +
      "<Create xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
      "<entity xsi:type='email'>" +
      "<regardingobjectid type='quote'>" + recordid + "</regardingobjectid>" +
      "<subject>" + "Email with Attachment4" + "</subject>" +
      "</entity>" +
      "</Create>" +
      "</soap:Body>" +
      "</soap:Envelope>";
      
      // Prepare the xmlHttpObject and send the request.
      var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
                  xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
                  xHReq.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Create");
                  xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
                  xHReq.setRequestHeader("Content-Length", xml.length);
                  xHReq.send(xml);
      
      // Capture the result
      
      var resultXml = xHReq.responseXML;
                 // alert("resultXml " + resultXml);
      
      // Check for errors.
      var errorCount = resultXml.selectNodes('//error').length;
      if (errorCount != 0) {
                      alert("ERROR");
      
      var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
                      alert(msg);
                  }
      
      var emailid = resultXml.selectSingleNode("//CreateResult").nodeTypedValue;
                  alert("emailid" + emailid.toString());
      
      
      //var emailid = userID;
      var post = Object();
                  post.Body = encodePdf(params);
      
      var email = new Array();
                  email[0] =new Object();
                  email[0].id = emailid;
                  email[0].entityType ='email';
                  post.Subject ="File Attachment";
                  post.AttachmentNumber = 1;
                  post.FileName ="Report.pdf";
                  post.MimeType ="application/pdf";
                  post.ObjectId = Object();
                  post.ObjectId.LogicalName ="email";
                  post.ObjectId.Id = email[0].id;
                  post.ObjectTypeCode ="email";
                  alert(post.ObjectId.Id);
                  createEntity(post,"ActivityMimeAttachment", "");
                  alert("created successfully");
      
      email.Subject = "Your Order";
      
      
      
          //Set The current order as the Regarding object
      
          email.RegardingObjectId = {
      
              Id: Xrm.Page.data.entity.getId(),    //Get the current entity Id , here OrderId
      
              LogicalName: Xrm.Page.data.entity.getEntityName()//Get the current entity name, here it will be “salesOrder”
      
          };
      
      
      
         //Create Email Activity 
      
          SDK.JScriptRESTDataOperations.Create(email, "Email", EmailCallBack, function (error) { alert(error.message); });
      
      
              }
      // Email Call Back function
      
      function EmailCallBack(result) {
      
      
      
          email = result; // Set the email to result to use it later in email attachment for retrieving activity Id
      
      
      
          var activityPartyFrom = new Object();
      
          // Set the From party of the ActivityParty to relate an entity with Email From field
      
          activityPartyFrom.PartyId = {
      
              Id: customerId,  // id of entity you want to associate this activity with.          
      
              LogicalName: "contact"
      
          };
      
      
      
          // Set the "activity" of the ActivityParty
      
          activityPartyFrom.ActivityId = {
      
              Id: result.ActivityId,
      
              LogicalName: "email"
      
          };
      
          // Now set the participation type that describes the role of the party on the activity).
      
          activityPartyFrom.ParticipationTypeMask = { Value: 2 }; // 2 means ToRecipients
      
      // Create the from ActivityParty for the email 
      
          SDK.JScriptRESTDataOperations.Create(activityPartyFrom, "ActivityParty", ActivityPartyFromCallBack, function (error) { alert(error.message); });
      
      
      
      
      
          var activityPartyTo = new Object();
      
      // Set the From party of the ActivityParty to relate an entity with Email From field
      
          activityPartyTo.PartyId = {
      
              Id: ownerId,  // id of entity you want to associate this activity with.          
      
              LogicalName: "systemuser"
      
          };
      
      
      
          // Set the "activity" of the ActivityParty  
      
          activityPartyTo.ActivityId = {
      
              Id: result.ActivityId,
      
              LogicalName: "email"
      
          };
      
      
      
      // Now set the participation type that describes the role of the party on the activity).    activityPartyTo.ParticipationTypeMask = { Value: 1 }; // 1 means Sender
      
      // Create the from ActivityParty
      
          SDK.JScriptRESTDataOperations.Create(activityPartyTo, "ActivityParty", ActivityPartyToCallBack, function (error) { alert(error.message); });
      
      
      
      }
      
      
      //ActivityParty From Callback
      
      function ActivityPartyFromCallBack(result) {
      
      }
      
      
      
      //ActivityParty To Callback
      
      function ActivityPartyToCallBack(result) {
      
      }
      var StringMaker = function () {
      
      this.parts = [];
      this.length = 0;
      this.append = function (s) {
      this.parts.push(s);
      this.length += s.length;
                  }
      this.prepend = function (s) {
      this.parts.unshift(s);
      this.length += s.length;
                  }
      this.toString = function () {
      return this.parts.join('');
                  }
              }
      var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
      
      function encode64(input) {
      var output = new StringMaker();
      var chr1, chr2, chr3;
      var enc1, enc2, enc3, enc4;
      var i = 0;
      while (i < input.length) {
                      chr1 = input[i++];
                      chr2 = input[i++];
                      chr3 = input[i++];
                      enc1 = chr1 >> 2;
                      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
                      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
                      enc4 = chr3 & 63;
      
      if (isNaN(chr2)) {
                          enc3 = enc4 = 64;
                      }
      
      
      else if (isNaN(chr3)) {
                          enc4 = 64;
                      }
                      output.append(keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4));
                  }
      return output.toString();
              }
      var bdy = new Array();
      var bdyLen = 0;
      function concat2Bdy(x) {
                  bdy[bdyLen] = x;
                  bdyLen++;
              }
      
      function encodePdf(params) {
                  bdy = new Array();
                  bdyLen = 0;
      
      var retrieveEntityReq = new XMLHttpRequest();
      var pth = Xrm.Page.context.getServerUrl() + "/Reserved.ReportViewerWebControl.axd?ReportSession=" + params[0] + "&Culture=1033&CultureOverrides=True&UICulture=1033&UICultureOverrides=True&ReportStack=1&ControlID=" + params[1] + "&OpType=Export&FileName=Public&ContentDisposition=OnlyHtmlInline&Format=PDF";
                  retrieveEntityReq.open("GET", pth, false);
                  retrieveEntityReq.setRequestHeader("Accept", "*/*");
                  retrieveEntityReq.send();
                  BinaryToArray(retrieveEntityReq.responseBody);
      return encode64(bdy);
              }
      </SCRIPT>   
      
      
      
          <SCRIPT type=text/vbscript>
      
          Function BinaryToArray(Binary)
      
                 Dim i
      
                 ReDim byteArray(LenB(Binary))
      
                 For i = 1 To LenB(Binary)
      
                       byteArray(i-1) = AscB(MidB(Binary, i, 1))
      
                       concat2Bdy(AscB(MidB(Binary, i, 1)))
      
               Next
      
                BinaryToArray = byteArray
      
         End Function
      
      
      </SCRIPT>
      
      </head>
      
      <body>
      
      <input type="button" onclick="createAttachment();" value="Attach Report" />
      
      </body>
      

      感谢。并等待你的宝贵意见。

1 个答案:

答案 0 :(得分:2)

我通过以下代码解决了这个问题...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
    <script type="text/javascript">
        if (typeof (SDK) == "undefined")
        { SDK = { __namespace: true }; }
        SDK.JScriptRESTDataOperations = {
            _context: function () {

                if (typeof GetGlobalContext != "undefined")
                { return GetGlobalContext(); }
                else {
                    if (typeof Xrm != "undefined") {
                        return Xrm.Page.context;
                    }
                    else { return new Error("Context is not available."); }
                }
            },
            _getServerUrl: function () {
                var serverUrl = this._context().getServerUrl()
                if (serverUrl.match(/\/$/)) {
                    serverUrl = serverUrl.substring(0, serverUrl.length - 1);
                }
                return serverUrl;
            },
            _ODataPath: function () {
                return this._getServerUrl() + "/XRMServices/2011/OrganizationData.svc/";
            },
            _errorHandler: function (req) {
                return new Error("Error : " +   req.status + ": " +  req.statusText + ": " +  JSON.parse(req.responseText).error.message.value);
            },
            _dateReviver: function (key, value) {
                var a;
                if (typeof value === 'string') {
                    a = /Date\(([-+]?\d+)\)/.exec(value);
                    if (a) {
                        return new Date(parseInt(value.replace("/Date(", "").replace(")/", ""), 10));
                    }
                }
                return value;
            },
            Create: function (object, type, successCallback, errorCallback) {
                var req = new XMLHttpRequest();
                req.open("POST", this._ODataPath() + type + "Set", true);
                req.setRequestHeader("Accept", "application/json");
                req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                req.onreadystatechange = function () {
                    if (this.readyState == 4 /* complete */) {
                        if (this.status == 201) {
                            successCallback(JSON.parse(this.responseText, SDK.JScriptRESTDataOperations._dateReviver).d);
                        }
                        else {
                            errorCallback(SDK.JScriptRESTDataOperations._errorHandler(this));
                        }
                    }
                };
                req.send(JSON.stringify(object));
            },
            Retrieve: function (id, type, successCallback, errorCallback) {
                var req = new XMLHttpRequest();
                req.open("GET", this._ODataPath() + type + "Set(guid'" + id + "')", true);
                req.setRequestHeader("Accept", "application/json");
                req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                req.onreadystatechange = function () {
                    if (this.readyState == 4 /* complete */) {
                        if (this.status == 200) {
                            successCallback(JSON.parse(this.responseText, SDK.JScriptRESTDataOperations._dateReviver).d);
                        }
                        else {
                            errorCallback(SDK.JScriptRESTDataOperations._errorHandler(this));
                        }
                    }
                };
                req.send();
            },
            Update: function (id, object, type, successCallback, errorCallback) {
                var req = new XMLHttpRequest();

                req.open("POST", this._ODataPath() + type + "Set(guid'" + id + "')", true);
                req.setRequestHeader("Accept", "application/json");
                req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                req.setRequestHeader("X-HTTP-Method", "MERGE");
                req.onreadystatechange = function () {
                    if (this.readyState == 4 /* complete */) {
                        if (this.status == 204 || this.status == 1223) {
                            successCallback();
                        }
                        else {
                            errorCallback(SDK.JScriptRESTDataOperations._errorHandler(this));
                        }
                    }
                };
                req.send(JSON.stringify(object));
            },
            Delete: function (id, type, successCallback, errorCallback) {
                var req = new XMLHttpRequest();
                req.open("POST", this._ODataPath() + type + "Set(guid'" + id + "')", true);
                req.setRequestHeader("Accept", "application/json");
                req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                req.setRequestHeader("X-HTTP-Method", "DELETE");
                req.onreadystatechange = function () {
                    if (this.readyState == 4 /* complete */) {
                        if (this.status == 204 || this.status == 1223) {
                            successCallback();
                        }
                        else {
                            errorCallback(SDK.JScriptRESTDataOperations._errorHandler(this));
                        }
                    }
                };
                req.send();

            },
            RetrieveMultiple: function (type, filter, successCallback, errorCallback) {

                if (filter != null) {
                    filter = "?" + filter;
                }
                else { filter = ""; }

                var req = new XMLHttpRequest();
                req.open("GET", this._ODataPath() + type + "Set" + filter, true);
                req.setRequestHeader("Accept", "application/json");
                req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                req.onreadystatechange = function () {
                    if (this.readyState == 4 /* complete */) {
                        if (this.status == 200) {
                            successCallback(JSON.parse(this.responseText, SDK.JScriptRESTDataOperations._dateReviver).d.results);
                        }
                        else {
                            errorCallback(SDK.JScriptRESTDataOperations._errorHandler(this));
                        }
                    }

                };
                req.send();


            },
            __namespace: true
        };
    </script>
    <script type="text/javascript">
        //Create Email and link it with Order as Regarding field
        var Xrm;
        var email = new Object();
        var ownerID = "";
        var CustomerId = "";
        if (window.opener) { Xrm = window.opener.Xrm; }
        else if (window.parent) { Xrm = window.parent.Xrm; }

        //Get ownerid who send email of quotation to customer
        function GetOwnerID() {
            var owner = Xrm.Page.getAttribute("ownerid").getValue();
            ownerID = owner[0].id;
            var ownerName = owner[0].name;
            var entityType = owner[0].entityType;

            GetToEmailGUID();
        }


        //Get customerid who receive email of quotation from owner
        function GetToEmailGUID() {
            var Customer = Xrm.Page.getAttribute('customerid').getValue();
            CustomerId = Customer[0].id;
            var CustomerName = Customer[0].name;
            var entityType = Customer[0].entityType;

            //if CustomerId is type of "Account" then get Primary Contact id of that account 
            if (entityType == "account") {
                var contact = Xrm.Page.getAttribute("customerid").getValue();

                if (contact === null) return;

                var serverUrl = Xrm.Page.context.getClientUrl();
                var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/AccountSet(guid'" + contact[0].id + "')?$select=PrimaryContactId";

                var req = new XMLHttpRequest();
                req.open("GET", oDataSelect, false);
                req.setRequestHeader("Accept", "application/json");
                req.setRequestHeader("Content-Type", "application/json;charset=utf-8");
                req.onreadystatechange = function () {
                    if (req.readyState === 4) {
                        if (req.status === 200) {
                            var retrieved = JSON.parse(req.responseText).d;
                            CustomerId = retrieved.PrimaryContactId.Id;
                        }
                        else {
                            alert(this.statusText);
                        }
                    }
                };
                req.send();

            }
        }
        function CreateEmail() {
            GetOwnerID();
            email.Subject = "Email with Report Attachment";

            //Set The current order as the Regarding object
            email.RegardingObjectId = {

                Id: Xrm.Page.data.entity.getId(),    //Get the current entity Id , here OrderId
                LogicalName: Xrm.Page.data.entity.getEntityName()//Get the current entity name, here it will be “salesOrder”

            };
            //Create Email Activity 
            SDK.JScriptRESTDataOperations.Create(email, "Email", EmailCallBack, function (error) { alert(error.message); });
        }

        // Email Call Back function

        function EmailCallBack(result) {
            email = result; // Set the email to result to use it later in email attachment for retrieving activity Id
            var activityPartyFrom = new Object();

            // Set the From party of the ActivityParty to relate an entity with Email From field
            activityPartyFrom.PartyId = {
                Id: CustomerId, //"79EBDD26-FDBE-E311-8986-D89D6765B238",  // id of entity you want to associate this activity with.          
                LogicalName: "contact"
            };
            // Set the "activity" of the ActivityParty

            activityPartyFrom.ActivityId = {
                Id: result.ActivityId,
                LogicalName: "email"
            };

            // Now set the participation type that describes the role of the party on the activity).

            activityPartyFrom.ParticipationTypeMask = { Value: 2 }; // 2 means ToRecipients

            // Create the from ActivityParty for the email 

            SDK.JScriptRESTDataOperations.Create(activityPartyFrom, "ActivityParty", ActivityPartyFromCallBack, function (error) { alert(error.message); });

            var activityPartyTo = new Object();

            // Set the From party of the ActivityParty to relate an entity with Email From field
            activityPartyTo.PartyId = {
                Id: ownerID, //"79EBDD26-FDBE-E311-8986-D89D6765B238",  // id of entity you want to associate this activity with.          
                LogicalName: "systemuser"
            };

            // Set the "activity" of the ActivityParty  
            activityPartyTo.ActivityId = {
                Id: result.ActivityId,
                LogicalName: "email"
            };

            // Now set the participation type that describes the role of the party on the activity).    
            activityPartyTo.ParticipationTypeMask = { Value: 1 }; // 1 means Sender

            // Create the from ActivityParty

            SDK.JScriptRESTDataOperations.Create(activityPartyTo, "ActivityParty", ActivityPartyToCallBack, function (error) { alert(error.message); });
        }

        //ActivityParty From Callback

        function ActivityPartyFromCallBack(result) {
        }

        //ActivityParty To Callback

        function ActivityPartyToCallBack(result) {
            GetReportId('Quotation_Bitscape');
        }
        //Create attachment for the created email

        function CreateEmailAttachment() {
            //get reporting session and use the params to convert a report in PDF

            var params = getReportingSession();

            //Email attachment parameters

            var activitymimeattachment = Object();
            activitymimeattachment.ObjectId = Object();
            activitymimeattachment.ObjectId.LogicalName = "email";
            activitymimeattachment.ObjectId.Id = email.ActivityId;
            activitymimeattachment.ObjectTypeCode = "email",
                activitymimeattachment.Subject = "File Attachment";
            activitymimeattachment.Body = encodePdf(params);
            activitymimeattachment.FileName = "Report.pdf";
            activitymimeattachment.MimeType = "application/pdf";

            //Attachment call
            SDK.JScriptRESTDataOperations.Create(activitymimeattachment, "ActivityMimeAttachment", ActivityMimeAttachmentCallBack, function (error) { alert(error.message); });
        }

        //ActivityMimeAttachment CallBack function

        function ActivityMimeAttachmentCallBack(result) {
            var features = "location=no,menubar=no,status=no,toolbar=no,resizable=yes";
            var width = "800px";
            var height = "600px";

            window.open(Xrm.Page.context.getServerUrl() + "main.aspx?etc=" + 4202 + "&pagetype=entityrecord&id=" + email.ActivityId, "_blank", features);

            // To open window which works in outlook and IE both
            //openStdWin(Xrm.Page.context.getServerUrl() + "main.aspx?etc=" + 4202 + "&pagetype=entityrecord&id=" + email.ActivityId, "_blank", width, height, features);
        }

        //This method will get the reportId based on a report name that will be used in            getReportingSession() function

        function GetReportId(reportName) {
            var oDataSetName = "ReportSet";
            var columns = "ReportId";
            var filter = "Name eq '" + reportName + "'";
            retrieveMultiple(oDataSetName, columns, filter, onSuccess);
        }

        function retrieveMultiple(odataSetName, select, filter, successCallback) {

            var serverUrl = Xrm.Page.context.getServerUrl();
            var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
            var odataUri = serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "?";

            if (select) {
                odataUri += "$select=" + select + "&";
            }

            if (filter) {
                odataUri += "$filter=" + filter;
            }

            $.ajax({
                type: "GET",
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                url: odataUri,
                beforeSend: function (XMLHttpRequest) {
                    XMLHttpRequest.setRequestHeader("Accept", "application/json");

                },

                success: function (data) {
                    if (successCallback) {
                        if (data && data.d && data.d.results) {
                            successCallback(data.d.results);
                        }
                        else if (data && data.d) {
                            successCallback(data.d);
                        }
                        else {
                            successCallback(data);
                        }
                    }
                },
                error: function (XmlHttpRequest, errorThrown) {
                    if (XmlHttpRequest && XmlHttpRequest.responseText) {
                        alert("Error while retrieval ; Error – " + XmlHttpRequest.responseText);
                    }
                }

            });

        }

        function onSuccess(data) {
            reportId = data[0].ReportId.replace('{', ").replace('}', ");
            CreateEmailAttachment(); // Create Email Attachment
        }

        //Gets the report contents

        function getReportingSession() {
            var pth = Xrm.Page.context.getServerUrl() + "/CRMReports/rsviewer/reportviewer.aspx";
            var retrieveEntityReq = new XMLHttpRequest();
            var Id = Xrm.Page.data.entity.getId();
            var quotationGUID = Id.replace('{', ""); //set this to selected quotation GUID
            quotationGUID = quotationGUID.replace('}', "");

            var reportName = "Quotation_Report"; //set this to the report you are trying to download
            var reportID = "7C39D18F-1DC6-E311-8986-D89D6765B238"; //set this to the guid of the report you are trying to download
            var rptPathString = ""; //set this to the CRMF_Filtered parameter
            var strParameterXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'><entity name='quote'><all-attributes /><filter type='and'><condition attribute='quoteid' operator='eq' uitype='quote' value='" + quotationGUID + "' /> </filter></entity></fetch>";
            retrieveEntityReq.open("POST", pth, false);
            retrieveEntityReq.setRequestHeader("Accept", "*/*");
            retrieveEntityReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

            rptPathString = "id=%7B" + reportID + "%7D&uniquename=" + Xrm.Page.context.getOrgUniqueName() + "&iscustomreport=true&reportnameonsrs=&reportName=" +
                            reportName + "&isScheduledReport=false&p:CRMAF_Filteredquote=" + strParameterXML;
            //remove the part starting from &p:salesorderid if your report has no parameters

            retrieveEntityReq.send(rptPathString);

            var x = retrieveEntityReq.responseText.indexOf("ReportSession=");
            var ret = new Array();
            ret[0] = retrieveEntityReq.responseText.substr(x + 14, retrieveEntityReq.responseText.indexOf("&", x) - x - 14); //the session id
            x = retrieveEntityReq.responseText.indexOf("ControlID=");
            ret[1] = retrieveEntityReq.responseText.substr(x + 10, retrieveEntityReq.responseText.indexOf("&", x) - x - 10); //the control id

            return ret;
        }
        var bdy = new Array();
        var bdyLen = 0;
        function concat2Bdy(x) {
            bdy[bdyLen] = x;
            bdyLen++;
        }
        function encodePdf(params) {

            bdy = new Array();
            bdyLen = 0;

            var retrieveEntityReq = new XMLHttpRequest();
            var pth = Xrm.Page.context.getServerUrl() + "/Reserved.ReportViewerWebControl.axd?ReportSession=" + params[0] +
            "&Culture=1033&CultureOverrides=True&UICulture=1033&UICultureOverrides=True&ReportStack=1&ControlID=" + params[1] +
            "&OpType=Export&FileName=Public&ContentDisposition=OnlyHtmlInline&Format=PDF";
            retrieveEntityReq.open("GET", pth, false);
            retrieveEntityReq.setRequestHeader("Accept", "*/*");
            retrieveEntityReq.send();
            BinaryToArray(retrieveEntityReq.responseBody);

            return encode64(bdy);

        }

        var StringMaker = function () {

            this.parts = [];
            this.length = 0;
            this.append = function (s) {
                this.parts.push(s);
                this.length += s.length;
            }

            this.prepend = function (s) {
                this.parts.unshift(s);
                this.length += s.length;
            }

            this.toString = function () {
                return this.parts.join('');
            }
        }

        var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

        function encode64(input) {
            var output = new StringMaker();
            var chr1, chr2, chr3;
            var enc1, enc2, enc3, enc4;
            var i = 0;

            while (i < input.length) {
                chr1 = input[i++];
                chr2 = input[i++];
                chr3 = input[i++];

                enc1 = chr1 >> 2;
                enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
                enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
                enc4 = chr3 & 63;

                if (isNaN(chr2)) {
                    enc3 = enc4 = 64;

                } else if (isNaN(chr3)) {
                    enc4 = 64;
                }

                output.append(keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4));
            }

            return output.toString();

        }      
    </script>
    <script type="text/vbscript">

    Function BinaryToArray(Binary)

           Dim i

           ReDim byteArray(LenB(Binary))

           For i = 1 To LenB(Binary)

                 byteArray(i-1) = AscB(MidB(Binary, i, 1))

                 concat2Bdy(AscB(MidB(Binary, i, 1)))
         Next
          BinaryToArray = byteArray
   End Function
     </script>
</head>
<body>
    <input type="button" onclick="CreateEmail();" value="Attach Report" />
</body>
</html>