modificar el valor de un campo en el evento load con scriptrunner cloud jira cloud?

stephen January 17, 2025

Buenas tardes, estoy intentando modificar el valor del campo descripción o de un campo pesonalizado con scriptrunner behaviour cloud jira cloud pero no lo hace, es posible?

aqui el código: 

 

document.addEventListener("DOMContentLoaded", function() {
    // Encuentra el campo de descripción por su id
    const descriptionField = document.querySelector("#summary");

    // Verificar si el campo de descripción existe y es un textarea
    if (descriptionField && descriptionField instanceof HTMLTextAreaElement) {
        // Si el campo está vacío, asignar "Hola Mundo"
        if (!descriptionField.value.trim()) {
            descriptionField.value = "Hola Mundo";
        }
    }
});

1 answer

0 votes
Ashish Bijlwan
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.
January 20, 2025

Hi @stephen 

Welcome to Atlassian Community!

You can do it easily using Jira Automation as mentioned below. 

Go to Settings --> System --> Global Automation --> Create Rule

image.pngimage.png

image.png

 

Regards,
Ashish

 

 

 

stephen January 20, 2025

Hi @Ashish Bijlwan  thank you very much for your response, I had done it as you indicated, the problem is that the text: Hello World is seen after the issue is created and I need that Hello World text to be displayed when opening the form to create the issue, in the Load event it seems to me, please do you know if it is possible to do that?

Ashish Bijlwan
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.
January 20, 2025

Hi @stephen 

This can be done using ScriptRunner Behaviour. Here is the code for that.

const descriptionValue = getFieldById("description").getValue();

if (typeof descriptionValue !== "string") {
const descriptionValueContent = descriptionValue.content.toString();

if (!descriptionValueContent) {

getFieldById("description").setValue({
"version": 1,
"type": "doc",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Hello World ",
"marks": [
{
"type": "strong"
}
]
})
}
}

 

Regards,
Ashish

stephen January 21, 2025

Hi @Ashish Bijlwan thank you very much for the help but your script does not work, I have also tried the following:  

var campoInstrucciones = getFieldById("customfield_10061");
if (campoInstrucciones) {
campoInstrucciones.setValue("HELLO WORLD");
console.log("El texto 'HELLO WORLD' se ha establecido correctamente en el campo Instrucciones.");
} else {
console.error("No se encontró el campo Instrucciones con el ID 'helptext'.");

with a custom field but it doesn't make the change either, it seems to me that scriptrunner behaviour cloud in jira cloud doesn't allow modifying the values ​​of the fields in a form

Ashish Bijlwan
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.
January 21, 2025

Hi @stephen 

Did you tried this code for field Description (system field which is set to use the WIKI field renderer) , because it's working for me:

const descriptionValue = getFieldById("description").getValue();
if (typeof descriptionValue !== "string") {
const descriptionValueContent = descriptionValue.content.toString();

if (!descriptionValueContent) {

    getFieldById("description").setValue({
        "version": 1,
        "type": "doc",
        "content": [
            {
                "type": "paragraph",
                "content": [
                    {
                        "type": "text",
                        "text": "Hello World ",
                        "marks": [
                            {
                                "type": "strong"
                            }
                        ]
                    }
   
    })
}
}
       
Below are the screenshot of code and the result for the reference:

image.png

image.png

 

Also, for other Text type custom field which is set to use the DEFAULT Text field renderer, kindly use below 

const descriptionValue = getFieldById("customfield_10037").getValue();
if(!descriptionValue.toString())
{
 getFieldById("customfield_10037").setValue('Hello World')
}
image.png

In the above screen shot Default Text is appearing on both the fields Description (system field) and the Text_Field (custom field) I used the above codes separately for both the Fields.


Regards,
Ashish
stephen January 22, 2025

hi @Ashish Bijlwan I don't doubt that your script works correctly, but it seems that I have something wrong configured or maybe it's the type of project. I don't know, I'll send you the images, I follow the steps as you indicate but it doesn't work. What type of project do you have configured where the script works? I'm using Jira Cloud - Jira Service Management - Support Project and the issue is a task.

imagen1.PNGimagen2.PNGimagen3.PNG

Ashish Bijlwan
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.
January 23, 2025

Hi @stephen 

Currently ScriptRunner Behaviours only supports Jira Software (Company Managed Projects). Refer https://docs.adaptavist.com/sr4jc/latest/features/behaviours/behaviours-supported-fields-and-products for more info.

I am using Jira Software (Company Managed Projects).

 

Regards,
Ashish

stephen January 24, 2025

Hi @Ashish Bijlwan , thank you very much for your support, now the Hello World works in the description field, please ask one more question, I need to show a list of Jenkins jobs in that description field and I have the following script but it doesn't work, is it possible to show that list in the description field in the same context of scriptrunner behavior? script:  

const jenkinsUrl = 'jenkinsurl/api/json?tree=jobs%5Bname%2Curl%2Cjobs%5Bname%2Curl%5D%5D';
const base64Credentials = 'usuario:apitoken';
const authHeader = 'Basic ' + base64Credentials;

let errorMessage = '';
function setDescription(content) {
const descriptionField = getFieldById('description');
if (descriptionField) {
descriptionField.setValue({
"version": 1,
"type": "doc",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": content
}
]
}
]
});
}
}

function processJobs(jobs) {
let jobList = '';
jobs.forEach(job => {
jobList += `${job.name} - ${job.url}\n`;
if (job.jobs && Array.isArray(job.jobs)) {
job.jobs.forEach(subJob => {
jobList += ` ${subJob.name} - ${subJob.url}\n`;
});
}
});
return jobList;
}
setDescription('hola mundo');
fetch(jenkinsUrl, {
method: 'GET',
headers: {
'Authorization': authHeader,
'Accept': 'application/json'
}
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
let jobList = ''; // Inicializamos la lista de jobs

// Verificar si se obtuvieron jobs
if (data.jobs && Array.isArray(data.jobs)) {
jobList = processJobs(data.jobs);

// Si hay jobs, procedemos a llenar el campo de descripción
if (jobList) {
setDescription(jobList);
} else {
errorMessage = 'No se encontraron jobs para mostrar.';
}
} else {
errorMessage = 'No se encontraron jobs en la respuesta de Jenkins.';
}
})
.catch(error => {
// En caso de error en la solicitud o procesamiento
errorMessage = `Error al obtener los jobs de Jenkins: ${error.message}`;
})
.finally(() => {
// Si hubo un error, asignamos el mensaje de error al campo descripción
if (errorMessage) {
setDescription(errorMessage); // Establecer el contenido de error como valor del campo
}
});

Ashish Bijlwan
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.
January 27, 2025

Hi @stephen 

For this please raise help request on https://the-adaptavist-group-support.atlassian.net/servicedesk/customer/user/login they can help you with the correct function to call the Jenkins jobs with in Description field.

 

Regards,
Ashish

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events