Hello there.
One of my customer is using Script Runner Behaviors for dynamic forms and for prefilling data to various fields based on current user.
Mainly:
- The Current user is filled into custom field
- The Current user do have "shadow in Insight"
- One of the attributes in Insight is the user manager
- User manager is populated into another custom field
We dont know what exactly happened, code is little bit old, but right now customer figured out, that it wont work, if the user is logged in as the customer.
I tried it, reproduce it with 100% success. As service desk or software or core user, everything works.
As customer, neither current user or manager and other fields are populated.
Any idea where it could be wrong? Probably happened with some Script Runner or Insight upgrade, but noone noticed for a while ...
import com.atlassian.jira.component.ComponentAccessor
// Get the current user
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
// Get the Requestor field
def requestor = getFieldById("customfield_10401")
if (requestor.formValue == "")
{
requestor.setFormValue(currentUser.key)
}
// change this to ID of schema, where IQL should search for objects
def schemaID = 3
// change this to name of field, where Phone number should be stored
def phoneField = getFieldByName("Contact number")
// change this to name of field, where Manager should be stored
def managerField = getFieldByName("Manager or supervisor (approver)")
// change this to name of attribute in Insight, where phone number is stored
String attributeManager = "User Manager"
// change this to name of attribute in Insight, where phone number is stored
String attributePhone = "Mobile Phone"
// change this to name of attribute in Insight, where user key is stored
String attributeUserID = "Employee ID"
if( getFieldById(getFieldChanged()).getValue() )
{
// Get Insight IQL Facade from plugin accessor
Class iqlFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.IQLFacade")
def iqlFacade = ComponentAccessor.getOSGiComponentInstanceOfType(iqlFacadeClass)
Class objectFacadeClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade")
def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(objectFacadeClass)
def userKey = getFieldById(getFieldChanged()).getValue().toString()
def objects = iqlFacade.findObjectsByIQLAndSchema(schemaID, "\"" + attributeUserID + "\" = \"" + userKey + "\"")
int objectId = objects[0].getId()
def objectPhone = objectFacade.loadObjectAttributeBean(objectId, attributePhone)
if( objectPhone )
{
def objectAttributeValues = objectPhone.getObjectAttributeValueBeans()
def objectAttributeValue = objectAttributeValues[0]
phoneField.setFormValue(objectAttributeValue.getValue())
}
else
{
phoneField.setFormValue(null)
}
def objectManager = objectFacade.loadObjectAttributeBean(objectId, attributeManager)
if( objectManager )
{
def objectAttributeValues = objectManager.getObjectAttributeValueBeans()
def objectAttributeValue = objectAttributeValues[0] // object ID
def objectManagerRef = objectFacade.loadObjectAttributeBean(objectAttributeValue.getValue(), attributeUserID)
if( objectManagerRef )
{
objectAttributeValues = objectManagerRef.getObjectAttributeValueBeans()
objectAttributeValue = objectAttributeValues[0]
managerField.setFormValue(objectAttributeValue.getValue())
}
else
{
managerField.setFormValue(null)
}
}
else
{
managerField.setFormValue(null)
}
}
else
{
phoneField.setFormValue(null)
managerField.setFormValue(null)
}
Insight 8.4.3
Script Runner 5.9.1-p5
Jira Software 8.5.1
Jira Service Desk 4.5.1
Thanks for any ideas