Wednesday, 27 September 2017

Cross browser code to get Workflow Id in JavaScript

Hello everyone, in my last blog I have mentioned the process to get workflow GUID in JS and trigger the same WF from JS file. But only problem is that code is browser specific. That means it will work only when we will open CRM application in Internet Explorer. For other browsers (Chrome and Firefox) it will not work.
Earlier code was:
function GetWorkflowId(wfName) {
    var serverUrl = Xrm.Page.context.getClientUrl();
    //Odata query to fetch the WF Guid by WF name
    var odataQuery = serverUrl + '/XRMServices/2011/OrganizationData.svc /WorkflowSet?$select=WorkflowId&$filter=StateCode/Value eq 1 and ParentWorkflowId/Id eq null and Name eq \'' + wfName + '\'';
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("GET", odataQuery, false);
    xmlHttp.send();
    if (xmlHttp.status == 200) {
        var result = xmlHttp.responseText;
        var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.loadXML(result);
        return xmlDoc.getElementsByTagName("d:WorkflowId")[0].childNodes[0].nodeValue;
    }
}
Here Chrome and Firefox does not recognize ActiveXObject object and throws error as “ActiveXObject does not defined.
Then I have tried below code
function GetWorkflowId(wfName) {
    try {
        var serverUrl = Xrm.Page.context.getClientUrl();
        var odataQuery = serverUrl + '/XRMServices/2011/OrganizationData.svc/WorkflowSet?$select=WorkflowId&$filter=StateCode/Value eq 1 and ParentWorkflowId/Id eq null and Name eq \'' + wfName + '\'';
        var xmlHttp = new XMLHttpRequest();
        xmlHttp.open("GET", odataQuery, false);
        xmlHttp.send();
        if (xmlHttp.status == 200) {
            var result = xmlHttp.responseText;
            var xmlDoc;
            try {
                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                xmlDoc.async = false;
                xmlDoc.loadXML(result);
            }
            catch (e) {
                var parser = new DOMParser();
                xmlDoc = parser.parseFromString(result, "text/xml");
            }
            return xmlDoc.getElementsByTagName("d:WorkflowId")[0].childNodes[0].nodeValue;
        }
    }
    catch (e) {
        alert(e.message);
    }
}
But again this code doesn’t support Chrome and throws error but it works on Firefox and IE.  But I need a proper working code irrespective of browsers used to run CRM application, so I have changed the code snippet contains “ActiveXObject” with below code
function GetWorkflowId(wfName) {
    var serverUrl = Xrm.Page.context.getClientUrl();
    //Odata query to fetch the WF Guid by WF name
    var odataQuery = serverUrl + '/XRMServices/2011/OrganizationData.svc /WorkflowSet?$select=WorkflowId&$filter=StateCode/Value eq 1 and ParentWorkflowId/Id eq null and Name eq \'' + wfName + '\'';
    requestResults1 = RestCall(odataQuery);
    if (requestResults1 != null && requestResults1.results.length == 1) {
        var WorkFlow = requestResults1.results[0];
        var Wid = WorkFlow.WorkflowId;
        return Wid;
    }
}
:) :)

Create dependent optionsets in MS Dynamics CRM


 This blog is based on sample code provided by Microsoft and can be reviewed in the SDK located \SDK\SampleCode\JS\FormScripts.
Recently I have got a requirement to filter the value of one option set field by a value chosen in another option set field. This is not an out of box features of CRM so we have to implement this functionality using web resources.
Below are steps to create dependent option set:-
1.     Create both parent and child option set field and insert their all options.
2.     Create a Webresource of type Jscript and upload the JScript file from
   SDK -> SsmpleCode -> JS -> FormScripts -> SDK.DependentOptionSet.js
3.      Create Another Webresource of type XML and upload the XML file from
  SDK -> SsmpleCode -> JS -> FormScripts -> DependentOptionSetConfig.xml
Modify the XML based upon your options from Parent optionSet and Dependent optionSet
<!--<snippetDependentOptionSetConfig.xml>-->
<DependentOptionSetConfig entity="account" >
<ParentField id="new_parent"
           label="Parent">
<DependentField id="new_child"
               label="Child" />
<Option value="1"
       label="India">
<ShowOption value="1"
            label="New Delhi" />
<ShowOption value="2"
            label="Mumbai”/>
<ShowOption value="3"
            label="Patna”/>
</Option>
<Option value="2"
       label="USA">
<ShowOption value="4"
            label="New Jersy” />
<ShowOption value="5"
            label="Newyork" />
<ShowOption value="6"
            label="Texas” />
</Option>
<Option value="3"
       label="United Kingdom">
<ShowOption value="7"
            label="London" />
<ShowOption value="8"
            label="Amsterdom" />
<ShowOption value="9"
            label="Paris" />
</Option>
</ParentField>
</DependentOptionSetConfig>
<!--</snippetDependentOptionSetConfig.xml>-->
4.     Attach the JS Liabrary and add below function on OnLoad event of the Form and pass the config xml as parameter.
Function :-  SDK.DependentOptionSet.init
Parameter :- “new_DependentOptionSetConfig.xml
5.     Add below function at onChage Event of the parent OptionSet
Function :- SDK.DependentOptionSet.filterDependentField 
Parameter :- “new_parent”,”new_child”
6.     Now publish all the changes and open any related record.
7.     After the selection of the Parent optionset, the Dependent optionset will show the dependent values.
Hope this was helpful.
Thanks


Error Uploading Report


Have you ever got below error message while you trying to upload new report???

Some day ago I got a urgent issue from my Client to change some data in reports. 
Earlier I used to create SSRS report using SQL Server Data Tools 2012 and Visual Studio 2012.But when CRM application was updated to Dynamics 365 its stopped working. 
So I installed following components.
  • Visual Studio 2015 and Data Tools
  • Report Authoring Extensions for Dynamics 365
After that I was able to connect my SSRS to  365 CRM application. 

I updated my reports. But when i tried to upload it to CRM solution every time I was getting same error message. Constantly same error message :(

Error Uploading Report
This report can’t upload. This issue can occur when the Report Definition Language (RDL) file is not valid. If you contact support, please provide the technical details.

So I looked in log file

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An error occurred while trying to add the report to Microsoft Dynamics 365. Try adding the report again. If this problem persists, contact your system administrator.Detail:
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
  <ActivityId>bef8f11f-6f63-4f07-b831-96ab39317c1d</ActivityId>
  <ErrorCode>-2147188072</ErrorCode>
  <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
  <Message>An error occurred while trying to add the report to Microsoft Dynamics 365. Try adding the report again. If this problem persists, contact your system administrator.</Message>
  <Timestamp>2017-09-26T11:25:37.1478996Z</Timestamp>
  <ExceptionRetriable>false</ExceptionRetriable>
  <ExceptionSource i:nil="true" />
  <InnerFault i:nil="true" />
  <OriginalException i:nil="true" />
  <TraceText i:nil="true" />
</OrganizationServiceFault>

But again it was not useful.
So I looked for another workaround as below:
  • I  downloaded a existing report rdl
  • And tried to upload it back to CRM
And it was successfully uploaded.
Then :
  • I loaded the downloaded rdl into the Visual Studio
  • Did some minor changes
  • And tried to upload it back into Dynamics
But again I was getting same error message, And I knew there is some version mismatch.
So I googled it and found a solution
  • Go to Project property
  • Change the Target ServerVersion to "SQL Server 2008 R2,2012 or 2014"
  • Build the solution

Again I tried to upload rdl to Dynamics but got same error message. 

Then I looked the rdl code and got strange issue. rdl was still targeting to 2016 version of BIDS.


So again I googled and found below solution:
  • Change the target server version of your report project (make it 2008, …, 2014)
  • Build/Rebuild the project!!
  • Do not upload the original rdl file to Dynamics – instead, use the one you’ll find in the “bin/debug” or “bin/release” folder
And now it was successfully uploaded. :) :)

Have you ever faced an issue where you need to import some record without changing GUID of the record? For example if we need to migr...