CF Coding Practices
  • General Coding Practices
  • Debugging JSON-based CFC Methods
    • Valid JSON Formatting
    • Validating JSON
    • Final Hints
  • How We Invoke Modals
  • Submit Handlers
  • Useful SQL Snippets
  • jQuery/JS How-Tos And Hints
  • jQuery dataTables Tricks
  • CFSCRIPT Snippets and Hints
  • StatusPage Integration
  • Windows Server Setup
  • Common CF Formatting Commands
  • CF Snippets & Hints
  • Developing With The Mura Platform
    • ContentRenderer.CFC
    • Iterators
    • Iterating Remote Feeds
    • Components
    • Nested Content and Interactive Page Assembly
    • Modules and Display Objects
Powered by GitBook
On this page
  • Transaction Integrity Protection
  • Loops
  • Simple Loop
  • While Loop
  • Do / While Loop
  • For / In Loop
  • If / Then / Else
  • Switch Statement
  • Comments
  • Try / Catch / Throw

Was this helpful?

CFSCRIPT Snippets and Hints

Some useful cfscript snippets and syntax. Many of these originated from Pete Freitag's CFSCRIPT Cheat Sheet: https://www.petefreitag.com/cheatsheets/coldfusion/cfscript/

Transaction Integrity Protection

Instructs the database management system to treat multiple database operations as a single transaction. Provides database commit and rollback processing.

transaction {
	//do stuff
	if (good) {
		transaction action="commit";
	} else {
		transaction action="rollback";
	}
}

Loops

Simple Loop

for (i=1;i LTE ArrayLen(array);i=i+1) {
	WriteOutput(array[i]);
}

While Loop

x = 0;
while (x LT 5) {
	x = x + 1;
	WriteOutput(x);
}
//OUTPUTS 12345

Do / While Loop

x = 0;
do {
 x = x+1;
 WriteOutput(x);
} while (x LTE 0);
// OUTPUTS 1

For / In Loop

The for/in loop can be used to loop through queries, structures, or arrays

//Structure Loop
struct = StructNew();
struct.one = "1";
struct.two = "2";
for (key in struct) {
	WriteOutput(key);
}
//OUTPUTS onetwo

//Array Loop
cars = ["Ford","Dodge"];
for (car in cars) {
	WriteOutput(car);
}
//OUTPUTS FordDodge

//Query Loop
cars = queryNew("make,model",
    "varchar,varchar",
    [ {"make":"Dodge","model":"Charger"}, 
    {"make":"Buick","model":"Encore"} ]);
for (car in cars) writeOutput(car.make & " " & car.model & " ");
//outputs Dodge Charger Buick Encore

If / Then / Else

if (fruit IS "apple") {
	WriteOutput("I like apples");
}
else if (fruit IS "orange") {
	WriteOutput("I like oranges");
}
else {
	WriteOutput("I like fruit");
}

Switch Statement

switch(fruit) {
    case "apple":
         WriteOutput("I like Apples");
         break;
    case "orange":
         WriteOutput("I like Oranges");
         break;
    default: 
         WriteOutput("I like fruit");
}

NOTE: If your switch statement executes multiple case conditions, then you likely don't have a break command at the end of the case conditions. Don't forget to have break; as your last line for all case conditions (doesn't apply to the default condition).

Comments

Comments are done the same way a JavaScript: use // for single line comments and /* */ to encompass multi-line comments. Do not use CFML comment markers: <!--- --->

Try / Catch / Throw

try {
	//throw an error message that will be caught by the catch statement
	throw(message="Oops", detail="xyz"); 
} catch (any e) {
	WriteOutput("Error: " & e.message);
	rethrow; 
} finally { 
	WriteOutput("I always run (error or no error)");
}
PreviousjQuery dataTables TricksNextStatusPage Integration

Last updated 6 years ago

Was this helpful?