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:
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|\u200bx|\u200by|"
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.
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:
and I got response from rest api:
"||Header1||Header2||Header3||Header4||Header5||\r\n| | | | | |"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:-
Give this a try and let me know how it goes.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.