This blog post focuses on the SAP Cloud for Customer Rules and Validations.
Ruby script is used to write rules, which is made simple by a set of operators and functions. It help make fields visible, read-only, mandatory, change color and to assign default values.
We’ll see examples for each of the functions and operations here.
To write a rule or validation do the following,
- Enter into Adaptation Mode
- Go to Implementation option by clicking on Back arrow till last.
- Then select Implementation and Rules and Validation
Rule Writing Screen:
- Left side you can find all the fields that are available in the current business object.
- Center you can write your code.
- Right side you can find operation, functions, colors that can use in your code.
We have two types of rule,
Syntax for all the Functions:
You can also use ‘1’ for true and ‘0’ for false
1. IF() -> Evaluates the logical expression, if the outcome is true, return the first value, else return the other value
Syntax: IF(logical_expressions, value_if_true, value_if_false)
Example: IF(Root.Status == ‘01’, true, false)
2. AND() -> all the condition should be true. Output is a Boolean value; TRUE if all values are true, and FALSE if one or more values are false.
Syntax: AND(logical_expression1, logical_expression2, … )
Example: IF(AND(Root.Status == ‘01’, Root.Source == ‘02’), true, false)
3. OR() -> any one condition should be true. Output is a Boolean value; TRUE if any expression is true, FALSE if all expressions are false.
Syntax: OR(logical_expression1, logical_expression2, …)
Example: IF(OR(Root.Status == ‘01’, Root.Source == ‘02’), true, false)
4. NOT() -> NOT reverse the truth value of its operand. Output is FALSE for TRUE and TRUE for FALSE.
Example: IF(NOT(Root.Status == ‘01’) , true, false)
5. CASE() -> Returns result 1 if condition 1 is satisfied, returns result 2 if condition 2 is satisfied, and so on
Syntax: CASE(logical_expression1, result_1, logical_expression2, result2 … )
Example: CASE(Root.Status == ‘01’, true, Root.Status == ‘02’, false, Root.Status == ‘03’, 1, Root.Status == ‘04’, 0)
Date and Time:
1. DAY() -> Returns the day component of a date (if the date is October 28, 2016, the system returns 28).
- IF(DAY(Root. StartDate) == DAY(Root.LastChangeDate), true, false)
- IF(DAY(Root.StartDate) == 9, false, true)
2. MONTH() -> Returns the month component of a date in number (if the date is October 28, 2016, the system returns 10).
- IF(MONTH(Root.StartDate) == MONTH(Root.LastChangeDate), true, false)
- IF(MONTH(Root.StartDate) == 6, true, false)
3. NOW() -> Returns the current date and time in the format 2022-06-09T09:08:26.676Z.
4. TODAY() -> Returns the current date in your time zone.
Example: IF(Root.CreationDateTime == TODAY(), true, false)
5. WEEKDAY() -> Returns the day of the week in number(1=Sunday, 2 =Monday, and so on).
Example: IF(WEEKDAY(TODAY()) == 1, true, false)
6. YEAR() -> Returns the year component of a date (if the date is October 28, 2016, the system returns 2016).
- IF(YEAR(Root.CreationDateTime) == 2022, true, false)
- IF(YEAR(TODAY()) == 2022, true, false)
1. BOOL() -> Converts a character to a Boolean value; output is TRUE if there is a value, FALSE otherwise.
Example: IF(BOOL(Root.BusinessPartnerCreatedIndicator), true, false)
(refer this screenshot for below text function examples)
2. CONTAINS() -> Evaluates two arguments of text and the output is a Boolean value; TRUE if the second text contains the first text; if not, the output is FALSE.
Syntax: CONTAINS(‘find_text’, within_text)
Example: IF(CONTAINS(‘is’, Root.Note), true, false)
3. FIND() -> Returns the position of the first occurrence of the text within the text. Position value starts with index 0.
Syntax: FIND(‘find_text’, within_text)
Example: IF(FIND(‘i’, Root.Note) == 2, true, false)
4. LEN() -> Output is the number of characters in a specified text string.
Example: IF(LEN(Root.Note) == 19, true, false)
5. TEXT() -> Converts a value to a text.
6. TOLOWER() -> Converts a text string to lowercase.
7. TOUPPER() -> Converts a text string to uppercase.
8. TRIM() -> Removes the spaces and tabs from the beginning and end of a text string.
9. SUBTEXT() -> Retrieves part of a main text based on offset and length.
1. ISNUMBER() -> Evaluates if a text value is a number; output is TRUE if the text is a number, else it is FALSE.
2. ISBLANK() -> Evaluates if an expression has a value; the output is TRUE if it is blank; the output is FALSE, if it contains a value.
1. CLIENTTYPE() -> Returns FIORI when the client type is Fiori client, and HTML5 when it is HTML5.
2. DEVICETYPE () -> Returns DESKTOP, TABLET, or PHONE depending on the device type used.
3. ISOFFLINE() -> Returns TRUE if the solution is offline, and FALSE when connected.
4. MYUSERROLES() -> Returns one or more business role IDs assigned to the current logged-in user
Syntax: MYUSERROLES()==’Business Role ID’
Example: IF(MYUSERROLES()==’admin’), true, false)
5. GETUSERNAME() -> Retrieves the user’s name of the current logged in user.
Font Color Rules:
C4C support six pre-defined colors to change the look and feel of your user interface.
Syntax: IF(logical_expressions, color_if_true, color_if_false)
Example: IF(Root.Status == ‘01’, ‘GOOD_DARK’, ‘BAD_DARK’)
Default Value Rules:
This rule works only in the Quick Create screen of any business objects.
When working with default values, there are a few things to keep in mind.
- Value Type rules are applicable only on Quick Create screens
- The return value must be enclosed in single quotes
- Unless you wish to choose the return value from a different field, in such case you’ll just choose that field in the rule without single quotes.
To write a default rule, first we have the change the rule type as Value like below,
Syntax: IF(logical_expressions, ‘default_value’, ‘blank’)
Example: IF(Root.UseExistingAccount, ‘This lead is created using existing account’, ‘ ‘)
If existing account is selected, then in Note field it will display the “This lead is created using existing account”.
We have to assign this to the field where we want to display the default value.
Select the field in Adaptation Mode, select Rule under Set Default Value. It will display the default rule list that you have created. Choose it and apply it.
C4C has one more cool feature that is called Validations.
We have three level of Severity.
Error -> it shows a message with error symbol and wont allow us to save.
Warning -> it shows a message with warning symbol and we can save.
Information -> it simply display some message with information symbol.
Validation will work only with the existing rule. So, before creating a validation, a rule is must.
Example: If lead source is Referral, then we should display a warning message.
Below is my rule,
Now create validation like below,
If you need more information on this, refer SAP Help Portal.
Note: All of the above are just examples. You won’t be able to improve at rule writing unless you give it a try.
I hope that this blog post has given you some insight into SAP C4C rules and validation. Kindly like and follow to get more detailed blogs.
See You Soon👋🏻,