Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

False unique contsraint violation error when using the Java API in groovy

I've been making some customization in our environment that involves object creation automatically from a jira post function.

I'm using groovy to create the new object.

But I'm getting a unique constraint violation error on an attribute that I know to have a different value than any that exist.

As a test scenario, I created a brand new object type in my schema (typeId=50) with only the default attributes. I made the "name" attribute unique.

Then I wrote this simplified script to create a new value. I run this from the console:

import com.atlassian.jira.component.ComponentAccessor
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade

def objectFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectFacade)
def objectTypeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectTypeFacade)
def objectTypeAttributeFacade = ComponentAccessor.getOSGiComponentInstanceOfType(ObjectTypeAttributeFacade)

def typeId = 50
def newObjectName = 'test3'

def objectType = objectTypeFacade.loadObjectType(typeId)
def objectTypeAttributes = objectTypeAttributeFacade.findObjectTypeAttributeBeans(objectType.id)
def newObject = objectType.createMutableObjectBean()

def nameOTAB = objectTypeAttributes.find{it.name=='Name'}
def nameOAB = newObject.createObjectAttributeBean( nameOTAB)
def nameOAVB = nameOAB.createObjectAttributeValueBean()

nameOAVB.setValue(nameOTAB, newObjectName)
nameOAB.setObjectAttributeValueBeans([nameOAVB])

newObject.setObjectAttributeBeans([nameOAB])
objectFacade.validateObjectBean(newObject)
log.info "Validation passed"
def object = objectFacade.storeObjectBean(newObject)

This fails on the storeObjectBean() method even when I know for sure that newObjectName 'test3' does not exist.

As proof, if an object with that Name exists, then the script fails on the validateObjectBean() method.

If I remove the Unique flag from the attribute, then the script works fine (of course whether or not the name already exists, potentially creating duplicates).

Am I doing something wrong in how I'm building up my mutableObjectBean? 

Any examples out there of how to create a new object that has a unique attribute?

1 answer

1 accepted

0 votes
Answer accepted

Well, nevermind ... I think I found my issue.

If I remove my line to validate the object then the store object works correctly. So, somehow the validateObjectBean must be creating a cached version of the object that the storeObjectBean trips over.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Service Management

Why upgrade to Jira Service Management Premium?

We often have questions from folks using Jira Service Management about the benefits to using Premium. Check out this video to learn how you can unlock even more value in our Premium plan.  &nb...

133 views 0 4
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you