Whatever message this page gives is out now! Go check it out!

Sample SMS application

Last update:
Jun 9, 2026
The following CFC implements a simple employee phone directory lookup application. The user sends a message containing some part of the name being looked up (a space requests all names). The onIncomingMessage response depends on the number matches.
  • If no match exists, the onIncomingMessage function returns a message indicating that no matches exist.
  • If one match exists, the function returns the name, department, and phone number.
  • If up to ten matches exist, the function returns a list of the names preceded by a number that the user can enter to get the detailed information.
  • If over ten matches exist, the function returns a list of only the first ten names. A more complex application could let the user get multiple lists of messages to provide access to all names.
  • If the user enters a number, and previously got a multiple-match list, the application returns the information for the name that corresponds to the number.

    The following listing shows the CFC code:
<cfcomponent> 
 <cffunction name="onIncomingMessage"> 
 <cfargument name="CFEvent" type="struct" required="YES"> 
 <!--- Remove any extra white space from the message. ---> 
 <cfset message =Trim(arguments.CFEvent.data.MESSAGE)> 
 <!--- If the message is numeric, a previous search probably returned a 
 list of names. Get the name to search for from the name list stored in 
 the Session scope. ---> 
 <cfif isNumeric(message)> 
 <cfscript> 
 if (structKeyExists(session.users, val(message))) { 
 message = session.users[val(message)]; 
 } 
 </cfscript> 
 </cfif> 
 
 <!--- Search the database for the requested name. ---> 
 <cfquery name="employees" datasource="cfdocexamples"> 
 select FirstName, LastName, Department, Phone 
 from Employees 
 where 0 = 0 
 <!--- A space indicates the user entered a first and last name. ---> 
 <cfif listlen(message, " ") eq 2> 
 and FirstName like '#listFirst(message, " ")#%' 
 and LastName like '#listlast(message, " ")#%' 
 <!--- No space: the user entered a first or a last name. ---> 
 <cfelse> 
 and (FirstName like '#listFirst(message, " ")#%' 
 or LastName like '#listFirst(message, " ")#%') 
 </cfif> 
 </cfquery> 
 
 <!--- Generate andreturn the message.---> 
 <cfscript> 
 returnVal = structNew(); 
 returnVal.command = "submit"; 
 returnVal.sourceAddress = arguments.CFEVENT.gatewayid; 
 returnVal.destAddress = arguments.CFEVENT.originatorid; 
 
 //No records were found. 
 if (employees.recordCount eq 0) { 
 returnVal.shortMessage = "No records found for '#message#'"; 
 } 
 //One record was found. 
 else if (employees.recordCount eq 1) { 
 // Whitespace in the message text results in bad formatting, 
 // so the source cannot be indented. 
 returnVal.shortMessage = "Requested information: 

#employees.firstName# #employees.lastName# 

#employees.Department# 

#employees.Phone#"; 
 } 
 //Multiple possibilities were found. 
 else if (employees.recordCount gt 1) { 
 //If more than ten were found, return only the first ten. 
 if (employees.recordCount gt 10) 
 { 
 returnVal.shortMessage = "First 10 of #employees.recordCount# records"; 
 }else{ 
 returnVal.shortMessage = "Records found: #employees.recordCount#"; 
 } 
 // The session.users structure contains the found names. 
 // The record key is a number that is also returned in front of the 
 // name in the message. 
 session.users = structNew(); 
 for(i=1; i lte min(10, employees.recordCount); i=i+1) 
 { 
 // These two lines are formatted to prevent extra white space. 
 returnVal.shortMessage = returnVal.shortMessage & " 

#i# - #employees.firstName[i]# #employees.lastName[i]#"; 
 // The following two lines must be a single line in the source 
 session.users[i]="#employees.firstName[i]# #employees.lastName[i]#"; 
 } 
 } 
 return returnVal; 
 </cfscript> 
 </cffunction> 

</cfcomponent>

Share this page

Was this page helpful?
We're glad. Tell us how this page helped.
We're sorry. Can you tell us what didn't work for you?
Thank you for your feedback. Your response will help improve this page.

On this page