How to create scripted condition by referrer URL

I'm trying to prevent issue creation if initiated from a specific URL, i.e. {base-URL}/servicedesk/customer/portal/6/create/27.

I can't find the scriptrunner method required to do this.

Any ideas?

1 answer

Hi Soeren, 

So the referrer URL you posted, if you "decode" it means:

portal - The request came from the portal

6 - The portal id of the project

create - The action 

27 - The id of the request type

So, I do not think that you can get the referrer url in a scripted validator, but what you can do is. 

For the project with the portal Id 6 and for the request type with id 27 and in the issue creation step to create a validator that will check if the creation came form the portal or not. 

Now If the workflow is shared among different projects and/or request types, then a script will be required. 

Below is a script that demonstrates how to get a portal id from the project, the id of a request type upon issue creation and finally if the request came from the portal or not. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.servicedesk.api.portal.PortalService
import com.atlassian.servicedesk.api.requesttype.RequestTypeService

// the issue in creation that triggers the validator
def issue = issue as MutableIssue
def adminUser = ComponentAccessor.userManager.getUserByKey("admin")

def portalService = ComponentAccessor.getOSGiComponentInstanceOfType(PortalService)
def requestTypeService = ComponentAccessor.getOSGiComponentInstanceOfType(RequestTypeService)

// get issue project and then it's portal id
def project = issue.projectObject
def portalId = portalService.getPortalForProject(adminUser, project)?.right()?.get()?.id

// get issue request type value and get the id of it
def requestTypeCF = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Customer Request Type")
def requestType = issue.getCustomFieldValue(requestTypeCF)
def requestQuery = requestTypeService.newQueryBuilder().issue(issue.id).build()
def requestTypeId = requestTypeService.getRequestTypes(adminUser, requestQuery)?.right()?.get()?.results?.find {it.key == requestType?.requestTypeKey}?.id

// check if the request comes from portal or not
def objectNode = transientVars."issueProperties".get("request.channel.type")
def isFromPortal = objectNode?.get("value")?.textValue == "portal"

log.debug "Portal id: $portalId | Request Type Id: $requestTypeId | Is the request comming from the portal ? : $isFromPortal"

If something does not make sense, or if you need further assistance , please ping me.

Kind regards, Thanos

Hello Thanos.

 

Thanks for your reply - I think I get it.

 

For now, I'm getting errors:

"

unable to resolve class com.atlassian.servicedesk.api.requesttype.RequestTypeService and unable to resolve class com.atlassian.servicedesk.api.portal.PortalService

" - so I guess something is not loading correctly at our end....

 

I'll try to get that fixed, and then I'll try your script.

 

Thanks again for taking the time to help.

 

Kind regards

Søren

Hey Søren,

I forgot to mention that the above script depends on the ServiceDesk version you are in. So what is your ServiceDesk version ?

Regards, Thanos

Hello Again Thanos.

I'm on version 3.3.0....

Kind regards, Søren

Hi Søren,

I tested it on a 3.3.0 version and it seems to work. Can I ask you if you have the script inline or you have it in a file ?

Are the errors you get coming from the logs or you see those errors in the UI ?

Hello Thanos

 

The script is inline, and the errors are from the UI.

 

Kind regards

Søren

Hey Søren

It is possible those errors to be static type checking errors.

In that case you can ignore them and see if there are any real problems by checking your application logs

regards, Thanos

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,517 views 15 21
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you