# Method

## Overview

A **Method** is a way of adding custom logic to entities. It can also be used to handle table/entity events. It can be created directly in the entity file from *"methods"* tab or through *"events"* (table, forms, etc.).\
Entity scopes are the foundset scopes to give a foundset extra functionality. They should be used if there are business rules that are based on table/data, really belong to that datasource, so it is just a narrower scope then what named global scope would be.

## File Structure

The code for a Method is stored in a script file in a [Solution](https://docs.servoy.com/reference/servoy-developer/project-file-structure/workspace-folder/solution-folder) directory -> "datasources" directory -> "table-name" directory. The file has the same name as the table followed by "\_entity", with a `.js` extension. For example, the `orders` table would have the methods file `orders_entity.js`.\
Nesting file path: "solution/datasources/server-name/table-name/table-name\_entity.js"

## Properties Summary

The following properties can be set for Method:

| Property          | Summary                                                 |
| ----------------- | ------------------------------------------------------- |
| [Name](#name)     | The name of the method                                  |
| [Source](#source) | This is the JavaScipt function that defines the method. |

## Properties Details

### Name

The name of the method. It is also the same name as the function.

### Source

This is the JavaScript function that defines the method. Other data providers from this table, related data sources and scope variables can be used.

**Example**

Let's consider *"administration"* table, part of *"exampleData"* database server and a method defined in table entity scope:

```javascript
/**
 * @properties={typeid:24,uuid:"A2F01DCA-0BFB-4045-AF3C-9472064C5D5F"}
 */
function getDefaultAdministration()
{
	if (find()) {
		is_default = 1;

		if (search()) {
			return getRecord(1);
		}
	}
	
	return null;
}
```

The method can be called in solution or forms' scopes, **always related to the same foundset the method is defined for**: "datasources.db.server-name-table-name.getFoundset().EntityMethodName()"

```javascript
	var adminRecord = datasources.db.exampleData.administration.getFoundset().getCurrentAdministration();
```

The method can also be linked to a [table event](https://docs.servoy.com/reference/servoycore/object-model/database-server/table/..#events-details) or [form event](https://docs.servoy.com/reference/servoycore/object-model/database-server/table/method).
