> For the complete documentation index, see [llms.txt](https://help.santesuite.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.santesuite.org/developers/applets/recipes/assigning-a-home-facility.md).

# Assigning a Home Facility

When using the Disconnected Gateway, dCDR Android or dCDR Windows/Linux applications, you can optionally append the "home facility" or "registering facility" to the patient being registered.&#x20;

This pattern works for assigning any relationships you like (for example, automatically assigning birthplace on a birth notification).

### Create a new Business Rules File

Create a new JavaScript file in the **rules/** folder of your applet / plugin for SanteMPI. The basic structure of this file should be:

```javascript
/// <reference path="../.ref/js/santedb-bre.js" />
/// <reference path="../.ref/js/santedb-model.js" />
/// <reference path="../.ref/js/santedb.js" />

/**
 * Elbonia MPI / SanteMPI Rules for Assigning Facility
 * --
 */

```

### Write Assignment Code

Next, you'll want to write some code that looks up the configured facility where the dCDR is running and appends that information to the patient if not already done so.

```javascript
/**
 * Business rule - Append facility on registration
 */
function appendRegisteringFacility(patient) {

    // Only run when not running in master server
    if (SanteDBBre.Environment != ExecutionEnvironment.Server) {
        
        // Prepare relationship
        try {
            if (!patient.relationship)
                patient.relationship = {};

            // Get the configured facilities
            var facility = SanteDBDcg.GetFacilities().resource[0];
          
            // Assign the facility as the SDL
            patient.relationship.ServiceDeliveryLocation = new EntityRelationship({
                target: facility.id
            });
        }
        catch (e) {
            console.error("Error assigning facility: " + e);
        }
    }

    return patient;
};
```

### Attach the Business Rule

Finally, add the business rule to the BeforeInsert method on the patient.

```javascript
SanteDBBre.AddBusinessRule("elb.rule.home_fac", "Patient", "BeforeInsert", { "deceasedDate" : "null"}, appendRegisteringFacility);
```


---

# 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, and the optional `goal` query parameter:

```
GET https://help.santesuite.org/developers/applets/recipes/assigning-a-home-facility.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
