Home

Showing posts with label MS CRM Scripts. Show all posts
Showing posts with label MS CRM Scripts. Show all posts

Saturday, September 16, 2017

Dynamics 365 Javascript Best Practices

Avoid using unsupported methods
As a general rule, any function which is available through Xrm.Page should be considered as supported and rest are unsupported, if you are navigating the HTML DOM structure and using window.getElementById or even manipulating the innerHTML or outerHTML,These methods may work but because they are not supported you can’t expect that they will continue to work in future versions or update rollups of CRM.

Do not use jQuery for form script or commands
Microsoft do not recommend or support using jQuery for any pages within the application. This includes form scripts and ribbon commands

Recognize limitations for content delivery network (CDN) libraries
Content delivery network (CDN) JavaScript libraries provide many advantages for public websites. Because these libraries are hosted on the Internet, you do not need to create web resources that contain the content of the libraries. For Microsoft Dynamics 365 you should consider the following issues before you use a CDN JavaScript library.
  • Users of the Microsoft Dynamics 365 for Microsoft Office Outlook with Offline Access client have the capability to work with no Internet connection while working offline. If you are depending on an Internet connection for your JavaScript libraries, your code will fail.
  • Some organizations will restrict Internet access for employees. Unless they configure the network to allow access to the CDN library sites, your code may fail for those organizations.


Do not access the DOM
The Microsoft Dynamics CRM development team reserves the right to change how pages are composed, including the ID values for elements, so using the Xrm.Page object model protects your code from changes in how pages are implemented.

Use asynchronous data access methods
When you access data by using the Microsoft Dynamics 365 web services, always use an XmlHttpRequest that is configured to execute asynchronously. The reason is that the browser operates on a single thread. If that thread is being used to execute a long-running process synchronously the browser will stop responding.

Keep your libraries as small as possible
JavaScript libraries are stored in web resources and requested by the form each time a form is loaded.  This means that each time the form for a record is opened every JavaScript library tied to that form is downloaded from CRM’s web resource table.  Keep your libraries as simple and as clean as possible. This will increase performance and improve maintainability.

Keep Common utility file to address the common functions
Keep utility file that can be used to write down all the utility functions that would be used across all entities and custom HTML web resources. A classic example would be something like – getUserRoles(). So instead of this you can do something like this.

Create a webresource named myOrg.Utilities.js then you can define the methods in the file like below.

if (typeof (myOrg) == "undefined") {
   myOrg = { __namespace: true };
}
myOrg.Utilities = {
    _getUserInfo: function(){},
   //Other common functions
}

You can then include the webresource wherever you need. To call the method of the file, you would need to use the fully qualified name as
Var isAdmin = myOrg.Utilities._getUserInfo();

Keep separate scripts for forms and ribbon functions for each entity
For example take account entity as an example keep two sets of scripts for easy identifying one for form events and ribbon events.
Account.Form.js – This can be the script for all your form on-load, on-save and on-change events that you register for the form. So if there is any error you are getting during on save, on load and onchange of the form, you can be rest assured that the origin you can trace from this file.
Account.Ribbon.js – This can be very useful. If you place all the custom ribbon button event handler in this file, you no longer need to check the ribbon diff xml to find out which file the ribbon event handler is located in.
for opportunity.form.js

if (typeof (myOrg) == "undefined") {
   myOrg = { __namespace: true };
}
if (typeof (myOrg.Opportunity) == "undefined") {
   myOrg.Opportunity = { __namespace: true };
}
myOrg.Opportunity.Form= {
   _Load: function(){},
   _Save: function(){}
}

Namespace your library names
Associate each of your functions with a JavaScript object to create a kind of namespace to use when you call your functions, as shown in the following example.
//If the MyUniqueName namespace object isn’t defined, create it.
if (typeof (MyUniqueName) == "undefined")
 { MyUniqueName = {}; }

  // Create Namespace container for functions in this library;
  MyUniqueName.MyFunctions = {
   performMyAction: function(){
   // Code to perform your action.
   //Call another function in your library
   this.anotherAction();
  },
  anotherAction: function(){
   // Code in another function
  }
};

Then when you use your function you can specify the full name. The following example shows this.
MyUniqueName.MyFunctions.performMyAction();



Wednesday, September 13, 2017

Create Entity using Web API in Dynamics 365

Creating an account record with name "sample account" using web api's
https://msdn.microsoft.com/en-us/library/gg328090.aspx
// Request 
POST [Organization URI]/api/data/v8.2/accounts HTTP/1.1
Content-Type: application/json; charset=utf-8
OData-MaxVersion: 4.0
OData-Version: 4.0
Accept: application/json
{
    "name": "Sample Account",  
}
// Response
HTTP/1.1 204 No Content
OData-Version: 4.0
OData-EntityId: [Organization URI]/api/data/v8.2/accounts(GUID)
---------------------------------------------------------------------------------------------------
function createEntity() {
    var entityName = "accounts";// Entity Name
    var clientURL = Xrm.Page.context.getClientUrl();// To get CRM client URl 
    var req = new XMLHttpRequest(); //Creating New HTTP request

    req.open("POST", encodeURI(clientURL + "/api/data/v8.2/" + entityName, true));

    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");

    req.onreadystatechange = function () {
        if (this.readyState == 4 /* complete */) {
            req.onreadystatechange = null;
            if (this.status == 204) {/* Status 204 denotes success */
                alert("account created from api");
            }
            else {
                var error = JSON.parse(this.response).error;
                alert(error.message);
            }
        }
    };

    req.send(JSON.stringify(// json object to be created
        {
            name: "sample account"
        }));

}

Tuesday, December 8, 2015

Set Lookup Value using Javascripts in CRM

function set_LookupValue(fieldName, id, name, entityType) {

        if (fieldName != null && id != null) {
            var lookupValue = new Array();
            lookupValue[0] = new Object();
            lookupValue[0].id = id;
            lookupValue[0].name = name;
            lookupValue[0].entityType = entityType;
            Xrm.Page.getAttribute(fieldName).setValue(lookupValue);
        }

    }

Convert subgrid to Comments