Execute JavaScript from rules: Difference between revisions

Jump to navigation Jump to search
Line 31: Line 31:
== Example ==
== Example ==


<syntaxhighlight lang="html">
<syntaxhighlight lang="html"><!DOCTYPE html>
<!DOCTYPE html>
<html>
<html>
<head>
<head>
Line 46: Line 45:
             return new Promise(function(resolve, reject) {
             return new Promise(function(resolve, reject) {
                 var account = new MobileCRM.FetchXml.Entity("account"); // NOTE: If we save to shared variable of type entity ACCOUNT, we need to create/fetch account entity.
                 var account = new MobileCRM.FetchXml.Entity("account"); // NOTE: If we save to shared variable of type entity ACCOUNT, we need to create/fetch account entity.
                account.addAttributes();
                var fetch = new MobileCRM.FetchXml.Fetch(account);
                fetch.execute("DynamicEntities", function (res) {
                    var current = res[getRandomInt(res.length)];
                    var entity = new MobileCRM.DynamicEntity(current.entityName, current.id, current.primaryName, clearInvalidProps(current.properties));
                    resolve(entity);
                }, reject, null);
            });
        }
        function getContactEntity() {
            return new Promise(function (resolve, reject) {
                var contact = new MobileCRM.FetchXml.Entity("contact"); // NOTE: If we save to shared variable of type entity ACCOUNT, we need to create/fetch account entity.
                 contact.addAttributes();
                 contact.addAttributes();


                 var fetch = new MobileCRM.FetchXml.Fetch(account);
 
                 var fetch = new MobileCRM.FetchXml.Fetch(contact);
                 fetch.execute("DynamicEntities", function (res) {
                 fetch.execute("DynamicEntities", function (res) {
                     var current = res[getRandomInt(res.length)];
                     var current = res[getRandomInt(res.length)];
                     var entity = new MobileCRM.DynamicEntity(current.entityName, current.id, current.primaryName, clearInvalidProps(current.properties));
                     var entity = new MobileCRM.DynamicEntity(current.entityName, current.id, current.primaryName, clearInvalidProps(current.properties));
                     resolve(entity);
                     resolve(entity);
Line 56: Line 79:
             });
             });
         }
         }
        function modifyCurrentEntitySimple(entityForm) {
            entityForm.entity.properties.fax = getRandomInt(10);
        }


         function getLookUp() {
         function getLookUp() {
             return new Promise(function(resolve, reject) {
             return new Promise(function(resolve, reject) {
                 var account = new MobileCRM.FetchXml.Entity("contact");
                 var contact = new MobileCRM.FetchXml.Entity("contact");
                 account.addAttributes();
                 contact.addAttributes();
 


                 var fetch = new MobileCRM.FetchXml.Fetch(account);
                 var fetch = new MobileCRM.FetchXml.Fetch(contact);
                 fetch.execute("DynamicEntities", function (res) {
                 fetch.execute("DynamicEntities", function (res) {
                     var current = res[getRandomInt(res.length)];
                     var current = res[getRandomInt(res.length)];
Line 69: Line 102:
             });
             });
         }
         }


         function getInteger() { return getRandomInt(128); }
         function getInteger() { return getRandomInt(128); }
Line 80: Line 115:
             for (var i = 0; i < getRandomInt(); i++)
             for (var i = 0; i < getRandomInt(); i++)
                 res.push(getRandomString());
                 res.push(getRandomString());


             return res;
             return res;
Line 88: Line 125:
         }
         }


        function modifyCurrentEntity(entityForm) {
            entityForm.entity.properties.fax = getRandomInt(10);
        }
 
        function clearInvalidProps(properties, invalidProps) {
            var invProps = invalidProps || [];
            var props = {};
            for (var p in properties) {
                if (p.startsWith('_') || p.startsWith("shared") || invProps.indexOf(p) > -1)
                    continue;
                var pp = properties[p];
                props[p] = pp;
            }
            return props;
        }


         // **** Helpers ****
         // **** Helpers ****


         function getRandomString(length) {
         function getRandomString(length) {
Line 116: Line 141:
     </script>
     </script>
</body>
</body>
</html>
</html></syntaxhighlight>
</syntaxhighlight>


[[Category:Woodford]]
[[Category:Woodford]]

Navigation menu