Resco JavaScript Bridge: Difference between revisions

Jump to navigation Jump to search
Line 799: Line 799:
* entity edit dialog.
* entity edit dialog.


In the following example, we create a simple HTML page for a Contact associated with an Account. It displays just the Contact’s name. After clicking on one of items (name), standard Contact edit dialog appears. There is one more option to create a new associated contact: by opening entity new dialog with a predefined Account id. In our solution, we don’t use auto-reload when you save a newly created associated contact. We just create the button with a function to reload the page. We also use some basic Javascript and DOM html functions to print the result on a page.  
In the following example, we create a simple HTML page that you can add to your home. It includes simply two buttons: one opens the account form and displays one record, the other opens a new contact form.


<syntaxhighlight lang='js'>
<syntaxhighlight lang='js'>
function OpenForm()
function OpenAccountEditForm() {
{
    var entity = new MobileCRM.FetchXml.Entity("account");
   data.innerHTML = "";
    entity.addAttributes();
   var entity = new MobileCRM.FetchXml.Entity("account");
   //entity.addAttributes();
   var linkEntity = entity.addLink("contact", "parentcustomerid", "accountid", "inner");
   linkEntity.addAttribute("contactid");
   linkEntity.addAttribute("fullname");


   entity.filter = new MobileCRM.FetchXml.Filter();
    var fetch = new MobileCRM.FetchXml.Fetch(entity, 1); // fetch only one record.
   entity.filter.where("accountid", "eq", accountId);
    fetch.execute(
 
        "DynamicEntities",
   var fetch = new MobileCRM.FetchXml.Fetch(entity);
        function(res) {
   fetch.execute(
            if (res && res.length > 0) {
      "Array",
                MobileCRM.UI.FormManager.showEditDialog(res[0].entityName, res[0].id);
      function (res) {
            }
         if (res && res.length > 0) {
        }, MobileCRM.bridge.alert);
         for (var i in res) {
            var contact = res[i];
         try {
            var a = document.createElement("a");
            a.href = "#";
            a.id = contact[0];
            a.addEventListener("click",
               function (e)
               {
                  MobileCRM.UI.FormManager.showEditDialog("contact", e.target.id);
                  reloadData();
               },
            false);
 
            a.appendChild(document.createTextNode(contact[1]));
            data.appendChild(a);
            data.appendChild(document.createElement("br"));
            }
         catch (err)
         {
            alert("Exception Error : \n\n" + err);
         }
      }
   }
   },function (err) {alert(err); });
}
}


   function addAssociatedContact()
function openNewAssociatedContactForm() {
   {
    var entity = new MobileCRM.FetchXml.Entity("account");
   // Create reference for associated account
    entity.addAttributes();
   try{
      var target = new MobileCRM.Reference("account", accountId);
      var relationShip = new MobileCRM.Relationship("parentcustomerid", target, null, null);
      MobileCRM.UI.FormManager.showNewDialog("contact", relationShip);


      // Show all associated contact at the begining;
    var fetch = new MobileCRM.FetchXml.Fetch(entity, 1); // fetch only one record.
      reloadData();
    fetch.execute(
      } catch (err)
        "DynamicEntities",
      {
        function(res) {
         alert("Exception : " + err);
            if (res && res.length > 0) {
      }
                var target = new MobileCRM.Reference(res[0].entityName, res[0].id, res[0].primaryName);
   }
                var relationShip = new MobileCRM.Relationship("parentcustomerid", target, null, null);
                MobileCRM.UI.FormManager.showNewDialog("contact", relationShip);


   function reloadPage()
            }
   {
        }, MobileCRM.bridge.alert);
      document.location.reload(true);
}
   }
 
   function reloadData()
   {
      OpenForm();
   }</syntaxhighlight>
 
First, we use FetchXML to get data and in loop of getting result data we create the DOM element object of “a” and create event handler “onclick” with a function to Open Edit Form for specified Account. Function “reloadData” is used to fetch and display data.
 
<syntaxhighlight lang='js'>
a.addEventListener("click",function (e) {
   MobileCRM.UI.FormManager.showEditDialog("contact", e.target.id);
   reloadData();
   }, false);
</syntaxhighlight>
 
To create a new associated contact we use Method for FormManager object, showNewDialog.
 
We need to create a reference for the specified account and create a relationship object, which contains our reference as a target of relationship.
 
<syntaxhighlight lang='js'>
function addAssociatedContact()
   {
      // Create reference for associated account
      try{
         var target = new MobileCRM.Reference("account", accountId);
         var relationShip = new MobileCRM.Relationship("parentcustomerid", target, null, null);
         MobileCRM.UI.FormManager.showNewDialog("contact", relationShip);
 
         // Show all associated contact at the begining;
         reloadData();
      } catch (err)
      {
         alert("Exception : " + err);
      }
   }
</syntaxhighlight>
</syntaxhighlight>


Line 906: Line 837:


<syntaxhighlight lang='html'>
<syntaxhighlight lang='html'>
<h3>Account : </h3>
<h3>Welcome to form manager demonstration</h3>
   Name <p id="accountName"></p>
<button id="OpenAccountEditForm" onclick="OpenAccountEditForm()">Open some account</button><br /><br />
   <button id="reload" onclick="reloadPage()">Reload page</button><br /><br />
<button id="openNewAssociatedContactForm" onclick="openNewAssociatedContactForm()">Add associated contact</button><br /><br />
   <button id="reload" onclick="reloadData()">Reload Data</button><br /><br />
</syntaxhighlight>
   <button id="createNew" onclick="addAssociatedContact()">New Contact</button><br /><br />
   <div id="data">
</div></syntaxhighlight>


== EntityList ==
== EntityList ==

Navigation menu