Update Assets Date Attribute with HAPI

Lara Lopez July 4, 2023

Hello,

I have an Assets object in which I want to update a date via Postfunction, I am testing HAPI to do this. I have been following this documentation https://docs.adaptavist.com/sr4js/latest/hapi/work-with-assets-insight

I am able to update normal text fields but I am not sure about the format to update a date field.

 

def asset = Assets.getByKey('ITASSET-12749')
 
asset.update {
    setAttribute('Retired Date') {
         set(new Date())
    }
}
I have also tried
set(get()) 
following the cf documentation but that does not work.
Any tips?

1 answer

1 accepted

3 votes
Answer accepted
Lara Lopez July 5, 2023

Correct way was:

def asset = Assets.getByKey(issuevalue)
    asset.update {
        setAttribute('Retired Date', new Date())
    }
Jamie Echlin _ScriptRunner - The Adaptavist Group_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
July 5, 2023

Hey @Lara Lopez , tech lead for ScriptRunner here. Yep, you are correct. Give us a shout if you need anything else in HAPI Assets.

Like Lara Lopez likes this
Lara Lopez July 5, 2023

Hey Jamie!

Loving HAPI! Specially for working with Assets (Insight)

It would be great if it had something to make it easier to get in Server/DC the key of an Insight object from a customfield to work with it.

The above question was related to this little script, as you see when I get the key of the Assets object from the custom field of the issue, I have to use regex to just pick the key, as it returns something like [ABE124 (ITASSET-123)] and I only need the info between the brackets.

def issue = Issues.getByKey('ISSUE-111')
def issuetoupdate = issue.getCustomFieldValue('Affected Device').toString()

def matcher = issuetoupdate =~ /\((.*?)\)/
String issuevalue = matcher.find() ? matcher.group(1) : null
if (issuevalue != null) {
    def asset = Assets.getByKey(issuevalue)
    asset.update {
        setAttribute('Retired Date', new Date())
    }
    }
I hope this gives you some ideas!
Cheers!
Jamie Echlin _ScriptRunner - The Adaptavist Group_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
July 5, 2023

Hi Lara... 

Great to hear your feedback, thanks!

So, `getCustomFieldValue` will return the actual type of the custom field, in this case it is a List<ObjectBean>, or list of ObjectBeans.

You are calling toString on that, which is why you get the square brackets, and then `ABE124 (ITASSET-123)` is presumably the label and key.

I believe you always get a list from the Insight field, so you need to get the first object if present, then call the getObjectKey() method as shown here:


def issue = Issues.getByKey('JRA-1')
def insightObjects = issue.getCustomFieldValue('Affected Device')

def objectKey = insightObjects?.first()?.objectKey

// do something with objectKey
Like saitokazushi likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events