Running Rules

  • Updated

To utilize your Rule Application, include it in your web (or other JavaScript) application.

Sample Web Application

In this sample, the user enters the origin and destination ZIP codes for a shipment, along with the weight of the order. The ShippingCost Rule Application uses this information, along with the name of the carrier (Pigeon Air Freight) to calculate the shipping cost of the order. After the rules have been applied, the shipping cost is displayed back to the user.

Use this line to include your InRule JavaScript file:

<script src='ShippingCost.min.js'></script> 

Use the following JavaScript® snippet to read elements to an InRule Entity and apply the rules:
var shipRecord = {};
shipRecord.OriginZipCode = document.getElementById('originZip').value;
shipRecord.DestinationZipCode =
shipRecord.Carrier = 'Pigeon Air Freight';
shipRecord.Weight = document.getElementById('totalWeight').value;
var session = inrule.createRuleSession();
// Create the ShippingRecord entity
var shipRecordEntity = session.createEntity('ShippingRecord', shipRecord);
// Run rules
      //iterate over log.runtimeErrors array to see the errors.
       document.getElementById('shippingCost').value = shipRecord.ShippingCost;

The snippet performs these steps:

  • Creates a shipRecord object from controls on the form
  • Creates an InRule Entity from the shipRecord object
  • Uses the applyRules function to run the Rule Application
  • Places the calculated shipping cost in a form control

Note: Actions taken by the rules are applied directly to the object passed into the JavaScript rules engine. The benefit of this is that there is no extra work needed to obtain your results because the object itself has been updated. For examples of leveraging this model with various frameworks, see the Samples section.

If an object is not passed in, one will be created by the engine for you. After applying rules, you may retrieve the entity using the getValue method (e.g. shipRecordEntity.getValue();).

Note: See the API Reference section for more information on the InRule for JavaScript SDK.

Was this article helpful?

0 out of 0 found this helpful



Please sign in to leave a comment.