Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The preview feature allows the script to be evaluated in the context of the issue you define. Debug (via print(“nnn”)) output will be shown on screen and will also be logged in the JEMH log (see Enabling JEMH logging ).

Example code

(info) Custom Fields have ID’s eg customfield_12345, find these on the Jira Custom field page, hover over the ‘Edit’ link and check bottom of browser to locate.

Goal

Code Snippet

Set a custom field value

Code Block
cfObj = customFieldManager.getCustomFieldObject('customfield_10114');


issue.setCustomFieldValue(cfObj, "a new value");

Making decisions based on Customer Request Type

Code Block
var custRequestType = customFieldManager.getCustomFieldObject('customfield_10203');
var issueVal = issue.getCustomFieldValue(custRequestType);
if (issueVal!==null && issueVal.toString().toLowerCase().equals("itsm/getithelp") ) {
print("requestTypeVal: "+issueVal);
cfObj = customFieldManager.getCustomFieldObject('customfield_10114');
issue.setCustomFieldValue(cfObj, "ITSM CRT set");
}

Append “[UPDATED]” and the current date to an issue summary. Then set the priority of the issue to the highest.

Code Block
print('issue: '+issue.getKey());
print('issueSummary: '+issue.getSummary());
var updatedSummary=issue.getSummary()+" [UPDATED] "+new Date().toString();
print('updating issue to: '+updatedSummary);
issue.setSummary(updatedSummary);
issue.setPriorityId(1);

Add all the pinned comments of an issue to the issues description.

Code Block
adminUser = userManager.getUserByName("admin");
if (adminUser !== null) {
    pinnedComments = commentManager.getPinnedComments(issue, adminUser);
    var description = "";
    for (i = 0; i < pinnedComments.length; i++) {
        description = description + pinnedComments[i].getComment().getBody() + "\n";
    }
    issue.setDescription(description);
}

Set an assignee of an issue, if the user “user1” exists. When the user doesn’t exist, sets the assignee of the issue as admin.

Code Block
user = userManager.getUserByName("user1");
if (user !== null) {
    issue.setAssignee(user);
} else {
    adminUser = userManager.getUserByName("admin");
    issue.setAssignee(adminUser);
}

Gather a value from a Text Custom Field and then add this value as a Comment on the issue using the Reporter user.

Code Block
var fieldObject = customFieldManager.getCustomFieldObjectByName("placeHolder");
if (fieldObject !== null)
{
    var fieldValue = issue.getCustomFieldValue(fieldObject);
    if (fieldValue !== null)
    {
        commentManager.create(issue, issue.getReporter(), fieldValue, true);        
    }
}

Context

The classes available in the context are limited. Log a Support ticket if you need to do something else:

...