Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Getting error on code

Preeti Yadav June 19, 2023

Hi Team,

Please help with below error.

I am getting error on for this line - 

VpOrigin AufgabeVpOrigin = new VpOrigin(portal.key, AufgabeRequestType.key);

Below are the error's which are coming

 

1. [Static type checking] - Cannot find matching method com.atlassian.servicedesk.internal.customfields.origin.VpOrigin#<init>(java.lang.Object, java.lang.Object). Please check if the declared type is correct and if the method exists.
2. [Static type checking] - No such property: key for class: com.atlassian.servicedesk.api.portal.Portal
Possible solutions: id

Please check the below code :-

package swit

import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.*
import com.atlassian.jira.config.*
import com.riadalabs.jira.plugins.insight.services.model.*
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.servicedesk.api.requesttype.*
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.*
import com.riadalabs.jira.plugins.insight.services.core.*
import com.atlassian.jira.bc.user.search.UserSearchService
import com.atlassian.servicedesk.api.ServiceDeskManager
import com.atlassian.servicedesk.api.ServiceDesk;
import com.atlassian.jira.project.Project
import com.atlassian.servicedesk.api.portal.PortalService
import com.atlassian.jira.user.util.UserManager
import com.atlassian.servicedesk.internal.customfields.origin.VpOrigin

//import com.riadalabs.jira.plugins.insight.services.model.InsightObject

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

Issue issue

@WithPlugin(["com.riadalabs.jira.plugins.insight","com.atlassian.servicedesk"])

IssueManager issueManager = ComponentAccessor.getIssueManager()
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//!!!!!!!!!! LINE TO COMMENT OUT
// issue variable will be injected by context of workflow transition
//MutableIssue issue = issueManager.getIssueObject("SWIT-254")
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//!!!!!!!!!!!!



RequestTypeService requestTypeService = ComponentAccessor.getOSGiComponentInstanceOfType(RequestTypeService)
ServiceDeskManager serviceDeskManager = ComponentAccessor.getOSGiComponentInstanceOfType(ServiceDeskManager)

PortalService portalService = ComponentAccessor.getOSGiComponentInstanceOfType(PortalService)
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
UserSearchService userSearchService = ComponentAccessor.getComponent(UserSearchService)
UserManager userManager = ComponentAccessor.getUserManager() as UserManager

def issueFactory = ComponentAccessor.getIssueFactory()

def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectFacade)

Logger Log = Logger.getLogger("swit-request-splitter")
Log.setLevel(Level.TRACE)


//IssueTypeManager itm = ComponentAccessor.getIssueTypeManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

def costCenterFieldObj  = customFieldManager.getCustomFieldObjectsByName("CostCenter")[0]
def cfRequestType = customFieldManager.getCustomFieldObjectsByName("Customer Request Type")[0]
def cfApprovers = customFieldManager.getCustomFieldObjectsByName("Approvers")[0]
//Log.info(cfApprovers.class.name)
//return

IssueLinkTypeManager issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager)
def availableIssueLinkTypes = issueLinkTypeManager.issueLinkTypes
def linkType = availableIssueLinkTypes.findByName("Relates")

//def costCenterFieldObj  = customFieldManager.getCustomFieldObject(COST_CENTER_FIELD_ID)


def costCenterObject =  ((Object [])issue.getCustomFieldValue(costCenterFieldObj))[0]

def reqQ = requestTypeService.newQueryBuilder().issue(issue.id).build()
def reqT = requestTypeService.getRequestTypes(currentUser, reqQ)
Optional<RequestType> requestType = reqT.findFirst()

//Log.info(issue.getProjectObject());

ServiceDesk serviceDesk = serviceDeskManager.getServiceDeskForProject(issue.getProjectObject())
def portal = portalService.getPortalForProject(currentUser, issue.getProjectObject())

def query = requestTypeService.newQueryBuilder().serviceDesk(serviceDesk.getId()).build()
RequestType [] reqTypes = requestTypeService.getRequestTypes(currentUser, query).findAll();

RequestType AufgabeRequestType = reqTypes.find { it-> it.getName() == "Aufgabe" }

Log.info("query: " + query)
Log.info("reqTypeToAssign: " + AufgabeRequestType)
for(RequestType reqType in reqTypes ){
    Log.info("regType: " + reqType.getName() + ", " + reqType.getKey());
}

VpOrigin AufgabeVpOrigin = new VpOrigin(portal.key, AufgabeRequestType.key);

//return;

String requestTypeName = requestType.get().getName()
Log.info("req type: " + requestTypeName)
//log.info("blah:" + costCenterObject.objectAttributeBeans)
String insightFieldName;

switch(requestTypeName){
    case "E-Mail Verteiler":
        insightFieldName="E-Mail Verteiler";
        break;

    case "Raum/Ressource":
        insightFieldName="Raum/Ressource";
        break;

    case "Verzeichnis":
        insightFieldName="Verzeichnisse";
        break;

    case "Windows Gruppe":
        insightFieldName="Windows Gruppe";
        break;

    case "Gruppenpostfach":
        insightFieldName="Gruppenpostfach";
        break;

    default:
        Log.error("unsupported request type ["+requestTypeName+"] - bail out");
        return;
        break;
}
def insightFielObject = customFieldManager.getCustomFieldObjectsByName(insightFieldName)[0]
ObjectBean [] insightObjects =  (List<ObjectBean>)issue.getCustomFieldValue(insightFielObject)

Log.info("issueTypeId" + issue.issueTypeId);
Log.info("issueType" + issue.issueType);
Log.info("issueTypeObj" + issue.getIssueType());
def taskIssueType = ComponentAccessor.issueTypeSchemeManager.getIssueTypesForProject(issue.projectObject).find{it.name=="Task"};
Log.info("taskIssueType" + taskIssueType);
Log.info("link type: " + linkType);

List<Object> insightObject_attributeValues(ObjectBean obj, String attrName){
    Logger Log = Logger.getLogger("swit-request-splitter")
    Log.setLevel(Level.TRACE)
    List<Object> ret = []
//    def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectFacade)
    ObjectTypeAttributeFacade objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectTypeAttributeFacade);
    ObjectAttributeBean [] attributes = obj.getObjectAttributeBeans()
    for(ObjectAttributeBean attr in attributes){
        def objectTypeAttributeId = attr.getObjectTypeAttributeId();
        def objectTypeAttributeBean = objectTypeAttributeFacade.loadObjectTypeAttributeBean(objectTypeAttributeId)
        String currAttributeName = objectTypeAttributeBean.getName()
        if(currAttributeName != attrName){
            continue;
        }
       
//        ObjectAttributeBean attrBean = objectFacade.loadObjectAttributeBean(obj.getId(), objectTypeAttributeId);
//        ObjectTypeAttributeBean meta = objectTypeAttributeService.loadObjectTypeAttribute(objectTypeAttributeId)

//        Log.info("attr meta: " + objectTypeAttributeBean)
        ObjectAttributeValueBean [] values = attr.getObjectAttributeValueBeans()
        for (def val in values){
//            Log.info("  val: " + val.value)
            ret.add(val.value)
        }
        //Log.info("attr: " + attr.getId() + ", " + objectTypeAttributeId + ", " + attr.getObjectId());
    }
    //Log.info(attributes);
    return ret;
}

//return;

int cloneNumber =0;

insightObjects.each { obj ->
    cloneNumber++;

    Log.info("obj: " + obj.getId() + " - " + obj.getName())
    String [] approverEmails = insightObject_attributeValues(obj,"Approver")
    List<ApplicationUser> approverUserObjects = [];
    //List<String> approvers
    for(def approverEmail in approverEmails){
        Log.info("approver: " + approverEmail)
        //ApplicationUser user = userSearchService.findUsersByEmail(approverEmail).first()
        ApplicationUser user = userManager.getUserByName(approverEmail)
        if (user == null){
            user = userManager.getUserByKey(approverEmail)
        }
        Log.info("user: " + user)
        approverUserObjects.add(user)
    }
    //Log.info(approvers)
    String insight_object_description = insightObject_attributeValues(obj, "Description")[0]
    Log.info("descr: " + insight_object_description)
//    return;
    def newIssue = issueFactory.cloneIssueWithAllFields(issue)
    newIssue.setIssueTypeObject(taskIssueType);
    newIssue.summary = requestTypeName + " - Aufgabe #" + cloneNumber + " von " + issue.key + " " + insight_object_description
    newIssue.setCustomFieldValue(insightFielObject, [obj])
   
    newIssue.setCustomFieldValue(cfApprovers, approverUserObjects);
//    newIssue.setCustomFieldValue(cfApprovers, approverEmails);
    newIssue.setCustomFieldValue(cfRequestType, AufgabeVpOrigin);
    Issue createdIssue = issueManager.createIssueObject(currentUser, newIssue)
    Log.info("new issue: " + createdIssue.getKey());

    ComponentAccessor.getIssueLinkManager().createIssueLink(issue.getId(), createdIssue.getId(), linkType.id, 1, currentUser)

}


1 answer

0 votes
Radek Dostál
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 19, 2023

There's a lot to unpack here..

 

But first look:

[Static type checking] - Cannot find matching method com.atlassian.servicedesk.internal.customfields.origin.VpOrigin#<init>(java.lang.Object, java.lang.Object).

This is saying that there is no such a constructor. However, it also says (Object, Object), because you're using groovy generics, so static type checking doesn't know the classes you're trying to create the value with.

You might try something like

new VpOrigin((String) portal.key, (String) AufgabeRequestType.key)

 

Assuming that both should be strings. Then, if there is a valid constructor for two strings, it should work. Though I don't seem to get my hands on sources for that class so I don't know the proper constructor, if any. I don't seem to find javadocs either, so, dunno where you found it.

 

Hmm and on second look (the one that I can actually see sources for):

public interface Portal {
int getId();

String getName();

String getDescription();
}

I don't think that portal.key will work at all, unless the "key" is implemented in the concrete class, but it certainly is not in the interface.

 

 

You also might check other threads for similar thing, e.g. https://community.atlassian.com/t5/Answers-Developer-Questions/Set-JIRA-Service-Desk-Custom-Field-in-groovy/qaq-p/518940

people seem to be having more success with 'requestTypeCF.getCustomFieldType().getSingularObjectFromString("STRING")' in general, across couple other threads, so maybe instead of a constructor you could try that approach.

Suggest an answer

Log in or Sign up to answer