Hi everybody,
i wan to create a post-function that will will sum the values of two number custom fields. A third number field will take the value of the sum. I have made some tests using script runner where i sum the values the two fields for a certain issue but i don't know how to make it work for a workflow transition. The code i'm using in script runner is the following
import com.atlassian.jira.ComponentManager; import com.atlassian.jira.issue.CustomFieldManager; import com.atlassian.jira.issue.fields.CustomField; import com.atlassian.jira.issue.IssueManager; import com.atlassian.jira.issue.Issue; IssueManager issueManager = ComponentManager.getInstance().getIssueManager(); CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager(); Issue issue = issueManager.getIssueObject( "RF-9944" ); CustomField customField_amdtl = customFieldManager.getCustomFieldObjectByName( "Amount Due to Losses" ); CustomField customField_amdtfr = customFieldManager.getCustomFieldObjectByName( "Amount Due to Full Refund" ); Object fam = issue.getCustomFieldValue( customField_amdtl ) + issue.getCustomFieldValue( customField_amdtfr );
Thanks in advance,
Kostas
You should just be able to delete the where you define and initialise the issue variable. That will be set in the script binding. But you need to call setCustomFieldValue or something.
Thanx for the help.
The code i have used is the following:
import com.atlassian.jira.ComponentManager; import com.atlassian.jira.issue.CustomFieldManager; import com.atlassian.jira.issue.fields.CustomField; import com.atlassian.jira.issue.IssueManager; import com.atlassian.jira.issue.Issue; com.atlassian.jira.issue.MutableIssue; // gets a reference to the IssueManager and CustomFieldManager manager classes IssueManager issueManager = ComponentManager.getInstance().getIssueManager(); CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager(); // gets a reference to the desired issue and custom field CustomField customField_amdtl = customFieldManager.getCustomFieldObjectByName( "Amount Due to Losses" ); CustomField customField_amdtfr = customFieldManager.getCustomFieldObjectByName( "Amount Due to Full Refund" ); CustomField customField_fam = customFieldManager.getCustomFieldObjectByName( "Final Amount" ); // retrieves the custom field value object from the issue Object fam = issue.getCustomFieldValue( customField_amdtl ) + issue.getCustomFieldValue( customField_amdtfr ); issue.setCustomFieldValue(customField_fam, fam);
Cheers,
Kostas
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Kostas, Should the same work if I am trying to subtract? So in the Object fam line, if I make the operator - should it subtract the value?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Do you really need a third custom field and listener. Why not just add a scripted field from Jamie's plugin and just show the sum of the first two ?One line code.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Agree... post-functions/listeners are overly complicating this.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The scripted field approach is much easier and better than the idea i had.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Renjith,
Could you please explain exactly on how to do this?
Thanks,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Could somebody please help me in understanding on how to use this?
Thanks in advance,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have a scripted field which calculates the sum of two fields and it works (calculates the sum) when I edit the issue as well.....but I should use the tab key on the keyboard. Otherwise, it wouldn't add up the values.
Script in scripted field:
def CE = getCustomFieldValue("CE")
def HOE = getCustomFieldValue("HOE")
def Sum = CE + HOE
if (CE) {
return Sum
}
else {
return null
}
if (HOE) {
return Sum
}
else {
return null
}
Am I missing something?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What do you mean "but I should use the tab key on the keyboard" - the field will only be displayed in view mode, so there is nothing to tab to.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Jamie,
While editing the custom field values, I have to use the tab key on my keyboard for the Sum value to be updated.
Thanks,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I don't really understand. If you want the field to be updated as soon as the user changes a field, but before they submit, you need to use javascript or the behaviours plugin or something.
I have no idea why the tab key would have any effect on a calculated custom field.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Is it possible to use this mechanism and also add 15% to the sum?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Isn't it just a case of multiplying by 1.15 ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Kostas,
Where did you paste this script? I have three number fields and I have used the script as a post-function at 'Create Issue'. During Edit or during any other transition's that I modify the values, there is no update to the Sum field.
Could you please help me?
Thanks,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Go for a listener then.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You'd need to add it as a listener. But updating a field is a bit more complicated in a listener. You can use very similar code in a scripted field: https://jamieechlin.atlassian.net/wiki/display/GRV/Scripted+Fields
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Jamie,
I have also used 'Final Amount' as a scripted field and used the same script at Administration->Scripted fields->Final Amount. I am not able to create the issue when I do this.
Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Jamie,
Do you mean to say Script Listener or just the Atlassian Listener? Could you please point me to some documentation on how to achieve this?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you so much for your immediate response. Let me check on how to do that. Have a nice day!
Regards,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Either really but I meant a script listener: https://jamieechlin.atlassian.net/wiki/display/GRV/Listeners
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.