Wednesday, 1 October 2014

Create a new log file When the Size of Current log file Crosses size limit

Logging messages is always a good practice in development.You can check the log file to know where the application is breaking.Sometimes a log file becomes huge in size and takes some time to open.

Below is C# code to create a new log file When the Size of Current log file Cross size limit.You can define your maximum size of file say 5 MB.
       

          public static void writeLog(string text)
        {
            try
            {
                long length = 0;
                string fileName = "My_Logger_1" + "_" + DateTime.Now.Year.ToString("0000") + "_" + DateTime.Now.Month.ToString("00") + "_" + DateTime.Now.Day.ToString("00") + ".log";
                string filePath = string.Empty;
                string[] logfileVersion = new string[] { };

                string date = System.DateTime.Now.Year.ToString() + "_" + System.DateTime.Now.Month.ToString() + "_" + System.DateTime.Now.Day.ToString();
                string logpath = @"D:\Logger";
                int MAX_FILE_SIZE = 5242880;//5MB

                filePath = logpath + "\\" + fileName;



                if (File.Exists(filePath))
                {
                     List<String> files = getTodaysFiles(logpath);

                    length = new System.IO.FileInfo(files[files.Count - 1]).Length;
                    logfileVersion = Path.GetFileName(files[files.Count - 1]).Split('_');
                    filePath = files[files.Count - 1];
                }


                StreamWriter LogWriter = new StreamWriter(new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.Read));

                if (length > MAX_FILE_SIZE)
                {
                   
                    int nextVersion = Convert.ToInt32(logfileVersion[2]) + 1;
                    LogWriter.Close();

                    LogWriter = new System.IO.StreamWriter(new FileStream(string.Format(
                    logpath + "\\My_Logger_{0}" + "_" + DateTime.Now.Year.ToString("0000") + "_" + DateTime.Now.Month.ToString("00") + "_" + DateTime.Now.Day.ToString("00") + ".log", nextVersion), FileMode.Append, FileAccess.Write, FileShare.Read));

                }
                LogWriter.WriteLine(DateTime.Now.ToString() + "  -  "+text);
                LogWriter.Close();


            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<String> getTodaysFiles(String folderPath)
        {
            List<String> todaysFiles = new List<String>();
            foreach (String file in Directory.GetFiles(folderPath))
            {
                DirectoryInfo di = new DirectoryInfo(file);
                if (di.CreationTime.ToShortDateString().Equals(DateTime.Now.ToShortDateString()) && di.Name.Contains("My_Logger"))
                {
                    todaysFiles.Add(file);

                }

            }
            return todaysFiles;
        }
       
 


Friday, 19 September 2014

MS CRM 2013 Showing a Specific Process Stage

Suppose you have a Business Process Flow with multiple Stages on entity Opportunity say

1.Quality
2.Develop
3.Propose
4.Close

Now I want to create the opportunity from Stage 3(Propose).Using JavaScript you can easily achieve this.

Solution: We should get the ProcessID and StageID for the Given Business Process Flow.

Get the ProcessID and  use ProcessStage table to get the Stages  where ProcessID equals same.it will display StageID for all the stages. Get the Propose StageID.

Use JavaScript to open the new record :

var parameters = {};
parameters["formid"] = "A837E4A7-01B8-4F82-A475-BE9ABD67E667";//Specific form id
parameters["stageid"] = "1B015DCA-9790-D8D0-0425-54EECEE24900"; // Stage ID for a specific Stage say "Propose"
Xrm.Utility.openEntityForm("opportunity", null, parameters);

Result:


 


Hope it helps,
Yusuf




MS CRM 2011 and 2013 not working properly with Chrome.

One day while opening MS CRM 2011 in chrome browser I found out that it is not showing  modal dialog. Although it was working fine in IE.I thought there is some bug in application.

Some other day I was working on CRM 2013 and I noticed some functionalities are broken in Chrome.

1.Button Click events were not working for a dynamatically created HTML DIV.
2.On Form customization I faced some issues.

Later I found out that it is due to update of Chrome browser (37).They removed  number of API which results in such issues.

Below is the explanation  and workaround of this  issue.
http://xrmrocks.com/2014/09/08/chrome-37-breaks-crm-2011-functionality/

http://www.crmanswers.net/2014/09/google-chrome-registry-fix-for-dynamics.html


Hope this helps,

Yusuf





Thursday, 12 December 2013

Get the record ID from the URL

As a developer, you will find yourself extracting record ID from the URL.Most of the time we open the notepad and using our  ID extracting skills we strip out the ID from the URL.To make this task more easier just use URL Decoder/Encoder


1. Copy and paste your URL in the textbox
2. Press Decode button two times.
3.Your URL is decoded you can easily copy the ID.



Hope this helps.

Wednesday, 14 August 2013

Showing only selected Tab on the MS CRM 2011 Form

When you open any form it loads all the tabs on the form.Suppose you want to display the selected tab on the form at a time.Here is a JavaScript function to hide the other tabs.This function can be called on the Form OnLoad and TabStateChange events.

1.JavaScript Function:

function ToggleTabDisplayState(name) {
 
    var currentTab = Xrm.Page.ui.tabs.get(name);
    if (currentTab != null && currentTab.getDisplayState() == "expanded") {
        var currentTabId = currentTab._control.get_id();
        var tabs = Xrm.Page.ui.tabs.get();
        for (var i in tabs) {
            var tab = tabs[i];
            var tabId = tab._control.get_id();
            if (tabId != currentTabId) {
                if (tab.getDisplayState() == "expanded") {
                    tab.setDisplayState("collapsed");
                }
                document.getElementById(tabId).style.display = "none";
            }
        }
    }
}
2.Before Calling the Function :
Form shows all the tabs before calling the above function Form OnLoad event.
 
 
 
 3.Calling the Function :
 
3.1 Call on Form OnLoad event :
 
Suppose on Form OnLoad event I want to show only "General" tab and hide other tabs then call the above function as below.
 
function Form_OnLoad() {
    ToggleTabDisplayState("general"); //"general" is the name of the tab
}
 
3.2 Call on TabStateChange event:
 
Call the same function on the TabStateChange event 
 
Go to Form Properties =>Select Control as tab name=>Add Handler =>Select Library=>add function as ToggleTabDisplayState =>Provide name of the tab in Comma separated parameters list for example 
"contacts" => Save and publish


4.After Calling the Function :
Form shows only selected tab after calling the above function Form OnLoad event.
 
 










Hope it helps,
Regards,
Yusuf



Tuesday, 16 July 2013

Retrieving Subgrid Items count using Javascipt in MS CRM 2011/MS CRM 2013

Retrieving Subgrid Items count using Javascipt.

1.Get the subgrid  using "Name" of subgrid as given below.
















2.Write the Javascript function as given below:

function subgridItemCount() {
 
      // Get the Subgrid Control
    var grid = Xrm.Page.ui.controls.get('LineItems')._control;
    if (grid.get_innerControl() == null) {
        setTimeout(subgridItemCount, 1000);
        return false;
    }
    else if (grid.get_innerControl()._element.innerText.search("Loading") != -1) {
        setTimeout(subgridItemCount, 1000);
        return false;
    }
    var countRec = grid.get_innerControl().get_allRecordIds().length;
    if (countRec > 0) {
        alert(countRec);
 
    }
 
}


For MS CRM 2013:

function FindControl() {
    if (document.getElementById('subgridName')!= null) {
        var count=document.getElementById('subgridName').control.get_totalRecordCount();
        alert(count);
        }
        else {
            setTimeout("FindControl()", 1000);
        }
    }


3.Result




























Hope this helps,

Regards,

Yusuf