Execute JavaScript from rules: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Line 5: Line 5:


The option to execute scripts directly from rules combines these two options. It aims to simplify writing JavaScript for Mobile CRM by removing the need for boilerplate code, and it should open path for creating reusable JavaScript libraries with additional functionality for Resco mobile apps, e.g., additional math, date, or string operations, and also business logic.
The option to execute scripts directly from rules combines these two options. It aims to simplify writing JavaScript for Mobile CRM by removing the need for boilerplate code, and it should open path for creating reusable JavaScript libraries with additional functionality for Resco mobile apps, e.g., additional math, date, or string operations, and also business logic.
For now, this option is supported for form rules


== Offline HTML ==
== Offline HTML ==
Line 16: Line 18:
== Example ==
== Example ==


TBD
<syntaxhighlight lang="html">
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>JS from Resco rules</title>
    <meta http-equiv='X-UA-Compatible' content='IE=edge' />
    <meta name='viewport' content='initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no'>
    <script type='text/javascript' src='JSBridge.js'></script>
</head>
<body>
    <script>
        function getAccountEntity() {
                                                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.
                                                                contact.addAttributes();
 
                                                                var fetch = new MobileCRM.FetchXml.Fetch(contact);
                                                                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 getLookUp() {
                                                return new Promise(function(resolve, reject) {
                                                                var account = new MobileCRM.FetchXml.Entity("contact");
                                                                account.addAttributes();
 
                                                                var fetch = new MobileCRM.FetchXml.Fetch(account);
                                                                fetch.execute("DynamicEntities", function (res) {
                                                                                var current = res[getRandomInt(res.length)];
                                                                                resolve(new MobileCRM.Reference(current.entityName, current.id, current.primaryName));
                                                                }, reject, null);
                                                });
        }
 
        function getInteger() { return getRandomInt(128); }
        function getString() { return getRandomString(); }
        function getDecimal() {
                                                var precision = 100; // 2 decimals
                                                return Math.floor(Math.random() * (10 * precision - 1 * precision) + 1 * precision) / (1 * precision);
        }
        function getStringList() {
                                                var res = [];
                                                for (var i = 0; i < getRandomInt(); i++)
                                                                res.push(getRandomString());
 
                                                return res;
        }
        function getDateTime() {
                                                var dt = new Date();
                                                return dt.toLocaleTimeString();
        }
 
        // **** Helpers ****
 
        function getRandomString(length) {
                                                var len = length || 7;
                                                return Math.random().toString(36).substring(len);
        }
        function getRandomInt(maximum) {
                                                var max = maximum || 4;
                                                return Math.floor(Math.random() * Math.floor(max));
        }
    </script>
</body>
</html>
</syntaxhighlight>


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

Navigation menu