Skip to main content
Solved

Using SQL functions in calculation fields

  • September 21, 2021
  • 2 replies
  • 282 views

Tejo van de Bor
Captain
Forum|alt.badge.img+5

hi, 

I’m trying to add some calculated fields in a table, and found the calculation field and the specific possibilities for each type.

The calculation is rather complex and can be used in multiple tables, so I want to make one function with parameters. But I can’t find examples using SQL functions in a calculation field? Like how to create a control procedure (templated) to create and maintain an SQL function in SF, and using it in a calculation field.  I can't find any in the training course, documentation or the community info. Does anybody have a simple example?

It is probably a n00b question, but hey, I am ;-)

Best answer by Erwin Ekkel

Let me explain below how to make function using an expression column. Please note that function's and expression columns can slow down performance. Depending on the calculation and the size of the table/amount of data shown on screen it might be better to create a view or have a hardcoded value with the result of the calculation (depending on how static the input for the calculation is). 

First you create a subroutine of type function (scalar for a single result set, table if you want to return a set of values based on the input). Add subroutine parameter(s) as input for the function. 

Here is a simple example for a scalar function which returns an integer, with 1 parameter for the input:
 

 

 

After creating the subroutine add functionality for the function code and assign it to the function (code group functions). Here is an example for a code that adds a value of 2 to the input variable and returns the value of the calculation. 

 

Next in data model add an expression column and to execute the function your code could look like this if column ID was the input.

 

 

View original
Did this topic help you find an answer to your question?
This topic has been closed for comments

2 replies

Forum|alt.badge.img+17
  • Moderator
  • 761 replies
  • Answer
  • September 23, 2021

Let me explain below how to make function using an expression column. Please note that function's and expression columns can slow down performance. Depending on the calculation and the size of the table/amount of data shown on screen it might be better to create a view or have a hardcoded value with the result of the calculation (depending on how static the input for the calculation is). 

First you create a subroutine of type function (scalar for a single result set, table if you want to return a set of values based on the input). Add subroutine parameter(s) as input for the function. 

Here is a simple example for a scalar function which returns an integer, with 1 parameter for the input:
 

 

 

After creating the subroutine add functionality for the function code and assign it to the function (code group functions). Here is an example for a code that adds a value of 2 to the input variable and returns the value of the calculation. 

 

Next in data model add an expression column and to execute the function your code could look like this if column ID was the input.

 

 


Tejo van de Bor
Captain
Forum|alt.badge.img+5

ah, subroutines! I was betting on control procedures.. Now I see there is a reference in the documentation, but your example is beter :slight_smile:  Thank you! 

 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings