In this lesson, we will implement our XBO’s business logic to perform the custom validation and inform the user about the result.
Implementing business logic for an extension object works the same as for a custom business object: We first have to create the file for the script stub and then we can write the scripting logic (or in our case copy and paste the script from the course material). Finally, the script needs to be saved and activated.
For our scenario we will work with two scripts:
- The action that we have defined in the XBO definition: CheckReviewNeeded. It contains the actual check logic for verifying zero-price items. In the next lesson, we'll implement a button to call this action manually.
- Additionally, we'll create a script stub for the BeforeSave event of the sales quote and call the action from there, allowing the check logic to be carried out automatically before the quote gets saved.
In the script creation selection dialog, you will see other events with Validation in their names and you may ask: "Our logic sounds like a validation logic! Wouldn’t it make sense to use such an event for it?" With validation events, you can check whether an object can be saved or not. That means you can block the user from saving the object. We don't want that. Furthermore, we want to set the Review Needed indicator during the check. However, changing the content of fields is not allowed from within validation events.
You can find more details in the help pages for:
The following video guides you through the script creation. A detailed explanation on what the scripts do, follows after the script snippets.
If you have access to an SAP Sales/Service Cloud tenant and want to follow along with the demonstrations, you can use the following code snippets for the business logic creation. Please keep in mind to uncheck Mass Enabled when creating the scripts.
Action: CheckReviewNeeded
1234567891011121314151617181920212223242526272829303132333435363738394041/*
Add your SAP Business ByDesign scripting language implementation for:
Business Object: CustomerQuote
Node: Root
Action: CheckReviewNeeded
Note:
- To access the elements of the business object node,
use path expressions, for example, this.<element name>.
- To use code completion, press CTRL+J.
*/
import ABSL;
var TARGET_QUOTE_TYPE = "AG";
// by default we expect that everything is ok; if at least one item has 0 price, the review will be necessary
this.ReviewNeeded = false;
// if the quote has a different document type, do nothing
if (this.ProcessingTypeCode != TARGET_QUOTE_TYPE)
return;
foreach(var item in this.Item)
{
var priceNode = item.PriceDocumentPriceAndTaxCalculationItem;
if (!priceNode.IsSet() || !priceNode.ItemMainPrice.IsSet() || priceNode.ItemMainPrice.Rate.IsInitial())
{
// missing price throws standard error message "Mandatory List Price Missing"
continue;
}
if (priceNode.ItemMainPrice.Rate.DecimalValue == 0)
{
// show the message in case the value is 0
this.ReviewNeeded = true;
raise PriceMissing.Create("W", item.ID);
}
}
Event: BeforeSafe
123456789101112131415161718/*
Add your SAP Business ByDesign scripting language implementation for:
Business Object: CustomerQuote
Node: Root
Event: BeforeSave
Note:
- To access the elements of the business object node,
use path expressions, for example, this.<element name>.
- To use code completion, press CTRL+J.
- The solution uses this script when:
- the instance of the business object is being saved.
- the instance of the business object is created from other sources like web services, preview screen, and so on.
*/
import ABSL;
this.CheckReviewNeeded();
This video showcased how the scripts are created and populated with the coding from the script snippets. The following simulation explains the implemented logic in detail. To start it, select Watch Demo below.