Javascript in Groovy script

Mizan
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.
April 8, 2012

Hi All,

I am using the Behaviours plugin to add behavior to a field.i want to add a Javascript snippet in my Groovy script .

Below is the js which i want to add in my Groovy script

<script type="text/javascript">
AJS.$("#versions option").each(function()    {
    if( AJS.$(this).val() == 'optionId'){ 
        AJS.$(this).remove();
    }
    
});
</script>

The optionId in the above code will be provided by the groovy script.

Is this possible ? if yes, how ? (examples with code will be appreciated )

Thank you .

3 answers

1 accepted

3 votes
Answer accepted
JamieA
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.
April 8, 2012

In general it's not a good idea to have code write other code... it gets impossibly difficult to debug and deal with escaping issues.

But, I have not tried it, but if you write javascript using the setHelpText() method it will probably work.

What is it that you're tried to do, it looks like remove a version option that has the literal value optionId?

Mizan
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.
April 8, 2012

Hi Jamie ,

I am actually trying to set the options of a version picker . There is a parent child relationship between versions , this relationsihp is stored in an active object table , There are two customfields consider Parent ( A multi version picker) and child (a single version picker). I want to show only the parent versions in the Parent field and only child versions in the child field .

However due to this bug i am not able to set values to the Parent field.

So as a workaround i want to remove options from the Parent field which are child .

:( i have to complete this task this week

Did not get what setHelpText() does and how to include js using this method

Mizan
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.
April 8, 2012

Was able to run the script using setHelpText() :) thanx .

1 vote
adardesign December 27, 2012

How did you add script?

It doesnt allow <script> tags? in fact it doesnt allow any < or /> or </ charachters..

Thanks!

Mizan
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.
December 27, 2012

It worked for me ... try escaing it \x3Cscript\x3E

You can use this http://www.htmlescape.net/stringescape_tool.html

Mizan
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.
December 28, 2012

If possible paste your script here .. there might be some other escaping issue ...

adardesign December 29, 2012

Thanks @Mizan, for your help.

Here is the script i am trying to add:

getFieldById("summary").setHelpText ("\x3Cscript\x3Esssdadsasda;\x3C\x2Fscript\x3E")

This is the error i am getting (before saving)

Compilation failure: startup failed: Script1.groovy: 3: unexpected char: '\' @ line 3, column 39. getFieldById("summary").setHelpText ("\x3Cscript\x3Esssdadsasda;\x3C\x2Fscript\x3E") ^ 1 error

http://i.adoimg.com/LqyN

Mizan
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.
December 30, 2012

ok , dont escape the < > characters let it be <script> ... what is sssdadsasda ? the escaping issue might be in your script and not the <script> tag .. What is the real script you are trying to apply ?

Also you will need some imports try the below script

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField

getFieldById("summary").setHelpText ("<script>sssdadsasda;</script>")

0 votes
adardesign December 29, 2012

Thanks @Mizan, for your help.

Here is the script i am trying to add:

getFieldById("summary").setHelpText ("\x3Cscript\x3Esssdadsasda;\x3C\x2Fscript\x3E")

This is the error i am getting (before saving)

Compilation failure: startup failed: Script1.groovy: 3: unexpected char: '\' @ line 3, column 39. getFieldById("summary").setHelpText ("\x3Cscript\x3Esssdadsasda;\x3C\x2Fscript\x3E") ^ 1 error

Suggest an answer

Log in or Sign up to answer