Wednesday, July 24, 2013

How to load a web resource using Java Script

In Dynamics CRM 2011, if we implement JavaScript for particular form we add script web resource in the Form Libraries list under form customization, if there is any related script which need to be referred,  then we need to include that script as well in the Form Libraries. As you can see in the below screen, we have included “new_SDK.Rest” and “new_Json2”. We have not called the function of those scripts in the “Event Handlers”, but we have used them in our library “new_Account_Library.jsa

 




Now suppose if you need to add a button on the Home Page ribbon of entity, and this button should call some function in the “new_Account_Library.js” library. This means the button is *not* calling any HTML or Silverlight page.


In such case, if we use the HTML page instead of the javascript then you can load the related script library with the following line of code.

<script src="../new_sdk.rest" type="text/javascript"></script>




 

Here the problem is how to add/ load related JScript reference like in this case sdk.rest jscript and json Jscript reference. To retrieve the data from CRM we generally use functions from the sdk.rest library. Hence this library “sdk.rest” needs to be loaded as we are referring the function of “sdk.rest” library in this “new_Account_Library.js” script.


So to resolve this we can simply load referred web resource from JScript programmatically before performing our business logic, below is the function you can refer to.


We need to pass name of web resource.


LoadWebResource(Name of the Web Resource);

e.g.

LoadWebResource(“new_json2”)

LoadWebResource(“new_SDK.Rest”)


Function for load web resource:


//Function to Load webResource


function LoadWebResource(resource) {

 var httpRequest = null;


 try {    



if (window.XMLHttpRequest) {  // code for IE7+, Firefox, Chrome, Opera, Safari

            httpRequest = new XMLHttpRequest();


}
      else// code for IE6, IE5


httpRequest = new ActiveXObject("Microsoft.XMLHTTP");



}
              var serverUrl= Xrm.Page.context.getServerUrl();

             if (serverUrl.match(/\/$/)) {


serverUrl = serverUrl.substring(0, serverUrl.length - 1);

}
 
httpRequest.open("GET", serverUrl + "/webresources/" + resource, false);

httpRequest.send(null);

eval(httpRequest.responseText);

}


catch (e) {
 
alert("LoadWebResource >> Error loading " + resource + ":\n" + e.description);


  }

}

Thursday, July 18, 2013

Maplytics Dashboards

Today we will demonstrate a simple though very useful feature of our flagship product Maplytics, which is Dashboards. Maplytics Dashboards provides a fast and easy way to view and communicate business data in a visual format.


Maplytics is a plug and play application made available as Managed solution that can be easily installed and uninstalled without affecting other CRM components. Maplytics is a Geo-Analytics tool designed for Microsoft Dynamics CRM 2011(Online/On-Premise/IFD/Office 365) using the API services provided by Bing maps.

Maplytics Dashboards make it simple for sales, marketing, and customer service people to create dashboards on the fly—so they can spend more time acting on critical data, and less time gathering it.

Here we have taken an example of Appointments; Maplytics Dashboard will provide you with detailed information of the appointment scheduled with the contact for that day.



 
Another example could be for Cases to be resolved today or your sales pipeline which is shown in below screen shot.


As we are always looking for ways to better understand our data and showing data on Map alone like new leads within the month, monthly sales; provides quick and easy insights. However it doesn’t allow you to see trends like new leads within this month, new accounts within this month, volume of sales. Maplytics provides you with a feature called Heat Map. Heat map is an Advanced Map based visualization which allows better analysis of such data.


Thus Maplytics will provide you with easy insights with trends being followed and would also help you to serve your customer better in every possible way.


Please contact us at crm@inogic.com for more details and trial requests of Maplytics

Sunday, July 14, 2013

Check attribute type/formats in CRM 2011 javascript.


Now we can check the attribute formats in CRM java script.

To do this you just need to use the function “getFormat”. This function returns a string value that represents formatting options for the attribute. You can refer the below code to check the format of different types of attributes.

function GetFormatOfAttributes() {
    var attributeFormat;
    try {

        //Get the format of String
        attributeFormat = Xrm.Page.getAttribute("name").getFormat();

        alert("The format of name is " + attributeFormat);





        //Get the format of Email attribute
        attributeFormat = Xrm.Page.getAttribute("emailaddress1").getFormat();

        alert("The format of emailaddress1 is " + attributeFormat);




               

        //Get the format of the Date and Time attribute
        attributeFormat = Xrm.Page.getAttribute("createdon").getFormat();

        alert("The format of createdon is " + attributeFormat);





               

        //Get the format of Duration
        attributeFormat = Xrm.Page.getAttribute("new_duration").getFormat();

        alert("The format of Duration is " + attributeFormat);






        //Get the format of Date Only
        attributeFormat = Xrm.Page.getAttribute("new_date").getFormat();

        alert("The format of new_date is " + attributeFormat);






        //Get the format of Time Zone
        attributeFormat = Xrm.Page.getAttribute("address1_utcoffset").getFormat();

        alert("The format of address1_utcoffset is " + attributeFormat);






       //Get the format of Url
        attributeFormat = Xrm.Page.getAttribute("websiteurl").getFormat();

        alert("The format of websiteurl is " + attributeFormat);





               

        //Get the format of multi line text
        attributeFormat = Xrm.Page.getAttribute("description").getFormat();

        alert("The format of description is " + attributeFormat);





    }
    catch (e) {
        alert("GetFormatOfAttributes Err >> " + e.description);
    }
}




Thursday, June 27, 2013

How to convert the DateTime in Local and UTC Date Time format

While working with the plug-ins or with Custom Workflows Assemblies, we get the Datetime from CRM which doesn’t match with user’s Local DateTime format.

As we get the date from the CRM in UTC Format so we can convert it to User’s Local DateTime using LocalTimeFromUtcTimeRequest Request.

To get the Current logged in user’s Local time You need to first retrieve the Time Zone code of that user using RetrieveCurrentUsersSettings request and then you need to convert the UTC date into user’s Local DateTime.

We can also convert the Date from Local Datetime format  to the UTC Datetime Format.

Below examples illustrates how to write LocalTimeFromUtcTimeRequest  and UtcTimeFromLocalTimeRequest request to convert the Date.

Example :

First retrieve the time zone code from UserSettings entity and then excecute request LocalTimeFromUtcTimeRequest to convert the DateTime from UTC Format to Local DateTime format and UtcTimeFromLocalTimeRequest to convert the DateTime from Local to UTC DateTime format as given below.

                        
DateTime convertDate = new DateTime();



           //get the Time Zone Code of user

            int? getTimeZoneCode = RetrieveCurrentUsersSettings(service);



//Convert the UTC Date time into Users Local DateTime Format using request LocalTimeFromUtcTimeRequest

            DateTime localDateTime = RetrieveLocalTimeFromUTCTime(convertDate, getTimeZoneCode, service);

            //Convert the Local Date time into UTC DateTime Format using request UtcTimeFromLocalTimeRequest

            DateTime utcDateTime = RetrieveUTCTimeFromLocalTime(convertDate, getTimeZoneCode, service);



        /// <summary>

        /// Retrieves the current users timezone code

        /// </summary>

        /// <param name="service"> IOrganizationService </param>

        /// <returns></returns>

        private int? RetrieveCurrentUsersSettings(IOrganizationService service)

        {

            var currentUserSettings = service.RetrieveMultiple(

                new QueryExpression("usersettings")

                {

                    ColumnSet = new ColumnSet("timezonecode"),

                    Criteria = new FilterExpression

                    {

                        Conditions =

                        {

                            new ConditionExpression("systemuserid", ConditionOperator.EqualUserId)

                        }

                    }

                }).Entities[0].ToEntity<Entity>();

            //return time zone code

            return (int?)currentUserSettings.Attributes["timezonecode"];

        }



        /// <summary>

        ///  Retrive the local time from the UTC time.

        /// </summary>

        /// <param name="utcTime">UTC Date time which needs to convert to Local DateTime</param>

        /// <param name="timeZoneCode">TimeZoneCode</param>

        /// <param name="service">IOrganizationService service</param>

        /// <returns></returns>

        private DateTime RetrieveLocalTimeFromUTCTime(DateTime utcTime, int? timeZoneCode, IOrganizationService service)

        {

            if (!timeZoneCode.HasValue)

                return DateTime.Now;

            var request = new LocalTimeFromUtcTimeRequest

            {

                TimeZoneCode = timeZoneCode.Value,

                UtcTime = utcTime.ToUniversalTime()

            };

            var response = (LocalTimeFromUtcTimeResponse)service.Execute(request);

            return response.LocalTime;

        }



        /// <summary>

        ///  Retrive the UTC DateTime from Local Date time format.

        /// </summary>

        /// <param name="localTime">Local Date time which needs to convert to UTC</param>

        /// <param name="timeZoneCode">TimeZoneCode</param>

        /// <param name="service">IOrganizationService service</param>

        /// <returns></returns>

        private DateTime RetrieveUTCTimeFromLocalTime(DateTime localTime, int? timeZoneCode, IOrganizationService service)

        {

            if (!timeZoneCode.HasValue)

                return DateTime.Now;



            var request = new UtcTimeFromLocalTimeRequest

            {

                TimeZoneCode = timeZoneCode.Value,

                LocalTime = localTime

            };

            var response = (UtcTimeFromLocalTimeResponse)service.Execute(request);

            return response.UtcTime;

        }









Tuesday, June 25, 2013

How to show List Box Items Horizontally in CRM 2011 using Silverlight

As we know default List box shows list of items in vertical Format. We need to scroll down to view all Items. If you want to show Items horizontally with the horizontal Scrollbar then it can be done with the following way:

1)      You need to first setup <ListBox> in XAML Design. <ListBox> should contain <ItemsPanelTemplate> to where it will change the default template of <ListBox> from vertical to horizontal by adding <StackPanel> with its orientation as "Horizontal".

<ListBox Name="imageList">
<ListBox.ItemsPanel>
              <ItemsPanelTemplate>
                 <StackPanel Name="horizontalOrientation" VerticalAlignment="Top" Orientation="Horizontal"/>
            </ItemsPanelTemplate>
       </ListBox.ItemsPanel>

       <ListBox.ItemTemplate>
              <DataTemplate>                           
                    <Border BorderBrush="Black" BorderThickness="1">
                            <StackPanel  Name="DataStack" Height="Auto" Width="Auto" Orientation="Vertical">                              
                                <StackPanel Orientation="Horizontal" Name="AccountName">
                                <TextBlock Name="Account" Text="Sub Account:" Width="100"/>
                                <Image Name="AccountImage" Source="/HorizontalListImagesView;component/Images/ico_16_1.png" Height="20" Width="20"/>
                                <TextBlock Name="AccountName" Text="{Binding Path=Name, Mode=TwoWay}"/>
                                </StackPanel>
                               
                                <StackPanel Orientation="Horizontal">
                                <TextBlock Name="Contact" Text="Primary Contact:" Width="100"/>
                                <Image Name="ContactImage" Source="/HorizontalListImagesView;component/Images/ico_16_2.png" Height="20" Width="20" />
                                <TextBlock Name="ContactName" Text="{Binding Path=PrimaryContactId.Name, Mode=TwoWay}"/>
                                </StackPanel>

                                <StackPanel Orientation="Horizontal">
                                <TextBlock Name="EmailAddress" Text="Email:"  Width="100"/>
                                <TextBlock Name="Email" Text="{Binding Path=EMailAddress1,Mode=TwoWay}"/>                               
                                </StackPanel>

                                <StackPanel Orientation="Horizontal">
                                <TextBlock Name="PhoneNumber" Text="Main Phone:" Width="100"/>
                                <TextBlock Name="Phone" Text="{Binding Path=Telephone1,Mode=TwoWay}"/>
                                </StackPanel>
                            </StackPanel>
                            </Border>
              </DataTemplate>
       </ListBox.ItemTemplate>
</ListBox>



2)      Now just design your List in Data Template and bind item Source to it. It will show following output.