Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Not able to compare custom Rich Text value with default value using behavior

Chaya May 30, 2024

I am using behavior to set up an error on a rich text custom field if it is not updated. When I try to compare using ==, it is not working. I have a default table added on a rich text field. I want to show error message if the field is not changed/updated.

I have below server side script:

def customRichTextField = getFieldById(fieldChanged)
def customRichTextFieldValue = customRichTextField.value.toString()
def customRichTextFieldDefaultValue = "||header1||header2||header3||header4||header5|| | | | | | |"

if(customRichTextFieldDefaultValue == customRichTextFieldValue){
    customRichTextField.setError("Please update custom rich text field")
}else{
    customRichTextField.clearError()
}
When I tried to print the default value on console, it gives me the exact same value that I am using as default string. When I try to alert customRichTextFieldValue, it shows the exact value as default value when it is not changed. But still comparison is not working.

2 answers

0 votes
Radek Dostál
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 31, 2024

This generally won't work as is.

Tables need end of line - otherwise, you're only defining the header. Everything in that string is a header, there is no row.

 

Plus you're dealing with a text editor so there will be some "extra" unexpected characters between what you get in a getter and what you see in UI.

 

Create that table in some test issue, then open the issue with

/rest/api/2/issue/ABC-5?fields=description 

and you will see the "real" text that should work in your string comparison. Do note though there might be some unicode, for me I see this:

"description": "||header 1||header 2||\r\n|​x|​y|"

There is a non-printable character hidden that you might or might not see in your browser:

"description": "||header 1||header 2||\r\n|\u200b​x|\u200b​y|"

 

Lastly, you can sorta debug this using

log.warn("myVar1: " + someTextValue1)
log.warn("myVar2: " + someTextValue2)

Though again since it is a wiki editor I'm not sure it will print unicode control characters. You could try to invalidate the characters by doing

log.warn("myVar1: " + someTextValue1.replace("\\", ""))

 to remove the backslash so that it prints the unicode (it is in the string, just that it might not be printed in logs/html, so this might help).

 

Other than that I think what you have seems fine logic-wise, it's just the comparison and differences in seen and compared values.

Chaya May 31, 2024

I tried to get the value from Rest API, and there was \r\n as you have suggested. However, using the rest api string is also not working.

My table looks like beow:

Capture.PNG

and I got response from rest api: 

"||Header1||Header2||Header3||Header4||Header5||\r\n| | | | | |"
0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
May 31, 2024

Hi Chaya,

From your code, there are a few things to check.

Firstly, could you please add a logging parameter to see what is actually returned by the customRichTextFieldValue variable, i.e.

log.warn "=============>>>> ${customRichTextFieldValue}"

Next, when you initialise the customRichTextFieldDefaultValue, you set the format to a GString and not a String when using double quotes.

Please modify it to:-

def customRichTextFieldDefaultValue = "||header1||header2||header3||header4||header5|| | | | | | |".toString()

so you will get it in a String format.

Please try to modify your code as shown below and see if there is any difference:-

def customRichTextField = getFieldById(fieldChanged)
def customRichTextFieldValue = customRichTextField.value.toString()
def customRichTextFieldDefaultValue = "||header1||header2||header3||header4||header5|| | | | | | |".toString()
 
log.warn "=============>>>> ${customRichTextFieldValue}"

if(customRichTextFieldDefaultValue == customRichTextFieldValue){

    customRichTextField.setError("Please update custom rich text field")

}else{

    customRichTextField.clearError()

}

I am looking forward to your feedback.

Thank you and Kind regards,
Ram

 

 

Chaya May 31, 2024

Hi Ram,

With log.warn I got response like: ||Header1||Header2||Header3||Header4||Header5|| | | | | | |

When I add the same value as default value, it is not working while comparing.

I also tried .toString() method for customRichTextFieldDefaultValue, but it is not working.  

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
June 3, 2024

Hi @Chaya,

From what you are saying, I assume that you have used the Behaviour Initialiser to create the Table Template for the Multi-Line Text Field you are using.

And your objective is to return a validation error if no update has been made to the table in that field.

You could try something like this:-

For the Behaviour Initialiser:-

def multiLine = getFieldByName('Multi-Line')
def tableTemplate = """||header1||header2||header3||header4||header5||
| | | | | |
| | | | | |
| | | | | |"""
multiLine.setFormValue(tableTemplate)

And for the Server-Side Behaviour:-

def multiLine = getFieldById(fieldChanged)

def tableTemplate = """||header1||header2||header3||header4||header5||
| | | | | |
| | | | | |
| | | | | |"""

multiLine.clearError()

if (multiLine.formValue == tableTemplate) {
multiLine.setError('Value Must Be Inserted')
}

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the Behaviour configuration for your reference:-

behaviour_config.png

Give this a try and let me know how it goes.

Thank you and Kind regards,
Ram

Suggest an answer

Log in or Sign up to answer