Step 4 – Creating another scenario

Now that we have one Enquiry scenario defined, we might want to add a second Sale scenario. It will reuse some of the previously defined scripting elements, but also introduce new ones.

We first need to define the new Sale scenario, and any new function mappings required. We’ve added the 2 new highlighted mappings below.

  • We need a new make_order mapping that we’ll use instead of make_request. This is because the customer’s request is going to be an actual order, not a request for portfolio information in this new Sale scenario.
  • We also need a new process_request mapping which will be the last step in our new Sale scenario defined by this tutorial (you’re welcome to keep extending it).
var make_request = { func: "Make Request", method: "Customer.MakeRequest" };
var make_order = { func: "Make Request", method: "Customer.MakeOrder" };
var receive_request = { func: "Receive Request", method: "Sales.ReceiveRequest" };
var send_product_portfolio = { func: "Send product portfolio", method: "PortfolioMgmt.SendProductPortfolio" };
var process_request = { func: "Process Request", method: "Admin.ProcessRequest" };

 

We then need to create the new Sale scenario using a combination of old and new mappings.

result["scenarios"] = [
{
name: "Enquiry",
description: "Customer inquires about products",
mapping: [
make_request,
receive_request,
send_product_portfolio
]
},
{
name: "Sale",
description: "Customer buys a product",
mapping: [
make_order,
receive_request,
process_request
]
}
];

A couple of callouts about the above new Sale scenario are:

  • It’s surrounded by curly braces, and note that there’s a comma separating the Enquiry scenario and Sale scenario. Just keep following this pattern to add as many scenarios as you like.
  • The Sale scenario reuses the receive_request mapping from the previous Enquiry scenario, however see below how we need to modify the Sales.ReceiveRequest method so that it handles both Enquiry and Sale scenarios.

 

We need to create a new MakeOrder function inside the existing Customer class. Notice below how the request_type property has been set to “Purchase” instead of “Portfolio“. We’ll check this value in our modified ReceiveRequest function below and it will determine where we direct the customer request to.

function MakeOrder(params) {

var result = {};

// define the customer request details
result["Request"] = {

// customer contact details
customer: {
name: "J.Smith",
phone: "555-5555"
},

// type of customer request
request_type: "Purchase"
};

// include a message to show in AIM as the scenario runs
result["Messages"] = [
{
msg: "Customer purchase request issued"
}
];

return result;
}

 

Now we need to modify the ReceiveRequest method, so that it can handle requests of type “Portfolio” and “Purchase“. The modified elements of the method are highlighted below.

 

class Sales {

function ReceiveRequest(params) {

var result = {};

// Extract 'Request' information from input params
var req = params["Request"];

if (req.request_type == "Portfolio") {

result["Request/Portfolio"] = {
customer: req.customer
};

result["Messages"] = [
{
msg: "Created portfolio request"
}
];

} else if (req.request_type == "Purchase") {

// We need to provide both 'Request/Insurance' information...
result["Request/Insurance"] = {
customer: req.customer,
policy: "BASIC"
};

// ...and 'Customer' information, as this is what is defined
// in the AIM model.
result["Customer"] = req.customer;

// We will incorporate the customer's name into the processing message
result["Messages"] = [
{
msg: req.customer.name + " ordered policy"
}
]
}

return result;
}
}

 

The last step is to define the Admin.ProcessRequest method.

class Admin {

function ProcessRequest(params) {

var result = {};

// Extract the insurance order from params
var cust_order = params["Request/Insurance"];

// Extract customer details from the order.
var cust = cust_order.customer;

// The policy type ordered is incorporated into the processing message
result["Messages"] = [
{
msg: "Ordering '" + cust_order.policy + "' policy"
}
]

return result;
}
}

 

After saving your .js files, you should be able to switch to AIM, click the Scenarios tab, and see both Enquiry and Sale scenarios.

Clicking play for each scenario should highlight what functions are in scope for each scenario, and then display your processing messages on the right side of the page as the scenario is executed.