Execute JavaScript from rules: Difference between revisions

Jump to navigation Jump to search
No edit summary
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{WIP}}
{{Bridge TOC}}
The [[Resco platform]] offers two main options for adding [[business logic]] to your projects:
The [[Resco platform]] offers two main options for adding client-side [[business logic]] to your projects:
* [[Rules editor|Rules]] which represent the no-code approach for simpler use cases
* [[Rules editor|Rules]] which represent the no-code approach for simpler use cases
* [[Resco JavaScript Bridge]] that allows you to write custom scripts to modify UI, data, and access additional functions
* [[Resco JavaScript Bridge]] that allows you to write custom scripts to modify UI, data, and access additional functions


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 a 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.


{{Note|For now, this option is supported for form rules. Do not attempt to use the ExecuteJS function in other rules, for example, in views.|Warning}}
{{Note|For now, this option is supported for form rules and questionnaires. Do not attempt to use the ExecuteJS function in other rules, for example, in views.|Warning}}


== Offline HTML ==
== Offline HTML ==
Line 18: Line 18:
# Edit a [[form]] in Woodford.
# Edit a [[form]] in Woodford.
# Click '''Add IFrame''' and link to the file script that you uploaded to the offline HTML folder in the previous section.
# Click '''Add IFrame''' and link to the file script that you uploaded to the offline HTML folder in the previous section.
# Next, open the rules editor for an event, for example '''On Save'''.
# Next, open the rules editor for an event, for example, '''On Save'''.
# Create a new variable and use the operation ExecuteJS.
# Create a new variable and use the [[Rules_editor#Operations|operation '''ExecuteJS''']].


[[File:Sample executejs rule.png|600px]]
[[File:Sample executejs rule.png|600px]]
Line 25: Line 25:
== Additional information ==
== Additional information ==


* The operator ExecuteJS is supported only for variables of the following types: Boolean, String, Integer, Double, Float, Decimal, Money, DateTime, StringList, Entity, Lookup.
* In the first argument, select the IFrame form tab that links to the script. In the second argument, enter the name of the JavaScript function.
* The serialized MobileCRM.UI.EntityForm object is automatically passed as a parameter to the function. You cannot pass a custom input parameter.
* The serialized MobileCRM.UI.EntityForm object is automatically passed as a parameter to the function. You cannot pass a custom input parameter.
* The operator ExecuteJS is supported only for variables of the following types: Boolean, String, Integer, Double, Float, Decimal, Money, DateTime, StringList, Entity, Lookup.
* The implementation supports also ''promises'' for asynchronous result return from functions.
* The implementation supports also ''promises'' for asynchronous result return from functions.
== Questionnaire specifics ==
The behavior of the ExecuteJS operator in questionnaire rules is similar to forms:
* It is supported for variables and also whenever you can enter a value; for example for Question.Value.
* In the first argument, simply select the value "IFrame". (Questionnaires don't have multiple tabs for selecting.)
[[File:Execute javascript in questionnaire rules.png|600px]]


== Example ==
== Example ==


<syntaxhighlight lang="html">
<syntaxhighlight lang="html"><!DOCTYPE html>
<!DOCTYPE html>
<html>
<html>
<head>
<head>
Line 45: Line 53:
         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 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(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);
Line 56: Line 88:
             });
             });
         }
         }
        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 111:
             });
             });
         }
         }


         function getInteger() { return getRandomInt(128); }
         function getInteger() { return getRandomInt(128); }
Line 80: Line 124:
             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 87: Line 133:
             return dt.toLocaleTimeString();
             return dt.toLocaleTimeString();
         }
         }


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


         function getRandomString(length) {
         function getRandomString(length) {
Line 100: Line 150:
     </script>
     </script>
</body>
</body>
</html>
</html></syntaxhighlight>
</syntaxhighlight>


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

Navigation menu