## Add Calculation Logic to WynReports Using User or Custom Functions

One of the commonly used features in Wyn Report is Expressions. It allows users to perform many types of calculations using a set of pre-defined functions. These functions are preset formulas that help to perform mathematical, statistical, financial, time, lookup, and logical operations.

WynReports support many built-in functions; however, chances are that you want to perform industry-specific or proprietary processing calculations in your reports. For instance, a conversion logic to convert the finance numbers from one form (or unit) to another, age calculation, information generation such as a resource URL, data cleaning like unifying names, “U.S.”, “USA” to “United States”, replacing words like “Car” for “Auto”, and many more.

For them, Wyn Enterprise 6.0 introduces the ability to create User functions (or Custom functions) for reusable code or calculations. These functions can be used in the WynReports like any other native function such as SUM, LOOKUP, COUNT, etc.

In this article, we’ll briefly describe this feature and explore the steps to add them to Wyn using the example of an Insurance Claim Payout report. In this report, we want to fetch the highest claim payouts for different age groups, as shown below:

The dataset used in this report contains information on insurers’ age but not on their Age-group. This is a commonly used metric while designing reports in an insurance company, and thus we need to define a logic to calculate the age group that can be reused across other reports.

#### User (or Custom) Functions

Custom functions, defined in C# or Visual Basic (VB), are user-defined calculation logic processed on input(s) values to generate an output. The input can be a constant, data from a data field, or a result of an expression using built-in functions. The output can be a single value, an array of values, a list, or a table.

A function in its simpler form can be depicted using the diagram below:

They work like the standard built-in functions like SUM, AVERAGE, or LOOKUP in a report. The primary difference lies in the fact that the logic is written by you for your business need.

#### Benefits of User Functions

User functions serve amazing advantages when it comes to saving time and reusing calculation logic. It promotes the reusability of the logic, increases consistency, and reduces redundancy by ensuring that the same business logic is used everywhere in a report or across reports.

When it comes to changing the logic, either in part or whole, there is only one place to make that change, instead of individual expression or logic added in the report. Thus, reducing the extra cost in the maintenance of the calculation logic.

They abstract your complex calculation and make the expression easy to read and understand.

With custom functions, the logic remains secured with the administrator and is not exposed to the report users, thus ensuring that the company’s proprietary formulas are well secured.

Administrators can add user functions designed by the developers from the USER FUNCTIONS page on the Administrator Portal.

It provides you an interface to add C# or VB code. You can generate the code template on this interface and type your code manually in the editor, or upload a .cs file. The custom function’s code must follow the standard C# or VB rules for writing a method.

A typical function template looks like the below, containing XML comments and the function definition:

XML comments are used to fill in the details in the INFO section of the Expression editor, as shown below:

While the function contains the logic of the operation to be performed, it takes one or more arguments and returns a single value or an array of values as required.

Visit the documentation page to learn more about the syntax and rules of the custom function’s definition.

For this blog, we’ve created a function named AgeGroup that takes in the Age of the insurer as an argument and returns the group it may belong to, as shown below:

Once the function logic is there, hit the Compile button to check for any compilation error. If there are no errors, hit the Save Changes button to save the custom function.

#### Use the Custom function in the Wyn Report

Once a user function is successfully compiled and saved on the admin portal, they are available in Wyn Reports under the USER FUNCTIONS node in the Expression Editor, as shown below:

The syntax to add a user function in the report is {Code.<Function Name>()}. It can be added manually or using the Expression Editor for the report item where you intend to use it.

In our sample report, we want to use this expression in the Table’s group, thus adding the custom function {Code.AgeGroup(Age)} to the Group expression and the text box in the group row, as shown below:

The function calculates the age group for each record in the dataset, and when grouped in the table, the data appears in the preview below:

#### Conclusion

User functions can be flexible and powerful enough to be used for simple logic, as the above example to a complex one like calculating the Amortization Schedule in the Finance industry, Production Calculations in the Manufacturing industry, Premium calculations for Insurance, and many more. Use them to build a reusable calculation logic and make your reporting process faster.

Paarisha Rana

As a software engineer, Paarisha enjoys GrapeCity's environment of encouragement and learning. She enjoys reading, aerobics, travelling, and exploring historical places. Paarisha graduated from Uttar Pradesh Technical University (UPTU) at Lucknow (India) with a Bachelor's of Technology in Information Technology, and you can find her on LinkedIn.