HTML Example

You may need to adjust the paths of the src attributes in the <script> and <img> tags of the following example to make them to reflect the location of the associated resource.

For example, suppose a user has saved the resource /WebResources/new_/something.htm. In this case, the relative paths would require the insertion of an extra ../ before each reference. Thus the first reference in the code example below would become:

<script src="../../ClientGlobalContext.js.aspx" type="text/javascript"></script>

We recommend that integrators favour relative paths over fixed or absolute paths.

The following example uses minimal error handling. Each integrator should ensure that the code they implement meets the error-handling standards they want to enforce.

<!DOCTYPE html>
<html>
<head>
        <title></title>
        <script src="../ClientGlobalContext.js.aspx" type="text/javascript"></script>
        <script src="../esl_/scripts/jquery.js" type="text/javascript"></script>
        <script src="../esl_/scripts/jquery_ui.js" type="text/javascript"></script>
        <script src="../esl_/scripts/json.js" type="text/javascript"></script>
        <script src="../esl_/scripts/XrmServiceToolkit.js" type="text/javascript"></script>
        <style>
            ul {
                margin: -8px 0 0 0;
            }

            li {
                float: left;
                display: inline;
                margin-left: 5px;
            }
        </style>
    </head>
    <body>
        <div>
            <ul>
                <li><button id="btnCreate"><img src="../esl_/images/PackageCreateIcon.png" height="14" alt="Create Package" /><span>Create</span></button></li>
                <li><button id="btnCreate2"><img src="../esl_/images/PackageCreateIcon.png" height="14" alt="Create Package with template" /><span>Create2</span></button></li>
            </ul>
        </div>
        <script type="text/javascript">

            $(function () {
                "use strict";

                function get (key) {
                    var attr = parent.Xrm.Page.getAttribute(key);
                    return attr.getValue();
                };

                function failedCall(error) {
                    try {
                        parent.Xrm.Utility.alertDialog(message);

                    } catch (e) {
                        alert(error.message);
                    }
                }

                function createOnCrm (item, entitySet, callback) {
                    XrmServiceToolkit.Rest.Create(item,
                        entitySet,
                        function (result1) {
                            if (callback)
                                callback(result1);
                        },
                        failedCall,
                        false);
                }

                function createSoapOnCrm (be, callback) {
                    var result1 = XrmServiceToolkit.Soap.Create(be);

                    if (callback)
                        callback(result1);
                }

                function addSignerToPackage (id, s, callback) {
                    XrmServiceToolkit.Rest.Associate(
                        id,
                        "esl_packageSet",
                        s,
                        "esl_signerSet",
                        "esl_package_signer ",
                        function (resp) {
                            callback(resp);
                        },
                        failedCall,
                        false);
                }

                function getTemplateByName (name) {
                    var result1 = {
                        Id: "00000000-0000-0000-000000000000",
                        Name: "",
                        LogicalName: "esl_template"
                    };

                    XrmServiceToolkit.Rest.RetrieveMultiple(
                        "esl_templateSet",
                        "?$top=1&$select=esl_templateId,esl_name&$filter=esl_name eq '" + name + "'",
                        function (result) {
                            result1.Id = result[0].esl_templateId;
                            result1.Name = result[0].esl_name;
                        },
                        failedCall,
                        function () {},
                        false);

                    return result1;
                }

                function getPackageSimple (name) {
                    var currentdate = new Date();

                    return {
                        esl_name: "test-" + name,
                        esl_description: "test " + name + " " + currentdate.toLocaleTimeString(),
                        esl_lookupobjecttype: parent.Xrm.Page.context.getQueryStringParameters().etc,
                        esl_loopkuprecordguid: parent.Xrm.Page.data.entity.getId(),
                        esl_lookuprecordname: name,
                        esl_lookupentitytype: parent.Xrm.Page.data.entity.getEntityName(),
                    };
                }

                function getPackageWithTemplate(name, templatename) {
                    var currentdate = new Date();

                    var templateRef = getTemplateByName(templatename);

                    return {
                        esl_name: "test-" + name,
                        esl_description: "test " + name + " " + currentdate.toLocaleTimeString(),
                        esl_lookupobjecttype: parent.Xrm.Page.context.getQueryStringParameters().etc,
                        esl_loopkuprecordguid: parent.Xrm.Page.data.entity.getId(),
                        esl_lookuprecordname: name,
                        esl_lookupentitytype: parent.Xrm.Page.data.entity.getEntityName(),
                        esl_basedontemplate: templateRef,
                    };
                }

                function getPrimaryContact(callback) {
                    var primarycontactid = get("primarycontactid");
                    if (!primarycontactid) return null;

                    var contactRef = primarycontactid[0];

                    var be = new XrmServiceToolkit.Soap.BusinessEntity("esl_signer", null);
                    be.attributes.esl_name = "Signer1";
                    be.attributes.esl_signingorder = 2;

                    be.attributes.esl_contactid = {
                        id: contactRef.id,
                        logicalName: contactRef.entityType,
                        name: contactRef.name,
                        type: "EntityReference",
                    };

                    XrmServiceToolkit.Rest.Retrieve(
                        contactRef.id,
                        "ContactSet",
                        "EMailAddress1,FirstName,LastName,JobTitle,MobilePhone",
                        null, /*expand*/
                        function (resp) {

                            if (resp) {
                                be.attributes.esl_emailaddress = resp.EMailAddress1;
                                be.attributes.esl_firstname = resp.FirstName;
                                be.attributes.esl_lastname = resp.LastName;
                                be.attributes.esl_title = resp.JobTitle;
                                be.attributes.esl_smsphone = resp.MobilePhone;
                            }

                            callback(be);
                        },
                        failedCall,
                        false);
                }

                function openPackage (packageId) {
                    parent.Xrm.Utility.openEntityForm("esl_package", packageId);
                }

                function addSigner(contact, packageId, callback) {
                    if (contact == null) openPackage(packageId);

                    createSoapOnCrm(contact,
                        function (result2) {
                            addSignerToPackage(
                                packageId,
                                result2,
                                function () {
                                    callback(packageId);
                                });
                        });
                }

                function createPackage(eslPackage) {
                    createOnCrm(eslPackage,
                        "esl_packageSet",
                        function(p) {
                            getPrimaryContact(
                                function(contact) {
                                    addSigner(
                                        contact,
                                        p.esl_packageId,
                                        function(id) {
                                            openPackage(id);
                                        });
                                });
                        });
                }


                $("#btnCreate").click(function () {
                    var p = getPackageSimple(get("name"));
                    try {
                        createPackage(p);
                    } catch (e) {
                        failedCall(e);
                    }
                });

                $("#btnCreate2").click(function () {
                    var p = getPackageWithTemplate(get("name"), "template01");
                    try {
                        createPackage(p);
                    } catch (e) {
                        failedCall(e);
                    }
                });
            });
        </script>
    </body>
</html>
Was this information helpful?
X