Hi,
I am trying to use a scripted field to extract information from a Table Grid Field, following this tutorial from the editor iDalko:
Using scripted fields to extract information from the grid using the table grid API
Yet I get these errors on line 44 and 45:
Could someone help me please? I don't know anything about script...
Regards,
Camille
I think the "error" is only about static type checking you did not type check the grid manager). I think the script will run, even with theses "errors".
Unfortunately the script is not really readable, please provide the full script, I can help you with the static type checking so the "errors" disappear...
Hi Marc,
Thank you for your answer, here is the full script:
/** * scriptedFieldSum.groovy * * This is a script for Scripted custom field that calculates sum of all the numbers in specified column of the TGE grid. * * Configuration: * - tgeCustomFieldName - name of the grid custom field that will be used for calculation * - columnNameToSum - column name which will be used to get data for sum calculation */import com.atlassian.crowd.embedded.api.Userimport com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.issue.CustomFieldManagerimport com.atlassian.jira.issue.fields.CustomFieldimport com.atlassian.jira.security.JiraAuthenticationContextimport com.atlassian.jira.user.ApplicationUserimport com.atlassian.plugin.PluginAccessorimport org.apache.log4j.Logger// set up loggerLogger log = Logger.getLogger("com.idalko.scripts");// configuration (update it to match your JIRA settings)String tgeCustomFieldName = "Demande d'achat NSD";String columnNameToSum = "total";// find TGE custom fieldCustomFieldManager customFieldManager = ComponentAccessor.getOSGiComponentInstanceOfType(CustomFieldManager.class);CustomField tgeCustomField = customFieldManager.getCustomFieldObjectByName(tgeCustomFieldName);Long tgeCustomFieldIdLong = tgeCustomField.getIdAsLong();// get current userJiraAuthenticationContext jiraAuthenticationContext = ComponentAccessor.getOSGiComponentInstanceOfType(JiraAuthenticationContext.class);Object userObject = jiraAuthenticationContext.getLoggedInUser();User user = userObject instanceof ApplicationUser ? ((ApplicationUser) userObject).getDirectoryUser() : (User) userObject// read the grid dataPluginAccessor pluginAccessor = ComponentAccessor.getPluginAccessor();Class dataManagerClass = pluginAccessor.getClassLoader().findClass("com.idalko.jira.plugins.igrid.api.data.TGEGridTableDataManager");def tgeGridDataManager = ComponentAccessor.getOSGiComponentInstanceOfType(dataManagerClass);Set<String> columnNames = new HashSet<String>();columnNames.add(columnNameToSum);List<Map<String, Object>> gridData = new ArrayList<Map<String, Object>>();try { def callResult = tgeGridDataManager.readGridData(issue.getId(), tgeCustomFieldIdLong, null, columnNames, 0, 10, user); gridData = callResult.getValues(); log.debug("Grid ID=" + tgeCustomFieldIdLong + " content: " + gridData + "\n");} catch (Exception e) { log.debug("Grid ID=" + tgeCustomFieldIdLong + " data cannot be retrieved: " + e.getMessage() + "\n");}// calculate sumdouble sum = 0;for (Map<String, Object> row : gridData) { Object rowValue = row.get(columnNameToSum); if (rowValue instanceof Map) { rowValue = ((Map) rowValue).get("value"); } Number rowNumber = null; if (rowValue instanceof Number) { rowNumber = (Number) rowValue; } else { String rowString = rowValue.toString(); rowNumber = Double.parseDouble(rowString); } sum += rowNumber.doubleValue();}return sum;
I think your script is correct.
You can ignore these "errors", they are just static type checking warnings , but your script should run correctly...
Did you try it ? Did you get execution errors ?
I tried but it does not display anything. I also tried without lines 44 and 45, and I get the field but it equals to zero.
Here is what I get in the Logs :
Time (on server): Wed Jan 30 2019 10:29:58 GMT+0100 (heure normale d’Europe centrale)The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
Time (on server): Wed Jan 30 2019 10:29:58 GMT+0100 (heure normale d’Europe centrale)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2019-01-30 10:29:58,481 ERROR [customfield.GroovyCustomField]: ************************************************************************************* Script field failed on issue: DANSD-23, field: Montant DA java.lang.NumberFormatException: For input string: "null" at java_lang_Double$parseDouble$1.call(Unknown Source) at Script29.run(Script29.groovy:64)
Unfortunately I've got the same problem as Camille, I tried your suggestion about the "rowValue" condition, but still the result is fixed to zero, do you have got any Idea?
by the way I used the exact code form Using scripted fields to extract information from the grid using the table grid API and only changed the required field names.
Regards
Ok, but without some more info ("the same problem..."), difficult to estimate what goes wrong :-)
It looks like you're new here. Sign in or register to get started.