We've defined our business object and designed its screens in the previous two lessons. In this lesson, let’s finally bring the BO alive and implement some business logic. We'll implement a sample script for the defined action and the AfterModify event for our business object’s Root node.
Further information can be found in SAP Help.
Implementing the Account Lookup
Our business object allows the user to assign an account. We’ve already implemented the object value selectors (OVS) for this field, so that users can look up accounts with a convenient selection dialog.
Whenever we want to access the data of a related object, we need more than just the object’s ID. We need an object reference. That makes the fields of the respective object accessible in scripts and in the UI Designer when binding controls. Our business object definition contains associations that serve as object references. However, assigning the right object reference to an association is a task that we must take care of. The sample use case demonstrates how to use the AccountID to retrieve the respective object reference from the database and assign it to the ToAccount association.
Retrieving the object reference should happen as soon as the user enters an account ID, or selects an entry from the OVS. The AfterModify event is triggered after every change to a field and hence is perfectly suited for that job. However, we have to keep in mind that the AfterModify event is triggered after any field has been changed. There’s no specific AccountID-AfterModify event. Thus, we always need to evaluate the currently entered data and determine whether the changed field was the account ID.
In the following video we will:
- Create the scripts for the AfterModify event and the CalculateReportAmount action.
- Implement and explain the script for the AfterModify event to retrieve the account object reference.
- Modify the Thing Inspector to display the name of the account instead of its ID.
You can find the script shown in the video here:
/*
Add your SAP Business ByDesign scripting language implementation for:
Business Object: SimpleContract
Node: Root
Event: AfterModify
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 if:
- the value of any field in the node in which this script is contained is modified.
- the node itself is modified by another business object within the same solution.
*/
import ABSL;
import AP.FO.BusinessPartner.Global;
if (this.AccountID.IsInitial())
{
if (this.ToAccount.IsSet())
{
this.ToAccount.Reset();
}
}
else
{
if (!this.ToAccount.IsSet() || (this.ToAccount.IsSet() && this.AccountID != this.ToAccount.InternalID))
{
var rCustomer = Customer.Retrieve(this.AccountID);
if (rCustomer.IsSet())
{
this.ToAccount = rCustomer;
}
}
}
Our business object definition contains further associations for the contract owner and the product at the item level. Assigning object references to them isn’t a part of the demonstrations, but it works similarly to the script above.