CRM and CX Blogs by Members
Find insights on SAP customer relationship management and customer experience products in blog posts from community members. Post your own perspective today!
cancel
Showing results for 
Search instead for 
Did you mean: 
abimanyu_g
Participant

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:

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

Rule Type:

We have two types of rule,

  1. Property
  2. Value

Operation:

Functions:


Syntax for all the Functions:

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.

You can also use '1' for true and '0' for false


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.

  1. Value Type rules are applicable only on Quick Create screens
  2. The return value must be enclosed in single quotes
  3. 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,


Note: All of the above are just examples. You won't be able to improve at rule writing unless you give it a try.


Change Logs:

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


My Other Blogs:


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

27 Comments
teresasanchez9
Explorer
0 Kudos
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
abimanyu_g
Participant
0 Kudos
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
teresasanchez9
Explorer
Hi abimanyu,

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
bradvukich
Explorer
0 Kudos
Great post abimanyu! 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
abimanyu_g
Participant
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
0 Kudos
Hey abimanyu,

 

great article!

 

  • Do you know if it possible do define a rule based on the warranty end date of a registered product? (If the warranty end date is in the history than field XY should be in another color.)


 

I cant find this attribute in the selection.

 

Many thanks in advance.

 

BR

Marc
bradvukich
Explorer
Thank you for the details Abimanyu, it's exactly what I wanted to know.

-Brad
abimanyu_g
Participant
0 Kudos

Hey mrogga322

Sorry. Please explain the question once more. A screenshot will helpful.

 

Tina_Bednar
Advisor
Advisor
0 Kudos
Hello abimanyu

thank you for your blog post. Its wonderfull!

Do you know by chance if there are date calculations also possible linke now()+7days =?

It is possible on UI Flied calculations and also Workflows formula, but I was not able to do it in UI Rule Editor.

 

Thank you & br.

Tina
abimanyu_g
Participant
0 Kudos
Hello Martin

Sorry for the delayed response.

In KUT adaptations, this kind of rules is not possible. As you mentioned, it can be done by using workflow field update.

Best Regards,

Abimanyu G.
0 Kudos
Hi Abimanyu G:

Hope you are doing well. Any chance you can help me with this one?

I need to write a rule to avoid edition of specific fields dependind on the status of a ticket.

I've tried the following 5 combinations with no success so far:

1. OR(Root.ServiceRequestUserLifeCycleStatusCode ('ZJ'),Root.ServiceRequestUserLifeCycleStatusCode!=('Z8'))

2.OR(Root.ServiceRequestUserLifeCycleStatusCode!='ZJ',Root.ServiceRequestUserLifeCycleStatusCode!='Z8')

3.OR(Root.ServiceRequestUserLifeCycleStatusCode!=TEXT('ZJ'),Root.ServiceRequestUserLifeCycleStatusCode!=TEXT('Z8'))

4.Root.ServiceRequestUserLifeCycleStatusCode!=OR('ZJ','Z8')

5.IF(OR(Root.ServiceRequestUserLifeCycleStatusCode!=TEXT('ZJ'),Root.ServiceRequestUserLifeCycleStatusCode!=TEXT('Z8')), true, false)

The objective is to have those fields editable only if service request is on either of those status.

Thank you and best regards.

Miguel V
abimanyu_g
Participant
0 Kudos

Hey,

I hope  your requirement is "When Ticket status is not equal to ZJ or Z8, you want to make some field Read-only".

Please use the below,

IF(OR(Root.ServiceRequestUserLifeCycleStatusCode != "ZJ", Root.ServiceRequestUserLifeCycleStatusCode != "Z8"), true, false)

Apply this rule is Set as Read-Only option.

Best Regards,

Abimanyu G.

Ricardo_Vargas
Explorer
0 Kudos
Hi Abimany G:

Great post and thank you so much for your help. I haven't been able to test it out because i'm facing an issue (reported to SAP). System is not allowing me to remove rules from KUT fields. In the meantime, i was hoping you can help me with one more question. From the rule at the last question how can I add an exception to the same rule in which Admin users can edit the field at every ticket status?

Would it be something like this?

IF(OR(AND(Root.ServiceRequestUserLifeCycleStatusCode != "ZJ", MYUSERROLES()!="ADMIN_SISTEMA"),AND(Root.ServiceRequestUserLifeCycleStatusCode != "Z8", MYUSERROLES()!="ADMIN_SISTEMA")), true, false)

Again, thank you so much for your help.

Ricardo V.
abimanyu_g
Participant
0 Kudos
Hey rvargas1985,

Your requirement is The field should be read only, when status is "Z8" or "ZJ". But ADMIN can edit that field any time.

Check this please,
IF(AND(MYUSERROLES()!="ADMIN_SISTEMA", OR(Root.ServiceRequestUserLifeCycleStatusCode != "ZJ", Root.ServiceRequestUserLifeCycleStatusCode != "Z8")),true, false)

Regards,

Abimanyu G.
0 Kudos
Hi Abimanyu,

 

i have a question to this topic.

 

I need a validation rule for readonly but not only for one Userrole.

 

I should check that some fields are readonly if account role is not prospect and User role are one of 5 different roles. But for all other the field should be editable.

We have round about 40 roles and only round about for 6 should get the field readonly.

 

I have checked several options but it doesnt work if i test with the role it looks ok but if i checked another role then the field is also readonly.

For this roles and account not prospect should the field readonly. For all other editable.

Z_CRM_USER_CN
Z_INSIDE_SALES_CN
Z_MANAGEMENT_CN
Z_RSM_MANAGER_CN
Z_SALES_ENGINEER_CN
Z_SALES_MANAGER_CN

 

I have checked some of conditions f.example this one

IF (AND(NOT(Root.ProspectIndicator), OR( MYUSERROLES() == "Z_CRM_USER_CN", MYUSERROLES() == "Z_SALES_ENGINEER_CN", MYUSERROLES() == "Z_INSIDE_SALES_CN", MYUSERROLES() == "Z_MANAGEMENT_CN", MYUSERROLES() == "Z_RSM_MANAGER_CN", MYUSERROLES() == "Z_SALES_MANAGER_CN")), true, false)


Hopefully you can help me.

 

Best regards

Alex

 
0 Kudos
Hey Abimanyu,

 

I was able to find the correct variable by using the debug mode.

 

Thank you.

BR

Marc
efrainantonio_pacheco
Participant
Few people writes useful blog like this, thank you for sharing
abimanyu_g
Participant
0 Kudos
Thanks for sharing your thought, epacheco50.
luisfilipepena
Explorer
Thanks for this post! Helped me get what I needed. Good examples and complete!
abimanyu_g
Participant
0 Kudos
Thanks luisfilipepena 👏🏻
kelie
Discoverer
0 Kudos
Hello,

I love the blog!  Great Job!  I do have a question.  I am trying to write a layout rule that will look at the created on date and time and when the current date and time is <= 2 min make another field read only. 

For Example:

 

Have you done anything like this in the past?  Any help would be greatly appreciated.

Thanks,

Kelie
kelie
Discoverer
Hi Great Blog!

I am trying to write a layout rule that will make a field available if 2 min has past.

For example:

A quote is created at 10:00 and I have a process in the background that needs to run before a field can become available.  So at 10:02 or after they can access the field.

Have you done this before?  If so could you share an example of a rule?

Thanks,

Kelie
abimanyu_g
Participant
Hi Kelie,

I guess with only the KUT rule we can't achieve this. But I have a workaround. Please find below.

1. Create a KUT indicator field,

2. Create a Workflow like below,


workflow event details



workflow action details



workflow field update


3. Activate the Workflow rule,

4. Now create a KUT rule in Adaptation Mode like below,
IF(your_indicator == true, true, false)

 

How will it work,

1. When a quote is created, automatically the above workflow will get triggered after two minutes based on the Created-On date field and it will update your_indicator field to true.

2. Once the indicator is set to true, by using above KUT adaptation rule, automatically the field will become editable or visible.

I hope this helps you. Kindly confirm your result.

 

Thank you,

Abimanyu G.
gminot
Explorer
0 Kudos
Hello Abimanyu

 

Excellent article very detailed but in the rules I do not see the possibility of adding value . The field is not present, is there a configuration before use?

 


rule value


Thank you for your answer

 

Gilles
gminot
Explorer
0 Kudos
sorry but i saw my mistake. This concerns only the quick access screens

THANKS

 

gilles
sathishg
Advisor
Advisor
0 Kudos
Hi Abimanyu,

 

Very useful post. I have a requirement where we need to force user to enter comments in a field whenever they change the 'Assigned To' field value to a value different than current value. For this I can create an extension field to add comments and make it visible and mandatory by a rule but how do I set the condition on the rule to track change in field value of 'Assigned To'.

Thanks for your help in advance.

Regards

Sathish
karola_wittkamp
Newcomer
0 Kudos
Hello Abimanyu G,

thank you for the great blog post.

I have the following question: For a customer, the action for deleting a contact relationship (overview of the contact persons of a customer) should only be displayed if the contact has already become obsolete. Can this be controlled by a rule?

Thank you and kind regards,

Karola