> For the complete documentation index, see [llms.txt](https://docs.cffoundry.technology/cf-coding-practices/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cffoundry.technology/cf-coding-practices/cfscript-snippets-and-hints.md).

# CFSCRIPT Snippets and Hints

## 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");
}
```

{% hint style="warning" %}
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).
{% endhint %}

## 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: `<!--- --->`&#x20;

## 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)");
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cffoundry.technology/cf-coding-practices/cfscript-snippets-and-hints.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
