Wednesday, November 27, 2013

Displaying multibyte characters or returning multibyte characters by AJAX call in ColdFusion

Sometimes we need to display a multibyte characters like Japanese or Chinese in our application or we have to build a application which can be converted into Japanese, Chinese or to some other non-English language.

If we are going to show any non-English language directly in our ColdFusion code then we will get some corrupted text.
Example: In below code I am simply trying to show a Japanese sentence.
<cfset x = "ユーザー名および/またはパスワードが正しくありません。もう一度やり直してください。" />
When I run above code I got following output in my browser.

Our browser is not displaying any Japanese character which we wants to display instead it's showing some other characters. How to display that?

It's very simple, just add "<cfprocessingdirective pageencoding="UTF-8">" at the start of the page like below:
<cfprocessingdirective pageencoding="UTF-8">
<cfset x = "ユーザー名および/またはパスワードが正しくありません。もう一度やり直してください。" />
When I run the code I got following output.

So, I am getting the desired output by using "cfprocessingdirective". It's always advisable to put this tag in onRequest method of Application.cfc in our application so that we don't have to place it in all cfm pages of our application.

Our cfm page now able to display multibyte characters in browser. What about AJAX calls? What will happen when we will make an AJAX call to a ColdFusion component which returns some multibyte characters?

Let's explore...
Here is my "testchar.cfm" code:
        <script type="text/javascript" src=""></script>
        $(document).ready(function() {

            //Making AJAX call to function returnChar present in component testchar.cfc which returns
            //the same Japanese string.
            $("#reloadC").click(function() {
                    type: "post",
                    url: './testChar.cfc?method=returnChar',
                    success: function(data) {
                    dataType: 'json'
        <h3>Display Multibyte Character</h3><hr/>
        <p>Press Refresh button to get multibyte characters from AJAX call</p>

        <!--- Press refresh button to get multibyte characters from AJAX call --->
        <input type="button" name="reloadC" id="reloadC" value="Refresh" />
        <div id="multi-char" style="width:500px"></div>
Here is our "testchar.cfc" code:
<cfcomponent output="true">
    <cffunction name="returnChar" access="remote" returnformat="JSON">
        <cfset x = "ユーザー名および/またはパスワードが正しくありません。もう一度やり直してください。" />
        <cfreturn x />
When I runs the testChar.cfm page and clicks the refresh button I gets following output.

So, again I am getting unwanted result. So, how to solve in ColdFusion component?
Here we have to place "cfprocessingdirective" tag after cfcomponent tag and before starting of any function like below.
<cfcomponent output="true">
<cfprocessingdirective pageEncoding = "UTF-8" suppressWhiteSpace = "yes">
    <cffunction name="returnChar" access="remote" returnformat="JSON">
        <cfset x = "ユーザー名および/またはパスワードが正しくありません。もう一度やり直してください。" />
        <cfreturn x />

No change is required in the "testChar.cfm" page.

When I runs the testChar.cfm again and clicks the refresh button then I gets following output.

Hope it may save your time!!!

Friday, November 15, 2013

Search Functionality in CFGRID

In my previous cfgrid posts we have covered following functionality of cfgrid:

1. Starting with CFGRID( part - 1 )
2. Starting with CFGRID( part - 2 )(Auto Refreshing CFGRID)
3. Export cfgrid Data or Table Data in Excel, Pdf and CSV Format(ColdFusion - 9)
4. Conditionally Change the Color Of a Cell Text In cfgrid

Today, we will see how we can implement searching functionality in cfgrid. Lets  see our cfgrid.cfm code below:

Now, see the cfgrid.cfc:

All the above code is self explanatory. Hope it will help people who are new in ColdFusion and trying to learn about cfgrid.

Happy Coding!!!  :)

Monday, November 11, 2013

Generate Report Using ColdFusion Report Template

In my following posts we learned up to creating a report template using ColdFusion report builder.

  1. Starting with Report Builder.
  2. Set up ColdFusion server in ColdFusion Report Builder.
  3. Creating a simple report template using ColdFusion Report Builder.
Now, it's time to use that report in our ColdFusion code.

Place the ".cfr" file in your project directory. See the following code to use the report template.

In the first section, I have written a SQL query for report then I am using the report template in cfreport tag to generate/display the report.

Here, if we will not use any query then it will display the report of the query which we had used during creating the report. If we are passing any query object to the cfreport tag then it will only consider the query object.

So, by using a customize queries we can generate multiple reports of the same type using ColdFusion reporting template. We can also pass parameter to a report. We will learn that in my future post.

Generated Report Screen Shot:

Creating a Tabular Report using ColdFusion Report Builder

In my previous two post we just learned how w just installed Report Builder and then connect Report Builder with our ColdFusion server.

If you want to see old posts please go from here #1(Report Builder installation) and #2(Set up Server in Report Builder

So, now it’s time to go for a simple example of ColdFusion reporting using Report Builder. Please follow the steps to generate a tabular report and use that in ColdFusion code.

 Click on File -> New in Report Builder, you will see a screen like below:

Go with “Report Creation Wizard” and click on “OK”.

You will see a next screen like below:
Click on “Query Builder” button.

Then we will see a new Query Builder screen for our report.
As shown in the picture, you can see all the available data sources and also you can write your own customize SQL query in the mentioned section. There is button “Test Query”. You can also test the query output by clicking that button. If your query has any problem then it will display error message.
After writing your SQL query just click on “Save” button.

 Then you will see another screen like below:

Here, you can select query columns which you want to in printable report.By using right side "Up" and "Down" arrow you can place column in a defined order. For now just forget it and click on “Next”.
·         We will see another screen as below:

Here, you can define any order by fields in your report. Let say I want to see first all arts which is not yet sold. By using the extreme right Up and Down arrow you can define the direction of the order by clause. Here, I want to see all sold items first so I made as descending order. It depends on your choice.

Click on “Next” to go to next screen.
·         Here is our next screen:
Just select the option as you see in screen and then click on “Next”. You will see another screen after that then click on “Next”.

·         You will see the next screen like below:
Here, in the text fields you have to put some descriptive name about your report. See the label and put the description text accordingly then click on “Finish”.

·         After, all these set up finally you see some different window like below:

In this screen we can customize our report header text and we can also format the display text in report. 

Let’s see how we can do that:
First click on #1: A new window will pop up like below
Here, I have used ColdFusion DateFormat() to formatting the displaying date.

Click on #2: A new pop up window will be displayed:
Here, I just changed column header from “ISSOLD” to “Sold”. So similarly we can change all the column header with some descriptive name.

Click on #3: A new pop up window will appear like below:
Here, we just used another ColdFusion function for display formatting of a field value. Similarly, you can apply your own logic on other fields if you want.

I have changed the column header and added display formatting function for header and cell values. Finally we will have the following screen:
Here, field names are changed and we can also see the display formatting function in applied area then  click on preview button to see the preview of the report. Then save the report. It will generate a “.cfr” file.

Next, how we will use generated template in our ColdFusion code. Go here...

Server Set up in ColdFusion 10 Report Builder

When we first run Report Builder then we will see screen for server set up otherwise by clicking on
File - > New, we will see a screen like below.

Here we have to select "Server Setup Wizard" as highlighted then click "OK" it will take you to the server set up screen(as shown below).

Click "Next" in above screen then you will see another screen as shown below.

In this screen, you can see "Measurement Units" drop down. It shows different scale, you have to select one which you want to use in your report. Then click "Next". You will see another screen like below.

In above screen, we have to provide server details to which want to connect.
  • Description: Any Name for identifying that server.
  • Host Name: IP/Server Name
  • Port: Port no on which your coldfusion is running. For me it's 8500
  • Use SSL check box, content Root, RDS Security(User Name), if it is required for you.
  • Password: Provide RDS password which you have set up in ColdFusion Admin.
After all these values click on "Test Connection" to see if you are able to connect to server or not. Then you will see a screen like below.

Here everything is fine for me. If you are facing any problem please check again the values you are providing and also check whether RDS is allowed in the target server and if the server is running or not. Then close the screen and click on "OK" button. You will see below screen.

Here in above screen you can see in CF Server label "CF10 Local" is selected which we just set up. Also, you can see two other fields,
  1. Local Webroot: Web root of the server which you have selected.
  2. Website Webroot: Exact URL root to browse any file on that server.

You can follow the instructions and the example I am showing you for reference. Then Click on "Next" button, you will see a screen like below.

So, all your set up is done just click on "Finish" to complete the process or "Previsous" to change anything.

We have linked a server to the Report Builder then what's next???
Let's try a simple report template with Report Builder here. Go here....

Starting With ColdFusion Report Builder

I think most of you have heard something about report builder in different tools like SQL server report building tool and other report building tools for different languages.

In ColdFusion, we also have a report building tool called ColdFusion Report Builder. So, lets explore about it.

What is a Report Building ?
Report building means to represent some information in graphical way ex:- different kinds of chart or tabular format.
I will cover basic things of ColdFusion Report building. I have divided the report building topic into following sections:

1. Installing ColdFusion 10 Report Builder:

- You can download ColdFusion 10 Report Builder from following URL:
- Double click on the exe file and follow the instructions to install.
- In Windows 8/Windows 2012 server you may see following error message:

Installer user interface mode not supported

Please follow below mentioned steps to avoid this error.

- Right click on the installer then you will see a screen like below.

- Select “Troubleshoot Compatibility” then you will see a trouble detecting screen and after few seconds you will see following screen.

- From the above screen select “Try recommended settings”. Then you will see following next screen.

In above screen you have to click on “Test the program” then it will be installed successfully. 

Next, after installation set up server in Report Builder. Go here to know details....