Inserting value with script runner in idalko table grid

Some time ago I found this script on the internet and adapted to my need. This script only works if the user has the jira-administrator permission. Could someone help me to adapt it to work for jira-user?



import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.plugin.PluginAccessor;
import com.atlassian.jira.user.ApplicationUser;

// get an issue
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class);

// get TGE custom field
CustomFieldManager customFieldManager = ComponentAccessor.getOSGiComponentInstanceOfType(CustomFieldManager.class);
CustomField tgeCustomField = customFieldManager.getCustomFieldObjectsByName("Disponibilidade / SLA").get(0);
Long tgeCustomFieldId = tgeCustomField.getIdAsLong();
//Campos

def nova_categoria = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_12100"))
def novo_tempo_reparo = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_12102"))
def nova_disponibilidade = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_12103"))
// get current user
JiraAuthenticationContext jiraAuthenticationContext = ComponentAccessor.getOSGiComponentInstanceOfType(JiraAuthenticationContext.class);
Object userObject = jiraAuthenticationContext.getLoggedInUser();
User user = userObject instanceof ApplicationUser ? ((ApplicationUser) userObject).getDirectoryUser() : (User) userObject;

// read grid data
PluginAccessor pluginAccessor = ComponentAccessor.getPluginAccessor();
Class dataManagerClass = pluginAccessor.getClassLoader().findClass("com.idalko.jira.plugins.igrid.api.data.TGEGridTableDataManager");
def tgeGridDataManager = ComponentAccessor.getOSGiComponentInstanceOfType(dataManagerClass);

StringBuilder result = new StringBuilder();
def callResult = null;
try {
callResult = tgeGridDataManager.readGridData(issue.getId(), tgeCustomFieldId, null, null, 0, 10, user);
result.append("Grid ID=" + tgeCustomFieldId + " content before modification: " + callResult.getValues() + "\n");
} catch (Exception e) {
result.append("Grid ID=" + tgeCustomFieldId + " data cannot be retrieved: " + e.getMessage() + "\n");
}

// let's add some rows
Map<String, Object> row = new HashMap<String, Object>();
row.put("categoria_do_site", "${nova_categoria.toString()}");
row.put("tempo_de_reparo", "${novo_tempo_reparo}");
row.put("disponibilidade", "${nova_disponibilidade}");

try {
List<Long> rowIds = tgeGridDataManager.addRows(issue.getId(), tgeCustomFieldId, Arrays.asList(row), user);
result.append("Grid ID=" + tgeCustomFieldId + " data was successfully added. IDs of added rows: " + rowIds + "\n");
} catch (Exception e) {
result.append("Grid ID=" + tgeCustomFieldId + " data cannot be added: " + e.getMessage() + "\n");
}

// check if data was really added
try {
callResult = tgeGridDataManager.readGridData(issue.getId(), tgeCustomFieldId, null, null, 0, 10, user);
result.append("Grid ID=" + tgeCustomFieldId + " content after modification: " + callResult.getValues() + "\n");
} catch (Exception e) {
result.append("Grid ID=" + tgeCustomFieldId + " data cannot be retrieved: " + e.getMessage() + "\n");
}

 

0 answers

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Published Jul 25, 2018 in Marketplace Apps

Jira Cloud and Bitbucket Cloud Integration with Microsoft Teams

One of the newest products in the Microsoft family - Microsoft Teams,  is a chat-based hub for teamwork that integrates all the people, content, and tools your team needs to be more engaged and ...

718 views 0 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you