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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published May 30, 2018 in Marketplace Apps

Three tips for boosting your board's efficiency with Story Maps

Trello is one of the most effective tools for driving your sprints. It's customizable for every Agile team and product owners and Scrum masters (SM) love it. However, Agile teams often struggle with:...

827 views 2 9
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