Hi Community, Is it possible to set default value in user picker custom field by the current user in customer portal? using behavior ?
I need to capture the current user on" Requested for " user picker custom field like on the above "Raise this request on behalf of" system field.
I tried it using this behavior , place it on Initialiser, mapped to Service Desk Project, because I just want it to become a default value. but no luck, any chances? When I try to check the console in chrome (f12)
When user is admin account, it can capture the user and place on the field, but when the user is customer account, it returns this error
Upon checking the behavior log , it returns this message:
2018-10-04 02:02:06,584 http-nio-8080-exec-8 DEBUG legoteam 122x13663x1 12ftncq 54.153.253.180,10.66.49.4 /rest/scriptrunner/behaviours/latest/jsd/jsd/validatorsByPid.json [c.o.j.groovy.user.FieldBehaviours] ------------------------TESTING BEHAVIOR-------------------------------
Upon checking /rest/scriptrunner/behaviours/latest/jsd/jsd/validatorsByPid.json, it returns this message:
"Cannot retrieve validators from PortalID: '{null}' , Request Type ID : '{null}'"This is the code:
import com.onresolve.jira.groovy.user.FormField
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
def requestorField = getFieldById("customfield_10385")
def currentUserName = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().getName()
requestorField.setFormValue(currentUserName)
It is possible to achieve this with either a Behaviour or a validator.
Behaviour scripts are capable of knowing which part of the workflow you are in. This is a good choice if you want to prevent users from clearing the component using the edit screen after it's deemed required.
Of course, a validator will work inherently within the workflow but will allow users to edit the component after the transition has taken place.
A Simple Scripted validator would look like this:
cf["Atlassian Platform"].value != 'Jira' || issue.components
Simple Scripted validators must return a true value to pass or false to generate an error.
A behaviour script would be a little more complicated:
if(!underlyingIssue) return //don't run the validator on create screen when the underlyingissue does not yet exist
def transitionToMakeCompoenntRequired = 'name of transition'
def statusesAfterThatTransition = ['status1', 'status2']
def componentsField = getFieldById('components')
def platformField = getFieldByName('Atlassian Platform')
def isTargetAction = actionName == transitionToMakeCompoenntRequired
def isTargetStatus = underlyingIssue.status.name in statusesAfterThatTransition
def isRequiredValue = platformField.value == 'Jira'
def componentRequired = (isTargetAction || isTargetStatus) && isRequiredValue
componentField.setRequired(componentRequired)
@PD Sheehan Unfortunately the simple scripted validator didn't work. I also ended up having to change my custom field name from Atlassian Platform to AM Application.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sorry... I was sloppy. It should be "cfValues", the built-in variable that scriptrunner offers that includes all the custom field values in an easily accessible map.
cfValues["AM Application"].value != "JIRA" || issue.coponents
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Bummer, still not working. I tried with both single quotes and double quotes.
cfValues['AM Application'].value != 'JIRA' || issue.coponents
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I was clearly under-caffeinated yesterday ... there was another typo: coponents vs components
cfValues["AM Application"].value != "JIRA" || issue.components
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Can you share some more details? Are there any errors in the script editor?
Do you see errors in the execution log (open the workflow/transition after executing the transition to view execution logs).
Maybe we can add some additional logging to help with identifying where it's failing:
log.info "'AM Application' is ${cfValues["AM Application"].value}"
log.info "issue.compoenents is $issue.components"
log.info "am application check is:${cfValues["AM Application"].value != "JIRA"}"
log.info "issue.component check is ${issue.components ? true : false}"
log.info "final validation result is ${cfValues["AM Application"].value != "JIRA" || issue.components}"
cfValues["AM Application"].value != "JIRA" || issue.components
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Here's a screenshot of the results and logs. Also, shouldn't it be == "JIRA" instead != "JIRA" because I only want Component/s field to be required if JIRA is selected.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The script will not work in the Script Console.
In a simple scripted validator script editor window, there will be some built-in variables and objects (you can see which by clicking the question mark below the editor).
issue and cfValues is two of those variables.
These variable are not available in the console since it's not aware of what ticket you want to act on etc.
As for the logic for the selected value... you want to return a "true" value (i.e. allow the transition to continue) either when the AM Application is something OTHER than Jira, OR if they've selected JIRA, then the components must be specified.
The way an OR (||) is processed, is that it will return true at the first instance that is true.
So from left to right, the first statement evaluated will be "AM Application" different than JIRA. If true, then return true and allow the transition. If False (AM Application IS JIRA) then evaluate the next statement.
Groovy has "thrutiness" shortcuts. So here, we are essentially validating that issue.components.size() > 0.
So, the final rule is, when AM Application is JIRA at least one component must be present.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @John Diaz
Using ScriptRunner Behaviours will let you do that easily. Take a look at this script.
Ravi
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Ravi Sagar _Sparxsys_ I appreciate the quick response. Unfortunately, Behavior isn't going to work for me.
There's a certain part of the workflow where I want someone on my team to be forced to enter a Component when moving the ticket "In Progress" ONLY IF "Jira" is selected from the "Atlassian Platform" field. So Component isn't required at the start of the ticket creation and will not be required if "Confluence" is selected.
Apologies for not providing enough context with what I'm trying to accomplish.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.