Sunday, 22 May 2016

How to call dialogs in javascript in MS CRM

Opens the specified dialog for a particular record in a CRM light-box, or Modal Dialog if run from Outlook. Once the dialog is closed, a custom callback function can be executed, e.g. to refresh the form with new data set by the dialog.

Parameters: Dialog ID/Guid, Entity Name, Record ID/Guid, Callback function, CRM Base URL (not required on forms/views)


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Process.callDialog("C50B3473-F346-429F-8AC7-17CCB1CA45BC", "contact", 
    Xrm.Page.data.entity.getId(),         
    function () { 
        Xrm.Page.data.refresh(); 
    });
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Don't forget to import Processjs solution into your CRM instance provided by codeplex to make it work.

Note: To not make the GUID of Dialog hard-coded, you can fetch GUID of workflow from it's name using Odata/Web code.

Get and Set option set values/text/object in MS CRM using js

How to get an Option by passing value?
The code below returns an Option object with text and value properties.
Value argument represents the numeric value of the optinonsetvalue field for searching the Option:


 var myOption =Xrm.Page.getAttribute("fieldSchemaname").getOption(value);


How to get selected CRM Option Set Value item?
The code below returns the Option object with text and value properties:


var selectedOption =Xrm.Page.getAttribute("fieldSchemaname").getSelectedOption();

How to get selected value of an CRM Option Set Value?
The code below gives us the numeric value of selected OptionSetValue field:


var optionsetValue = Xrm.Page.getAttribute("fieldSchemaname").getValue();

How to get Text of an CRM Option Set Value?
The code below gives us the text of selected OptionSetValue field:


var optionsetText = Xrm.Page.getAttribute("fieldSchemaname").getText();



How to set an CRM Option Set Value (by numeric value)?
The code below helps us to set the value of OptionSetValue by a numeric value passed to the setValue function:


Xrm.Page.getAttribute("fieldSchemaname").setValue(value);



How to set an CRM Option Set Value by Text?


Actually we can’t set the selected item by text but we can search all options until we find the one whose text (label) is the same as one the one you have then get its value like below:



function setOptionSetValueByText(fieldSchemaname, optionText) {

    var options = Xrm.Page.getAttribute("fieldSchemaname").getOptions();

    for (i = 0; i < options.length; i++) {

        if (options[i].text == optionText) {

        Xrm.Page.getAttribute(fieldSchemaname).setValue(options[i].value);

        }

    }

}

Saturday, 21 May 2016

How to call workflow from javascript in MS CRM

Some time you needs to perform some actions in a record at random time/manually.
For ex: Sending a predefined Manual SMS(which itself would be a custom feature) on  record using a custom button.
In such case traditionally developers tends to create a boolean field and toggle that boolean field using ribbon button using js.
On change of that boolean field workflow is triggered.
BUT it's not necessary to follow the same approach. You can directly call a workflow from javascrip.
Below is the code snippet :
It runs the specified workflow for a particular record asynchronously. Optionally, you can add callback functions which will fire when the workflow has been executed successfully or if it fails.
Parameters: Workflow ID/Guid, Record ID/Guid, Success Callback (function), Error Callback (function), CRM Base URL (not required on forms/views)


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Process.callWorkflow("4AB26754-3F2F-4B1D-9EC7-F8932331567A", 
    Xrm.Page.data.entity.getId(),
    function () {
        alert("Workflow executed successfully");
    },
    function () {
        alert("Error executing workflow");
    });
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Don't forget to import Processjs solution into your CRM instance provided by codeplex to make it work.

Note: To not make the GUID of workflow hard-coded, you can fetch GUID of workflow fromm it's name using Odata/Web code.

Thursday, 19 May 2016

Things to know about New Form Rendering Engine used by MS in CRM 2015 Update 1 and further

Things to know about New Form Rendering Engine used by MS in CRM 2015 Update 1 and furtherand impact of it on page load and custom javascripts and how to prevent issues in unsupported customization using custom code validation tool :


Microsoft Dynamics CRM Online 2015 Update 1 includes a number of terrific technical improvements including faster page loads, improved application performance, and faster email delivery. One noticeable feature is the new Form Rendering Engine. This enhancement loads CRM pages faster so you can view data on the forms quickly.
Users and administrators should be aware of the impact the new engine may have on their customizations and may need to consider enabling the “Legacy Form Rendering” setting if they encounter script errors due to the new form rendering engine. In today’s blog we will focus on the specifics of the new Form Rendering Engine and give you some tips and tricks for working with it! Let’s get started!
Here are a few of the benefits of the new Form Rendering Engine:
  • No action is needed by users or administrator as this is turned on by default.
  • The loading process for record forms is significantly better and faster since more content is cached. For more information on performance, see Microsoft’s note on form rendering engine.
  • Common and custom scripts are handled separately.
  • The engine has the same functionality as it is based on previous generations. It uses Form XML Schema and has full support for client scripting.
The tricky thing for users and administrators has to do with unsupported customization or direct DOM manipulations which will possibly fail and need to be re-engineered. The following are examples of when this might occur:
  • Access window.parent from a web source that has to do with a variable set.
  • Unsupported APIs, i.e., non-XRM.
  • Window.load or iframe/form.
  • Access DOM in content iframe using JQuery or JavaScript.
In order to help catch unsupported customization, MS has added a dialog that displays the issue when script errors occur so that they do not fail silently. If these symptoms occur then it is likely there are unsupported customization in the system.
We recommend formally testing any scripts, solutions, and customization in a sandbox to ensure proper functionality AS SOON AS POSSIBLE. To assist with testing and in order to prevent errors from going unnoticed, Microsoft has added a dialog box that will display the following error message should a script error occur:

The following two options are recommended when identifying potential issues:

  • For those pesky issues that are difficult to pinpoint or that need a post-upgrade fallback plan, consider Legacy Form Rendering.
Under Settings -> System Settings -> General tab, select Yes for “Use legacy form rendering” as shown in the images below.
Note: this is a temporary measure, as this functionality is expected to be removed by Microsoft in the next CRM release. All the more reason to update any broken script immediately to avoid problems in the future.