Skip to Content
Technical Articles
Author's profile photo Abimanyu G

Rules and Validations in SAP C4C

Hello everyone!

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,

  1. Enter into Adaptation Mode
  2. Go to Implementation option by clicking on Back arrow till last.
  3. Then select Implementation and Rules and Validation

Rule Writing Screen:

  1. Left side you can find all the fields that are available in the current business object.
  2. Center you can write your code.
  3. Right side you can find operation, functions, colors that can use in your code.

Rule Type:

We have two types of rule,

  1. Property
  2. Value

Operation:

Functions:


Syntax for all the Functions:

You can also use ‘1’ for true and ‘0’ for false

Logical Operator:

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.

Syntax: NOT(logical_expression)

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).

Example:

    • 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).

Example:

    • 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).

Example:

    • IF(YEAR(Root.CreationDateTime) == 2022, true, false)
    • IF(YEAR(TODAY()) == 2022, true, false)

Text:

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.

Syntax: LEN(text)

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.


Value Information:

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.


Special Function:

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.


You can combine two or more function in single rule. That is totally depends on the requirement.


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.


Validations:

C4C has one more cool feature that is called Validations.

We have three level of Severity.

  1. Error -> it shows a message with error symbol and wont allow us to save.
  2. Warning -> it shows a message with warning symbol and we can save.
  3. 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,


Change Logs:

Refer the Comment link to get more detail about Change logs.


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👋🏻,

Abimanyu G

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Teresa Sanchez
      Teresa Sanchez

      Very useful post! Thank you for the information

      I would like to ask something related, I am trugling with a rule for fields that should be read only according some conditions (for example for no prospect accounts) however if the user has ADMIN business role should be able to modify (for this MYUSERROLES). I tried several rules with OR without success. Could you give me any tip please?

      Thank you!

      Teresa

      Author's profile photo Abimanyu G
      Abimanyu G
      Blog Post Author

      Hey Teresa,

      Your condition is, if account role is not prospect and user role is admin, then they can able to edit the field. Else no one allowed to edit the field.

      If above condition is right then you can use any of the below rule,

      IF(AND(Root.Role != "BUP002", MYUSERROLES() == "Admin_Business_Role_ID"), true, false)

      BUP002 is a code value for Prospect.

      If the Role field in the Account does not equal Prospect and the current login user role is ADMIN, then this rule will triggered


      IF(AND(NOT(Root.ProspectIndicator), MYUSERROLES() == "Admin_Business_Role_ID"), true, false)

      If the Prospect Indicator in the account is false and the current login user role is ADMIN, then this rule will triggered

       

      Kindly confirm on the comment if it is working and share this blog with your connections.😊

       

      Thanks,

      Abimanyu G

      Author's profile photo Teresa Sanchez
      Teresa Sanchez

      Hi Abimanyu G,

      Thank you so much for the information provided. It has been very helpful.

      Finally I used the second Rule but I did some update for my case (when the account is not a prospect, some fields should be read only, however those fields should be editable if the user has business role Admin)

      IF(AND(NOT(Root.ProspectIndicator), MYUSERROLES() != "Admin_Business_Role_ID"), true, false)

       

      Hope this can help more people!

      Thank you,

      Teresa

      Author's profile photo Brad Vukich
      Brad Vukich

      Great post Abimanyu G! I appreciate the thoroughness in your description of function parameters, as well as the font color option codes.

      I have a question about tracking changes to rules. When I modify a rule, does the change get logged in history anywhere? My team is looking to gain more clarity as to when changes were made and if we're able to determine the logic before and after the change.

      Thanks

      - Brad

      Author's profile photo Abimanyu G
      Abimanyu G
      Blog Post Author
      Hi Brad,
      Yes it is. Every time we modify a rules, the Changes log will get updated in the below,

      1. Changes tab in Adaptation mode. we can see that changes in details by clicking the (right and left) arrow icon.

      2. Administrator WC -> General Setting WCV -> Layout Change History (Filter the Type column with AddBusinessLogic.

      And if you open any one in the list, it will display like below.

      And in top right we have Delete option to delete the logic. It will delete the changes in the particular field.

      Note: But in both the case, we can only see the change done by whom, where and its description. We can't the see the rule code.

      Kindly confirm if this reply clarifies your question.

      Thanks,

      Abimanyu G