Posts

Showing posts with the label ColdFusion

How to remove null from Coldfusion Lists

Below is a code example to remove null values from a list in coldfusion. <cfset exlist = ",,4,5,6,,7"> <cfdump var="#exlist#"> <cfset arr = ListToArray(exlist)> <cfset exlist1 = arraytolist(arr)><br> <cfdump var="#exlist1#"> 

How to get only the number from a alphanumeric String in ColdFusion

 If the input contains numbers and characters, and we need to get only the numeric part from that, we can use reReplace Function in Coldfusion like this: <cfset test = "rtf4545"> <cfset result = reReplace(trim(test),"[^0-9\.]+","","All")> <cfoutput>#result#</cfoutput> Output: 4545

how to use coldfusion variable in javascript | assigning coldfusion variable to a javascript variable

 There are some cases where we need coldfusion variable inside javascript. We may need to assign the javascript variable with coldfusion variable value. For this, we can use the script tag, as we use in HTML and can assign like given below: Please note that the entire JS code have to be inside <cfoutput> block. <cfset a="10"> <cfoutput>     <script type="text/javascript">         var test = '#a#';         alert(test);     </script> </cfoutput>

ColdFusion Interview questions 2021 - Part 1

Image
  1. What is ColdFusion. How it works? ColdFusion is a powerful and fast development platform built on java for web application development. For more detailed explanation,  you can refer the video below: 2. Difference between Application.cfc and Applicaion.cfm Every time a page runs, ColdFusion looks for the Application.cfm page  in the current directory and then in each parent directory up to the web root until it finds one. As soon as it finds an Application.cfm page, it runs it and stops looking. ColdFusion looks for the Application.cfc, page in the same manner as it does for Application.cfm. Below video shows the main difference between Application.cfm and Application.cfc   3. What are components in ColdFusion? Components are nothing but a class with methods / functions and variables in it. In ColdFusion the tag <cfcomponent> is used for creating components. The functions inside a component can be created by using <cffunction> tag. The syntax for creating a component

How to display comma separated query column values in coldfusion

 Suppose if you have a query which returns some names of person. For example, if you have a table called person which have a column called fist_name and last_name. if you have to display each first_name and last_name comma separated you can use the following code. We can have the query like this: <cfquery name="getperson" datasource="datasourcename">     select first_name,last_name from person </cfquery> so for displaying this result , we can have a code like this: <cfoutput query="#getperson#">      #trim(getperson.first_name)# #trim(getperson.last_name)#<cfif                getperson.currentrow LT getperson.recordCount>, </cfif>  </cfoutput>

how to arrange given list in a particular order in coldfusion

 Consider we have a list as given below: <cfset list1= "firstname, lastname, middlename, address ,email" /> and if we need this list to be ordered as <cfset order_list = "2,4,3,1,5" /> where order_list is the order for list1 to be arranged. Let us try to arrange the List1 in the given order of Order_list. <cfset list1= "first_name, last_name, middle_name, badge_name,email" /> <cfset order_list = "2,4,3,1,5" /> <cfoutput> New List Order : #order_list# <br> </cfoutput> <!---Arrange the values in list one with the order specified in list 2. ---> <cfset newlist1 = "" /> <cfloop list = "#order_list#" index="i"> <cfset newlist1 = listAppend(newlist1, listGetAt(list1, i, ',')) /> </cfloop> <br/> <cfoutput> First order is : #newlist1# <br></cfoutput>

ColdFusion Issue: Resource interpreted as stylesheet but transferred with MIME type text/html

Image
Got an issue after setting up the local site, images are not displaying properly in the UI. The reason is the style sheets are interpreted as HTML doc.  These are steps to fix the same. Start>Run>appwiz.cpl>Click Turn Windows Feature on >Check static content under Common HTTP features

ColdFusion code to get current Domain

Below simple function can be used to find the current domain, by calling this wherever required. <cffunction name="GetCurrentDomain" access="public" output="false" returntype="string"        hint="Get Current Domain">                  <cfset domain="" />          <cfset proto ="http" />          <cfif CompareNoCase(CGI.HTTPS , 'on') EQ 0>               <cfset proto = "https" />          </cfif>          <cfset domain = proto&'://'&CGI.server_name&'/' />         <cfreturn domain /> /cffunction>

onblur infinite alert issue fix

Issue: Infinte alert pop up when you use onblur event. This issue have been monitored in Safari, IE. Root Cause: Each time clicking on 'Ok' button of the alert, onblur happens and we are trapped in an infinite loop. Fix: You can refocus the control to different element in the form to get rid off an infinite alert pop up due to onblur  event usage in your application Sample code given below <script type="text/javascript"> function validate() { if(document.getElementById('txtval').value != " ") { alert("not valid value"); document.getElementById('txtint').focus(); } } </script> <form> <input type="text" id="txtval"  name="txtval" onblur="validate();"> <input type="text" name="txtint" id="txtint" > </form> Note:I faced this issue in Safari browser here, but i remember same issue i have been

Pagination with next and previous links in ColdFusion

The following code gives pagination output with 'next' and 'previous' links. <cfparam name="FirstRow" default="1"> <cfparam name="DispRecords" default="5"> <cfquery name="GetParks" datasource="cfdocexamples" >      SELECT PARKNAME, REGION, STATE      FROM Parks </cfquery> <cfset EndRow=FirstRow + DispRecords> <cfif FirstRow + DispRecords GREATER THAN GetParks.RecordCount>     <cfset EndRow=999> <cfelse>     <cfset EndRow=DispRecords> </cfif> <table border="1">    <tr>       <th>NO.</th>       <th>PARKNAME</th>       <th>REGION </th>       <th>STATE</th>          </tr> <cfoutput query="GetParks" startrow="#FirstRow#" maxrows="#EndRow#">  <tr>       <td>#CurrentRow#</td>  

ColdFusion code for pagination using numbers

We can do pagination of results in many ways. Here the following code gives the pagination output using numbers with ColdFusion. <cfparam name="URL.PageId" default="0"> <cfset RecordsPerPage = 5> <cfquery name="GetParks" datasource="cfdocexamples" >      SELECT PARKNAME, REGION, STATE      FROM Parks </cfquery> <cfset TotalPages = (GetParks.Recordcount/RecordsPerPage)-1> <cfset StartRow = (URL.PageId*RecordsPerPage)+1> <cfset EndRow = StartRow+RecordsPerPage-1> <cfoutput> <table border="1">    <tr>       <th>No.</th>       <th>PARKNAME</th>       <th>REGION </th>       <th>STATE</th>    </tr>    <cfloop query="GetParks">   <cfif CurrentRow gte StartRow >      <tr>         <td>#CurrentRow#</td>         <td>#PARKNAME#</td>         <td&

Advantages of using hidden fields in ColdFusion

Hidden form fields can be used in a good way for avoiding the spambots.  Spambots  are automated computer programs designed to assist in the sending of spam. If we have a hidden field in the form, user cannot fill the hidden field, but the spambots can do fill the hidden fields. So for avoiding this, we can have a hidden field say, <cfinput type="hidden" name="spm_inputcheck" value=" "> and we can have a condition like: <cfif form.spm_inputcheck is " ">     perform the actions.. <cfelse>     abort </cfif>

Fixing Cyrillic Character issue in coldfusion

For fixing cyrillic character issues, we can use the ways given below: way 1:  Keep <cfprocessingdirective pageenc oding = "utf-8"> line so that issue will get resolved. For example, <cfcomponent displayname="testCyrillic" hint="Replace special characters"> <!---To Fix Cyrillic character issue : starts---> <cfprocessingdirective pageencoding = "utf-8"> Way 2:  Create jdbc datasource and add use Unicode=yes&charactorEncoding=UTF-8 with JDBC URL. Driver class as usual:  com.mysql.jdbc.Driver For example, jdbc:mysql:// localhost:3306/mySite? useUnicode=yes& charactorEncoding=UTF-8

How to create password protected PDF documents in coldfusion

For creating password protected PDF documents, we can use the tag <cfdocument> with attributes userpassword,encryption . For example, the below code will create a PDF document which is protected by a password 'password'. <cfdocument format="PDF"  userpassword="password" encryption="128-bit">     <cfdocumentsection>         This document is password protected     </cfdocumentsection> </cfdocument>

how to find the execution time for partcular code blocks in ColdFusion

For finding the execution time of particular code blocks, we can use the ColdFusion function GetTickCount().  Without enabling the debugging information in the Administrator, we ca use the metho d GetTickCount() for finding the time used for executing a partcul ar code block. For example,  <cfset t1 = GetTickCount()> code block begin s.. ------------------------------------- code bloc k ends. <cfset  t2= GetTickCount()> <cfset t otal=t2-t1> <cfoutput> Time take n for execution was #total#</cfoutput >    

ColdFusion code to find the datasources in the server

<cfscript> adminObj = createObject("component","cfide.adminapi.administrator"); createObject("component","cfide.adminapi.administrator").login("CF admin password"); myObj = createObject("component","cfide.adminapi.datasource"); </cfscript> <cfdump var="#myObj.getDatasources()#">

Coldfusion code to get the mappings in the server

To find the mappings in the server, we can use the following code.     <cfset  mappings = StructNew()>     <cfset ServiceFactory = createObject("java","coldfusion.server.ServiceFactory")>         <cfset mappings = ServiceFactory.runtimeService.getMappings()>        <cfdump var="#mappings#">

Blank space validation in ColdFusion cfinput

For validating a required input field for blank space, we can use the attribute 'Validate="noblanks"  in the cfinput tag. For example, <cfform name="test" method="post" action="" > Name:<cfinput type="text" name="firstname"  required="yes" validate="noblanks"  message="Please Enter Your firstname"> <cfinput type=" submit" name="submit" value="Submit"> </cfform>

Remove non ASCII characters in coldfusion

The following code will remove the non ascii or illegal characters from a string. <cfset str=" Please enter string "> <cfset a=reReplace(str, "[^\x20-\x7E]", "", "ALL")> <cfoutput>#a#</cfoutput>

Coldfusion code to get multiple mail attachments using cfimap

Below given code will loop through multiple attachments of the mail and will copy the attachments to particular folder. <cfimap action="open" connection="Conn" server="serverURL" username="useremail" password="password" secure="yes" port="portvalue"> <cfimap action="getall" connection="Conn" name="Query_getAttachments" folder="Inbox" attachmentpath="#GetTempDirectory()#" > <cfquery dbtype="query" name="getMailAttachments">       select *       from Query_getAttachments       where seen=<cfqueryparam value="no" cfsqltype="cf_sql_varchar">       and ATTACHMENTS is not null </cfquery> <cfdump var="#getMailAttachments#"> <cfloop query="getMailAttachments">       <cfif getMailAttachments.ATTACHMENTS NEQ ''>         <cfloop list="#ge