Versions Compared

Key

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

...

Code Block
import com.atlassian.jira.component.*;
import com.deniz.jira.worklog.data.attr.AttrType;
import com.deniz.jira.worklog.data.attr.AttrValue;
import com.deniz.jira.worklog.services.attr.AttrTypeService;
import com.deniz.jira.worklog.data.attr.AttrValueImp;
import com.deniz.jira.worklog.scripting.WorklogPreEntryParameters;
import com.deniz.jira.worklog.services.*;
import com.atlassian.jira.issue.*;
import java.util.*;
import org.slf4j.*;
 
def issueManager = ComponentAccessor.getComponent(IssueManager.class);
def issue = issueManager.getIssueObject(worklogPreEntryParameters.issueKey);
 
def attrTypeService = ComponentAccessor.getOSGiComponentInstanceOfType(AttrTypeService.class);
def attrTypes = ComponentAccessor.getOSGiComponentInstanceOfType(AttrType.class);
def customFieldManager = ComponentAccessor.getComponent(CustomFieldManager.class);
 
def selectCustomField = customFieldManager.getCustomFieldObject(10400); //Getting custom field with custom field id
def selectedOption = issue.getCustomFieldValue(selectCustomField);
 
def singleSelectAttrId = attrTypeService.getAttrTypeWithName("Single Select Attribute").get().ID; //Getting attribute with attribute name
def singleSelectAttr = worklogPreEntryParameters.attrTypes.find {element -> element.id == singleSelectAttrId};
def singleSelectAttrValues=singleSelectAttr.attributeValues;
 
def attrNames=singleSelectAttrValues.collect {it -> it.name};
for(option in attrNames){
    def AttrOption=option.toString();
    if(AttrOption == selectedOption.toString()){
      def attrImp=singleSelectAttrValues.find {it -> it.name==AttrOption};
      attrImp.setDefaultValue(true);
    }
  }

The script above is assigning the value of the custom field of the issue to the relevant attribute values as a default before the worklog dialog was opened. However, if the issue is changed in the issue picker section while the worklog dialog was open, these values are not updated according to the selected issue. To update the attribute values according to the selected issue when the user changes the issue while the dialog is open ,the script below is needed to be used. The script below assigns the custom fields values specified with the IDs customfield_10500 and customfield_10501 to the attributes as a default by assigning the corresponding select type html elements which have IDs 'wa_10' and 'wa_11'. As with the script above, the important part here is that both the custom field and the attribute have the same values.

Code Block
import com.deniz.jira.worklog.scripting.WorklogPreEntryParameters;
import java.util.*;

def script = '''
  function issueChanged() {
    var issueKey=AJS.$("#log-work-issue-picker").val()[0];
    
    fetch(`/rest/api/2/issue/${issueKey}`).then(function(response) {
      if (response.status === 200) {
        response.json().then(function(data) {
          console.log("DATA:", data);
          
          if(data.fields.customfield_10500 !== null){
           var selectedOptionDevelopmentBucket=data.fields.customfield_10500.value.toString();
           console.log("selected option development bucket:", selectedOptionDevelopmentBucket);
           var devBucId =AJS.$("option:contains('" + selectedOptionDevelopmentBucket + "')").addClass("on").val();
           console.log("devBucId:", devBucId);
             AJS.$("#wa_10").val(devBucId).prop('selected', true);
             AJS.$("#wa_10").trigger("change");
          }else{
             AJS.$("#wa_10").val('').prop('selected', true);
             AJS.$("#wa_10").trigger("change");
          }
            
          if(data.fields.customfield_10501 !== null){
            var selectedOptionProductBucket=data.fields.customfield_10501.value.toString();
            console.log("selected option product bucket:", selectedOptionProductBucket);
            var prodBucId =AJS.$("option:contains('" + selectedOptionProductBucket + "')").addClass("on").val();
            console.log("prodBucId:", prodBucId);
              AJS.$("#wa_11").val(prodBucId).prop('selected', true)
              AJS.$("#wa_11").trigger("change");
          }else{
            AJS.$("#wa_11").val('').prop('selected', true)
            AJS.$("#wa_11").trigger("change");
          }
        });
      }
    });
  }
  AJS.$(document).on("change", "#log-work-issue-picker-field", function(evt) {
    setTimeout(issueChanged, 10);
  });

'''
worklogPreEntryParameters.jsScript = script;
return worklogPreEntryParameters;