SAP ByD – Reuse Libraries

SAP Business ByDesign

In this article we are going to discuss about Reuse Libraries.

 

What is Reuse Libraries?

A Reuse Library is the container for reuse functions, whereas the reuse functions provide the code and specify the parameters. You can create and define your own reuse libraries and reuse functions to organize complex code in your solution or to reuse your code in script files.

There are two types of Reuse Libraries:

  1. Custom Reuse Libraries
  2. SAP Reuse Libraries

lib

Custom Reuse Libraries

Your Reuse Libraries are called as “Custom Reuse Libraries”.

How to Create Custom Reuse Libraries?
  1. In the My Solutions tool window, open your solution.
  2. In the Solution Explorer, right-click your solution and select Add New Item.
  3. In the Add New Item dialog box, select Reuse Library and click Add.

    NewReuseLib

  4. A new item with the extension .library appears in the Solution Explorer.

 

How to Create Reuse Function?

In the studio, you can create a reuse function by adding it to an existing or new custom reuse library in your solution.

A Reuse Function is Created in Two main Steps
  1. First define the signature of the function by specifying the SAP Cloud parameters
  2. Then define the business logic in a separate script file that the system has automatically created for your Reuse Function.

[pullquote]    You cannot raise messages in a reuse function.[/pullquote]

Process
  1. In the document window of your Reuse Library, click Create Function.
    The Library Functions tool window opens.
    createfunction
  2. In the Create a Reuse Function step, enter a name for the reuse function.
    You can also select one of the following options:

    • Write-Enabled
      The default setting for reuse functions is that they can be used only to read business object data.
      If you enable this setting, a Reuse Function to modify data of business objects in a specific deployment unit.
    • Can Be Used in UI Designer
      If you want to use your Reuse Function in the UI designer, you must select this option.

      libraryfuncitonwizard

  3. Click Next.
  4. In the Define Parameters step, do the following to define an import parameter:
    • In the first line of the table, enter a name for the import parameter.
    • In the same line, under Type, select a data type or a business object node.
      If you have selected a business object node, the ElementsOf option is selected automatically, which
      means that the data structure of this node will be passed on by the reuse function.

      parameteradding
  5. Repeat the last step for each import parameter. You can also define one return parameter.
    You can click Move Up and Move Down to change the order of the parameters.
  6. Click Next.
  7. In the Review step, check that the information you have entered is correct and then click Finish.
  8. Save your reuse function.
    A new item with the file extension .absl appears in the Solution Explorer below the library file.

    abslfile

  9. Open the .absl file in the code editor to define the business logic of your reuse function.

 

SAP Reuse Libraries

The basic SAP reuse libraries of the scripting language extend the scope and functions that you use to implement your business logic in the studio.

These SAP Reuse Libraries provide basic functions that are used quite often, for example, to retrieve the current date or time for the current identity.

import ABSL;
var globalDateTime = GlobalDateTime.ParseFromString("2016–04–30");
var date = globalDateTime.ConvertToDate():

[pullquote] To use basic SAP Reuse Libraries, you need to import the namespace ABSL with the import keyword.[/pullquote]

List of Basic SAP Reuse Libraries
  1. Context
    Provides context information, for example, the current time in the time zone of the user or the identity UUID
    of the user that is currently logged on.

    Example:
     this.result = Context.GetCurrentGlobalDateTime();
     Result: Current date and time in UTC
    
  2. Duration
    Compares durations and performs simple calculations with durations.

    Example:
     this.duration1 = Duration.ParseFromString("P3X");
     this.duration2 = Duration.ParseFromString("P6X");
     this.result = this.duration1.GreaterEquals(this.duration2);
     Result: False
    
  3. Numeric
    Provides basic functions for integers and decimals.

    Example:
     this.result = Numeric.IsNumeric("17.4B");
     Result: False
    
  4. Numeric Character
    Converts a string into a numeric character.

    Example:
     this.numcCode.content = Numeric.ParseFromString("25");
     Result: Numeric character value 25
    
  5. Currency Conversion
    Provides functions for currency-based amounts. For example, you can convert an amount from one currency into another.

    Example:
     this.amount1.content = 25;
     this.amount1.currencyCode = "INR";
     this.amount2.content = 23;
     this.amount2.currencyCode = "INR";
     this.result = this.amount1.GreaterEquals(this.amount2);
     Result: False
    
  6. Quantity Conversion
    Compares quantities and performs simple calculations with quantities.

    Example:
     this.quantity1.Content = 45;
     this.quantity1.unitCode = "EA";
     this.quantity2.Content = 45;
     this.quantity2.unitCode = "EA";
     this.result = this.quantity1.GreaterEquals(this.quantity2);
     Result: True
    
  7. UUID
    Provides basic functions for UUIDs.

    Example:
     this.result.content = UUID.ParseFromString("00163E01023602ER45TYE2F34FC4C3F3");
     Result: UUID 00163E01023602DY7U9I92F34FC4C3F3
    
  8. Language Code
    Converts a string into a language code.

    Example:
     this.description.languageCode = LanguageCode.ParseFromString("EN");
     Result: Language code with content “E”.
    
  9. Date
    Compares dates, performs simple calculations with dates, and provides further useful date functions.

    Example:
     this.result = Date.Create(30,4,2016);
     Result: Date value 20160430
    
  10. Time
    Compares times and provides further useful time functions.

    Example:
     this.time1 = Time.ParseFromString("123456");
     this.time2 = Time.ParseFromString("654321");
     this.result = this.time1.GreaterEquals(this.time2);
     Result: False
    
  11. Date and Time
    Converts a string into a date and time value.

    Example:
     this.dateTime = DateTime.ParseFromString("2016-04-30T12:31:00Z");
     Result: Date and time value 2016-04-30T12:31:00Z
    
  12. Global Date and Time
    Compares global date and time values, performs simple calculations with global date and time values, and
    provides further useful global date and time functions.

    Example:
     this.gDateTime1 = GlobalDateTime.ParseFromString("20100215112345");
     this.gDateTime2 = GlobalDateTime.ParseFromString("20100402235432");
     this.result = this.gDateTime1.GreaterEquals(this.gDateTime2);
     Result: False
    
  13. Local Date and Time
    Converts local date and time values.

    Example:
     this.localDateTime = LocalDateTime.ParseFromString("20100215104352CET");
     this.result = this.LocalDateTime.ConvertTimeZone("UTC");
     Result: Local date time value 20100215094352 UTC
    
  14. Local Normalized Date and Time
    Converts local normalized date and time values.

    Example:
     var global2 = GlobalDateTime.ParseFromString("20080330120000");
     var locnorm = global2.ConvertToLocalNormalisedDateTime();
     var global3 = locnorm.ConvertToGlobalDateTime();
     Result: Global date time value 20080330120000
    
  15. Web Service Utilities
    Calls REST mashup Web services.

    Example:
     var result = WebServiceUtilities.ExecuteWebService(serviceID, parameters);
     var content = result.ResponseContent;
     var returnCode = result.ReturnCode;
    
  16. Binary
    Converts a string to a binary value.

    Example:
     this.Binary.Content = Binary.ParseFromBase64String("This is a Base64 string");
     Result: Binary value4E18AC8AC6816AC7BAE2CB6B8A78
    
  17. Number Range
    Draws numbers from a number range for different object types.

    Example:
     var recordNumber = NumberRange.DrawNumber("RECORD");
     var numberRangeObject : ID = "ORDER";
     var orderNumber = NumberRange.DrawNumber( numberRangeObject );
     Result: The value of the number range object RECORD and ORDER is the next number in sequence.
    

How to Use Reuse Function & SAP Function in our Script File

You call your Reuse Function in an action, an event in the same way you call an SAP Reuse Function.

In this example, the SAP Reuse Function Date.ParseFromString and the custom reuse function
CustomReuseLib.CalculateAge are used. To call the SAP reuse function, you must import the ABSL
namespace.

import ABSL;
var birthday = Date.ParseFromString("1975-05-10");
var age = CustomReuseLib.CalculateAge( birthday );

Today, we explored SAP Reuse Libraries. In our next article, we are going to discuss about Business Logic. Please leave your comment to improve this article.

If you want to learn more about SAP ByDesign, Don’t forget to subscribe here.