scriptrunner custom listener script on work logged on issue event.

Gunti_Reddy April 22, 2021

Hi Team ,

I am trying place to script in script runner listeners (work logged on issue event) . Please find error screenshot attached.

script is for to send email to some list of users when the time logged (calculated number custom field A ) is greater than 80% of custom field B number field in issue.
below script is working In Jira groovy console when execute against particular issue.


import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.issue.link.IssueLinkManager;
import com.atlassian.mail.Email;
import com.atlassian.mail.queue.SingleMailQueueItem;

import com.atlassian.jira.issue.worklog.Worklog
import com.atlassian.jira.issue.worklog.WorklogManager
import com.atlassian.jira.issue.MutableIssue

import org.apache.log4j.Logger
import org.apache.log4j.Level

import com.onresolve.scriptrunner.runner.util.UserMessageUtil

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();


WorklogManager worklogManager = ComponentAccessor.getWorklogManager()

Double total = 0;
Double total1 = 0;


def customField_orgEE = customFieldManager.getCustomFieldObjectsByName( "Original EE" );
def customField_aclrtd = customFieldManager.getCustomFieldObjectsByName( "Actual Reported" );

def issue = event.issue as MutableIssue

 

 

//Taking orginal EE field value and calucalte 80% from
Double sp = issue.getCustomFieldValue( customField_orgEE );
total += sp;
originalEE = 0.8 * total

//taking Actual Reported field value from RMID
Double sp1 = issue.getCustomFieldValue( customField_aclrtd );
total1 += sp1;
if (total1 >= originalEE) {
def email = new Email(issue.get("customfield_12912")?.emailAddress)
email.setCc("xxxxx.com")
email.setSubject("Jira time logged threshold")
email.setBody("Hello, Total time spent on RMID is reached more than 80% of OriginalEE for ${issue.getKey().toString()}. Kindly check it")
SingleMailQueueItem item = new SingleMailQueueItem(email);
ComponentAccessor.getMailQueue().addItem(item);
log.info "*** RMID email sent"
}


Thanks,
Gunti

 

1 answer

1 accepted

0 votes
Answer accepted
Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 24, 2021

Hi @Gunti_Reddy 

I can't see the screenshot about the error. Have you forgotten to attach it? 

Gunti_Reddy April 24, 2021

Hi @Tuncay Senturk 

Thanks for valuable response , i made below changes

It seems like the getCustomFieldObjectsByName is not able to capture the custom field object.

def customField_orgEE = customFieldManager.getCustomFieldObjectsByName( "Original EE" );
def customField_aclrtd = customFieldManager.getCustomFieldObjectsByName( "Actual Reported" );

replaced getCustomFieldObject api for example:

def customField_orgEE = customFieldManager.getCustomFieldObject("custom_10000");
def customField_aclrtd = customFieldManager.getCustomFieldObject("custom_10001");

Till here script works fine, my requirement is slightly changed now. 

whenever user worklog done on story. i need to get custom field value (it holds parent issuekey )from that story . 

and get custom field values from parent issue and compare and send email.

Thanks,

Gunti

Gunti_Reddy April 25, 2021

I did some thing like, no issues now.

//taking custom field vaue from storydef rmidkey = customFieldManager.getCustomFieldObject("customfield_12909")
def cfValue = issue.getCustomFieldValue(rmidkey)
def rmidCf = ComponentAccessor.getIssueManager().getIssueByCurrentKey("${cfValue}");
def customFieldOrgEE = ComponentAccessor.getCustomFieldManager().getCustomFieldObject('customfield_10000');
def customField_aclrtd = ComponentAccessor.getCustomFieldManager().getCustomFieldObject('customfield_10001');

//taking custom field value from parent issue

rmidcfValue = rmidCf.getCustomFieldValue(customFieldOrgEE) as Double;

 

Thanks,

Gunti

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 25, 2021

Hi @Gunti_Reddy 

Sorry for the late response.  getCustomFieldObjectsByName returns a list of custom fields of which name is the parameter you provided. It is always the best way to fetch custom field by ID, as you did later.

Anyway, as far as I understand you're storing the parent issue key in a custom field.

You can get parent issue of any issue by calling issue.getParentObject() method.

Best

Like Gunti_Reddy likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.5.1
TAGS
AUG Leaders

Atlassian Community Events