Set asset custom field value with reporters asset (searching by asset assignee)

In this example, we will define a post function to set asset custom fields value. First asset CF must be empty and we perform a custom search to locate to asset and then assign it to the asset custom field.


Adding post function

Post function is added to Create Issue Transition



"[AIP] - Asset generic groovy script" is selected.




Parameters of post function

  • Asset custom fields (with values) to inject as script parameter: Select the custom field to set. In this example "Assets - 10227" is picked
  • Groovy script: Add the groovy script and modify the parameter

    • assetCustomFieldId: id of the asset custom field. To find it quickly, right click on issue edit screen where asset custom field appears and find the id of it. example:  = customfield_10227

Post function groovy script (expand to see)





import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import groovy.transform.Field
import inventoryplugin.service.index.AipIndexService
import inventoryplugin.service.index.search.QueryIndexParam
import inventoryplugin.service.index.search.QueryIndexResult
import inventoryplugin.service.index.search.QueryIndexSearchParam
import inventoryplugin.workflow.function.genericscript.dto.AssetCustomField
import inventoryplugin.workflow.function.genericscript.dto.AssetCustomFieldAndValue
import org.apache.commons.lang3.StringUtils

import java.util.logging.Logger

/*********************************************************************************************************/
/* IMPORTANT: Configure these settings according to your environment
/*********************************************************************************************************/
@Field def assetCustomFieldId = "customfield_10227"; // id of the asset custom field. To find it quickly, right click on issue edit screen where asset custom field appears and find the id of it.
/*********************************************************************************************************/


Logger logger = Logger.getLogger("inventoryplugin.groovy.script")

/**
* searches for assets that matches reporters assets (as assignee)
* and returns matching asset ids as formatted for asset custom field (i.e: ,12,343,545,)
*/
def getAssetFieldValue() {
try {
AipIndexService aipIndexService = ComponentAccessor.getOSGiComponentInstanceOfType(AipIndexService.class);
if (aipIndexService != null) {

QueryIndexParam queryIndexParam = new QueryIndexParam();
queryIndexParam.setQuerySource(QueryIndexParam.QuerySource.GENERIC_SEARCH);
queryIndexParam.setSortField("asset.name");
queryIndexParam.setSortDirection("asc");
queryIndexParam.setSortType("STRING");
queryIndexParam.setSearchType("basic");
queryIndexParam.setPageNumber(1);
queryIndexParam.setPageSize(1000);
queryIndexParam.setQueryIndexSearchParams(new ArrayList<>());

// filter by asset type
QueryIndexSearchParam query = new QueryIndexSearchParam();
query.setField("asset.assignee");
query.setKeywords(new ArrayList<>(Arrays.asList(issue.reporter.key)));
queryIndexParam.getQueryIndexSearchParams().add(query);

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
QueryIndexResult queryIndexResult = aipIndexService.queryDocsForObjectOverrideSecurity(queryIndexParam, user);
List<String> recordIds = queryIndexResult.recordIds;
if (recordIds != null && recordIds.size() > 0) {
return ',' + StringUtils.join(recordIds, ',') + ',';
}
return null
}
} catch (Exception e) {
return null
}
}

/**
* updates asset custom field of the given issue
*/
def updateAssetCustomFieldOfTheIssue(customfieldId, issueKey, assetCustomFieldValue) {
// get issue
def issueToUpdate = ComponentAccessor.getIssueManager().getIssueObject(issueKey);
// get asset custom field
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def assetCf = customFieldManager.getCustomFieldObject(customfieldId) // asset cf id
// execute update
assetCf.updateValue(null, issueToUpdate, new ModifiedValue(issueToUpdate.getCustomFieldValue(assetCf), assetCustomFieldValue), new DefaultIssueChangeHolder())
}

/**
* Checks if Asset custom field value is empty
*/
boolean isCustomFieldEmpty(assetCustomFieldId) {
for (AssetCustomFieldAndValue assetCustomFieldAndValue : assetCustomFieldAndValueList) {
AssetCustomField assetCustomField = assetCustomFieldAndValue.getAssetCustomField();
if (assetCustomField.customFieldId == assetCustomFieldId) {
if (assetCustomFieldAndValue.getAssetList() != null && assetCustomFieldAndValue.getAssetList().size() > 0) {
return false;
}
}
}
return true;
}

try {
if (isCustomFieldEmpty(assetCustomFieldId)) {
def assetCfValue = getAssetFieldValue()
if (assetCfValue != null) {
updateAssetCustomFieldOfTheIssue(assetCustomFieldId, issue.getKey(), assetCfValue)
}
return assetCfValue
}
} catch (Exception e) {
return e.message;
}
return 'success';




Save post function and move it after "Creates the issue originally."


Publish post function and test it.