> For the complete documentation index, see [llms.txt](https://docs.obto.co/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.obto.co/sofos/order-management/server-scripts/order.md).

# Order

## How to create a Server Script

A Server Script is a reusable server side function or class which can called inside an API Route.

In order to create a Server Script, follow these steps: -&#x20;

1. On navigation, under Scripts, go to Server Script. You will see a list of all the Server Scripts.&#x20;
2. Click on ‘settings icon’ on top left corner and then click ‘new’.&#x20;
3. A form appears where you have to provide the following details: -&#x20;
4. Name - name of the Server Script.&#x20;
5. Active – Check / Uncheck if ready / not ready to used.&#x20;
6. Script – The code (Business Logic Class).&#x20;
7. Click on ‘create’ button. That’s it, you have created the Server Script.&#x20;

Below is the Orders server script. In this example we are using mongoose ORM library for database operations. This server script creates and initiates the Orders Schema.

```javascript
const mongoose = ob.mongoose;
const { Schema, model, SchemaTypes } = mongoose;
const { String, Boolean, Date, Number } = SchemaTypes;

const statusSchema = new mongoose.Schema({
  state: String,
  update_on: Date
});

const orderLineSchema = new mongoose.Schema({
  sku: String,
  description: String,
  quantity: Number,
  price: Number
});

const initiate = () => {
  const OrderSchema = new Schema({
    sequence: String,
    status: [statusSchema],
    orderLines: [orderLineSchema],
    transactionDate: Date,
    customerName: String,
    customerMobile: String,
    location: String,
    totalAmount: Number,
    created_on: Date,
    updated_on: Date,
    created_by: String,
    updated_by: String,
    domain: String
  });

  try {
    // ob.log(mongoose.modelNames());
    mongoose.connection.deleteModel("om_order");
  } catch (e) {}

  OrderSchema.pre("save", function(next) {
    const doc = this;
    ob.log("XXXXXXXXXXXX" + JSON.stringify(doc));
    doc.created_on = new Date();
    doc.updated_on = new Date();
    ob.sequence("om_orders", doc.domain, seq => {
      doc.sequence = seq;
      next();
    });
  });

  return model("om_order", OrderSchema);
};

module.exports.Order = initiate();
```
