How to create scripted condition by referrer URL

bauerprocessconsulting November 20, 2017

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

0 votes
Thanos Batagiannis _Adaptavist_
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.
November 20, 2017

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

bauerprocessconsulting November 21, 2017

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

Thanos Batagiannis _Adaptavist_
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.
November 21, 2017

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

bauerprocessconsulting November 21, 2017

Hello Again Thanos.

I'm on version 3.3.0....

Kind regards, Søren

Thanos Batagiannis _Adaptavist_
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.
November 21, 2017

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 ?

bauerprocessconsulting November 22, 2017

Hello Thanos

 

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

 

Kind regards

Søren

Thanos Batagiannis _Adaptavist_
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.
November 22, 2017

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