Friday, August 19, 2016

Dynamic CRM - Ways to retrieve data from database.



In this post, lets go over two different ways to retrieve data from SQL in Dynamics CRM.

Why do we need data:

    Though within CRM, it is easy to review data created towards any specific entity, but getting a customized view to get only specific/complex details as a report/page to senior management might be an option to get started with.

   As an example, lets take a SQL (sample) and try to implement them using direct SQL Fetch and FetchXml options.

SQL for example:

SELECT EmpID, EmpName FROM Employee WHERE EmpID = 101;

SQL Fetch:

This process involves using regular SqlConnection object and respective parameters to fetch data from the table directly. The above sql will be directly passed in as a parameter to Command objects text value. Using a DataReader, retrieved data can be looped through to analyze and handle accordingly;

 Advantage: Exclusive access to any table / entity. Any possible combination of data can be retrieved.

Disadvantage: Works primarily with on-prem versions only. When migrating to Online version, these procedures / SQL's will not be effective.

  To support the online version, we can use Fetch XML to handle them effectively.

FetchXml:

Now lets retrieve the same using Fetch XML.

string fxml = @"<fetch mapping='logical'>
<entity name='employee'>
   <attribute name='empid'/>
   <attribute name='empname'/>
   <filter type='and'>
   <condition attribute='empid' operator='eq' value='100'/></filter>
  </entity>
</fetch>"

Using the above string as input, Employee Entity can be retrieved using  serviceproxy (OrganizationServiceProxy)

Note :

  1. Entity and column names should be lowercase.
  2. Multiple conditions can be included (similar to where clause).


To get started, you can use this website http://sql2fetchxml.com/.

You can enter your SQL and in turn it will derive fetch xml for the provided SQL. Again this website has its own limitations

To know more on what will be supported, visit this page: http://sql2fetchxml.com/Help.aspx. But good enough for a starter to get started..

Happy coding...

XRM Page - getId() returning null or empty string

In this blog, I would like to indicate the usage of Xrm.Page.data.entity.getId() and its responses in various CRM versions and how it affects. Also what we can do to handle appropriately without having exceptions.

The return value for Xrm.Page.data.entity.getId() will return NULL, when called from a Create form (specific to Dynamics CRM version 2011)

The return value for Xrm.Page.data.entity.getId() will return EMPTY STRING, when called from a Create form (specific to Dynamics CRM version 2013).

To handle this given situation in any version (2011,2013 or 2015), you could implement the following code:


 var FormType = Xrm.Page.ui.getFormType();
 if(FormType != 1) //Refers to update form or other Form statuses.
  {
     var myId = Xrm.Page.data.entity.getId();
  }

Happy coding....

Thursday, May 8, 2014

Displaying Wait icon when invoking ajax call...



    In this tutorial, I will go over how to invoke wait icon to display when loading those initial data.. All we need is to define some div tag to show up when it is busy and on the successful return of ajax call, will request the div tag to be hidden and invoke the method(s) for next steps.... (I collected this from few other websites, will try to post the link, when I find it back..).


To get started, lets define a div tag and set up everything that we need to happen when the page is busy loading required data.... Below is a sample of the div tag I am using...


Once we have defined the div tag that will help us display the wait icon, all we need to do is, define where and how it needs to appear. In my code below, I had used before my ajax call and hiding it once I have the successful callback from server. This can be adjusted to suit the needs and handle properly when error condition happens. Code below shows everything that will be needed to display and hide the div tag. 

Happy coding....



Ajax call failed with Internal Server Error (500)



              Recently, when I tried to make an ajax call to my controller, I started getting this error and no way it was visible to me what went wrong. Syntax wise, everything looked great. Passing in the right parameter name from view and it receiving parameter name matched. But this kept occuring only to my last parameter.


               Started looking/searching for possible issues.  It can range between any kind of issues from the size involved for data transfer to missing parameter to setting the data type as string and many more could be found... None of them worked well for me and I know it was a simple mistake that is causing it.

            A quick look again, provided me with what was missing... I am passing in a string value, but receiving as type int. Once I made the type compatible between view and controller, everything started to work.

            Always, this will not be the situation, but this is a possible situation that any one can encounter... Hope this thought / idea will help someone, some where....

 Happy Coding....

Friday, April 4, 2014

Default if Empty...

In this tutorial, will go over using DefaultIfEmpty() in Linq to Objects to handle values when none found.

For this example, I created a sample windows application and wrote the following line of code to test.

In the above code, I had declared a List of type string without adding any values. When I try to access the first value, it is going to throw exception and say none found. Before handling them with try / catch there is another way to add one more step of validation before accessing it.


Above image captures an exception thrown when accessing data in an empty object. In this scenario, there is always an option to use a default value, when no items found. For example, see the image below...

To handle this situation, I had introduced the option to provide Empty string as substitute when list contains no items. Now, when I run the sample code, the output message box will be....


Here, the messagebox displays the default value i.e. empty string. Now lets add some values and try to run the same.


In the above scenario, I added two values to the list item and invoking it will give me the result as User1 (first item in the list). Also, you can use any string value to replace empty string to validate null situations.

Happy coding...

Wednesday, April 2, 2014

Loading jQuery on demand...

There might be situations, where we need to navigate to a new View / Page which does not be part of _layout.cshtml (that is the place where the common includes go). It might help there to check if jQuery is loaded and if not, then it can be included on that page on demand.

An example that worked for me is,

<script type="text/javascript">

    if (typeof jQuery == 'undefined') {
        document.write('<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></' + 'script>');
    }

</script>

I got this reference from a website (which I don't remember), but there are many interesting flavors to it available on the web....

Happy coding....

Friday, March 28, 2014

Moving data from one view to another view in MVC

Though there are many ways to handle this situation, I recently had to try the same and ended up using localStorage and it really helped to handle data at various levels of requirements.

Getting to use it is so easy and simple. Just make sure you have HTML5 for schema validation and rest is swift.

When setting up the value, this is what needs to be done :

<script>
   localStorage.setItem("firsttest", "Hello user...");
</script>

When retrieving the value, just do it as below..

<script>
  var testvalue =  localStorage.getItem("firsttest");
</script>

On a quick note, lets see what is localStorage.

   localStorage - These are nothing but Web Storages, replacing the need to use cookies. Web storages are faster and can accomodate up to 5MB of data. If you want to store / retrieve data based on sessions, then the above can be replaced to use sessionStorage instead of localStorage.

Happy coding....