Skip to content

Commit ca4f8b1

Browse files
committed
initial examples set
0 parents  commit ca4f8b1

6 files changed

Lines changed: 177 additions & 0 deletions
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## User Business Rules
2+
3+
### Add default lines on sales order creation
4+
5+
**Category:** Automation
6+
7+
**Description:**
8+
When a new sales order is created, automatically add default sales order lines if a specific condition is met (e.g., customer is a VIP).
9+
10+
**Repository / Event / Condition:**
11+
- **Repository:** `Crm.Sales.SalesOrders`
12+
- **Event:** `CREATENEW`
13+
- **Condition:** *Optional* (fully handled in script)
14+
15+
**Script:**
16+
```js
17+
// Automatically add default lines for VIP customers
18+
if (subject.Customer && subject.Customer.CustomerType.Id == "SPECIFIC-CUSTOMER-TYPE-ID-HERE") {
19+
// Add a default product line
20+
var defaultProduct = Domain.General.Products.ProductsRepository.getById("YOUR-PRODUCT-ID-HERE");
21+
if (defaultProduct) {
22+
var newLine = Domain.Crm.Sales.SalesOrderLinesRepository.createNew();
23+
newLine.SalesOrder = subject;
24+
newLine.Product = defaultProduct;
25+
newLine.Quantity = new Domain.Quantity(1, defaultProduct.MeasurementUnit);
26+
newLine.UnitPrice = 100; // Set default price if needed
27+
}
28+
}
29+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## User Business Rules
2+
3+
### Notify user when sales order exceeds threshold
4+
5+
**Category:** Automation
6+
7+
**Description:**
8+
Calculate the total value of a sales order by summing the `LineAmount.Value` of all its lines.
9+
10+
If the total exceeds a threshold, take action (e.g., notify or escalate).
11+
12+
**Repository / Event / Condition:**
13+
- **Repository:** `Crm.Sales.SalesOrders`
14+
- **Event:** `COMMIT`
15+
- **Condition:** *Optional* (fully handled in script)
16+
17+
**Script:**
18+
```js
19+
var threshold = 50000;
20+
var total = 0;
21+
if (subject.Lines && subject.Lines.Count > 0) {
22+
for (let i = 0; i < subject.Lines.Count; i++) {
23+
total += line[i].LineAmount.Value;
24+
}
25+
}
26+
27+
if (total > 50000) {
28+
Action.notify.user(
29+
<THE USER ID TO NOTIFY>,
30+
"Order " + subject.DocumentNumber + " exceeds the approval threshold (" + total + ")."
31+
);
32+
}
33+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## User Business Rules
2+
3+
### Credit limit override for cash payments
4+
5+
**Category:** Validation / Automation
6+
7+
**Description:**
8+
Automatically set the 'Credit Limit Override' field on a sales order when the payment type's system type is "In Cash" (`SystemType = 0`).
9+
This replaces previous logic that relied on calculated attributes and complex business rule setup.
10+
11+
**Repository / Event / Condition:**
12+
- **Repository:** `Crm.Sales.SalesOrders`
13+
- **Event:** Change of State (`RELEASING`)
14+
- **Condition:** *(Optional, handled in script)*
15+
16+
**Script:**
17+
```js
18+
// Check if the sales order payment type is 'In Cash' (SystemType = 0)
19+
if (subject.PaymentType && subject.PaymentType.SystemType === 0) {
20+
subject.CreditLimitOverride = true;
21+
}
22+
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## User Business Rules
2+
3+
### Validate customer credit status via external service
4+
5+
**Category:** Integration / Validation
6+
7+
**Description:**
8+
Before allowing a sales order to be committed, the script calls an external credit service (via HTTP GET) to check the customer's credit status.
9+
10+
If the service response is not `"APPROVED"`, the commit is cancelled.
11+
12+
**Repository / Event / Condition:**
13+
- **Repository:** `Crm.Sales.SalesOrders`
14+
- **Event:** `COMMIT`
15+
- **Condition:** *Optional* (fully handled in script)
16+
17+
**Script:**
18+
```js
19+
// Build the external API URL, including the customer's unique code
20+
var url = "https://credit.example.com/api/check?customerCode=" + encodeURIComponent(subject.Customer.Number);
21+
22+
// Optionally add authorization or other headers
23+
var headers = "Authorization: Bearer YOUR_TOKEN_HERE";
24+
25+
// Call the external service
26+
var response = Action.http.get(url, headers);
27+
28+
// Assume the service returns a JSON object, e.g. { "status": "APPROVED" }
29+
var creditStatus = JSON.parse(response).status;
30+
31+
if (creditStatus !== "APPROVED") {
32+
Action.cancel("Customer credit status is not approved. The order cannot be committed.");
33+
}
34+
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## User Business Rules
2+
3+
### Validate customer VAT number with VIES
4+
5+
**Category:** Integration / Validation
6+
7+
**Description:**
8+
On sales order commit, validate the customer’s VAT number using the official EU VIES REST API (POST request).
9+
10+
If the VAT number is not valid, the operation is cancelled.
11+
12+
**Repository / Event / Condition:**
13+
- **Repository:** `Crm.Sales.SalesOrders`
14+
- **Event:** `COMMIT`
15+
- **Condition:** *Optional* (fully handled in script)
16+
17+
**Script:**
18+
```js
19+
if (subject.Customer && subject.Customer.Party && subject.Customer.Party.Country) {
20+
var url = "https://ec.europa.eu/taxation_customs/vies/rest-api/check-vat-number";
21+
var body = JSON.stringify({
22+
countryCode: subject.Customer.Party.Country.CountryCode,
23+
vatNumber: subject.Customer.Party.PartyUniqueNumber,
24+
requesterMemberStateCode: "BG",
25+
requesterNumber: "XXXXX"
26+
});
27+
var headers = "Content-Type: application/json";
28+
29+
// POST request to VIES
30+
var response = Action.http.post(url, body, headers);
31+
32+
// Parse response (expected: { countryCode, valid, ... })
33+
var viesResult = JSON.parse(response);
34+
35+
if (!viesResult.valid) {
36+
Action.cancel("Customer VAT number is not valid according to VIES.");
37+
}
38+
}
39+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## User Business Rules
2+
3+
### Whole Quantity Validation
4+
5+
**Category:** Validation
6+
7+
**Description:**
8+
Ensure that quantities in sales order lines are always whole numbers.
9+
10+
**Repository / Event / Condition:**
11+
- **Repository:** `Crm.Sales.SalesOrderLines`
12+
- **Event:** `COMMIT`
13+
- **Condition:** None, handled in script
14+
15+
**Script:**
16+
```js
17+
// Cancel the operation if the quantity is not a whole number
18+
if (subject.Quantity && subject.Quantity.Value % 1 !== 0) {
19+
Action.cancel("You have entered a decimal number as a quantity. Please check the data entered in the sales order lines and try again!");
20+
```

0 commit comments

Comments
 (0)