Execute JavaScript from rules: Difference between revisions

Jump to navigation Jump to search
No edit summary
Line 31: Line 31:
     <script>
     <script>
         function getAccountEntity() {
         function getAccountEntity() {
                                                return new Promise(function(resolve, reject) {
            return new Promise(function(resolve, reject) {
                                                                var contact = new MobileCRM.FetchXml.Entity("account"); // NOTE: If we save to shared variable of type entity ACCOUNT, we need to create/fetch account entity.
                var contact = new MobileCRM.FetchXml.Entity("account"); // 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(contact);
                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);
                                                                }, reject, null);
                }, reject, null);
                                                });
            });
         }
         }


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


                                                                var fetch = new MobileCRM.FetchXml.Fetch(account);
                var fetch = new MobileCRM.FetchXml.Fetch(account);
                                                                fetch.execute("DynamicEntities", function (res) {
                fetch.execute("DynamicEntities", function (res) {
                                                                                var current = res[getRandomInt(res.length)];
                    var current = res[getRandomInt(res.length)];
                                                                                resolve(new MobileCRM.Reference(current.entityName, current.id, current.primaryName));
                    resolve(new MobileCRM.Reference(current.entityName, current.id, current.primaryName));
                                                                }, reject, null);
                }, reject, null);
                                                });
            });
         }
         }


Line 60: Line 60:
         function getString() { return getRandomString(); }
         function getString() { return getRandomString(); }
         function getDecimal() {
         function getDecimal() {
                                                var precision = 100; // 2 decimals
            var precision = 100; // 2 decimals
                                                return Math.floor(Math.random() * (10 * precision - 1 * precision) + 1 * precision) / (1 * precision);
            return Math.floor(Math.random() * (10 * precision - 1 * precision) + 1 * precision) / (1 * precision);
         }
         }
         function getStringList() {
         function getStringList() {
                                                var res = [];
            var res = [];
                                                for (var i = 0; i < getRandomInt(); i++)
            for (var i = 0; i < getRandomInt(); i++)
                                                                res.push(getRandomString());
                res.push(getRandomString());


                                                return res;
            return res;
         }
         }
         function getDateTime() {
         function getDateTime() {
                                                var dt = new Date();
            var dt = new Date();
                                                return dt.toLocaleTimeString();
            return dt.toLocaleTimeString();
         }
         }


Line 78: Line 78:


         function getRandomString(length) {
         function getRandomString(length) {
                                                var len = length || 7;
            var len = length || 7;
                                                return Math.random().toString(36).substring(len);
            return Math.random().toString(36).substring(len);
         }
         }
         function getRandomInt(maximum) {
         function getRandomInt(maximum) {
                                                var max = maximum || 4;
            var max = maximum || 4;
                                                return Math.floor(Math.random() * Math.floor(max));
            return Math.floor(Math.random() * Math.floor(max));
         }
         }
     </script>
     </script>

Navigation menu