Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Creating an instight object using HAPI isn't working as expected

Ismael Jimoh January 16, 2025

When I try to create or update an Asset using the HAPI's setAttribute Add method, I noticed that it is creating the item but only sets the last attribute from the source field.

Here's a snippet of the code I am using:

createdAsset.update { 

if(riskFieldValue){

log.warn(riskFieldValue)

for(int counter=0;counter < riskFieldValue.size(); counter++){

if(counter == 0){

setAttribute('Risk'){

log.warn(riskFieldValue[0])

add(Assets.getById(riskFieldValue[0].getId()))

}

}else{

setAttribute('Risk'){

log.warn(riskFieldValue[counter])

add(Assets.getById(riskFieldValue[counter].getId()))

}

}

}

}

if(processRiskOccursFieldValue){

log.warn(potentialFailureModeFieldValue)

for(int counter=0;counter < processRiskOccursFieldValue.size(); counter++){

if(counter == 0){

setAttribute('Process where the risk occurs'){

log.warn(processRiskOccursFieldValue[0])

add(Assets.getById(processRiskOccursFieldValue[0].getId()))

}

}else{

setAttribute('Process where the risk occurs'){

log.warn(processRiskOccursFieldValue[counter])

add(Assets.getById(processRiskOccursFieldValue[counter].getId()))

}

}

}

}

}
Here's the snippet I am referring to: 

https://docs.adaptavist.com/sr4js/latest/hapi/work-with-assets-insight#updating-attributes

 

Could anyone help with reviewing and letting me know why I am running into an issue where it is not taking all the Assets from the field and directly setting them in the created/updated Asset attribute?

Thanks.

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
January 16, 2025

Hi @Ismael Jimoh

My first question would be why do you need to use the for loops in your code?

Are you trying to create multiple assets at a time?

Please clarify which ScriptRunner component you are using to create the Asset. Is it the ScriptRunner Console?

I am looking forward to your feedback and clarification.

Thank you and Kind regards,
Ram

Ismael Jimoh January 17, 2025

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

Thanks for taking the time to reply.

I am using the for loop here because for the Asset that is being created, it has an attribute Risk which is linked to another Object Schema's object.

 As this relationship is one-many, I need to loop 

Originally, I started with the each operation which unfortunately resulted in the exact same result.

 

if(riskFieldValue){

riskFieldValue.each{ riskObject ->

setAttribute('Risk'){

add(Assets.getById(riskObject.getId()))

}

}

}

The above was the original code. 

Where am I using this, I am using it in a Post Function and the values for each attribute I am trying to link comes from a custom field on an issue. So in summary, when a specific transition is performed on the issue, it creates a new Asset Object.

The values for the created Object all come from the custom fields on the issue.

I originally tried using the above to set the value of each entry from the Risk field into the linked attribute of the created Object but at the end, I noticed that it always only takes the last entry.

The loop works because when I logged the entries, I see that it does return each field entry but when it came to creating or updating the Asset, it wasn't doing what I had expected and seemed to only add the last entry rather than loop through and for each entry found and it to the existing value of attribute.

Thanks again.

Ismael Jimoh January 20, 2025

@Ram Kumar Aravindakshan _Adaptavist_ do you have any update on the following question?

Do you need any further clarification on what I want to achieve?

Thanks and I am looking forward to hearing from you.

 

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
January 22, 2025

Hi @Ismael Jimoh

Appologies, I've been a bit busy.

I will get back to you once I have an update.

Thank you and Kind regards,
Ram

Ismael Jimoh January 24, 2025

Thanks a lot @Ram Kumar Aravindakshan _Adaptavist_ .

Much appreciated.

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
January 26, 2025

Hi @Ismael Jimoh

I tried a very simple test in my environment: a Host Server with Multiple Connected devices. I wanted to update the Host value in two of the devices.

Below is the sample working code that I have tested with:-

import com.adaptavist.hapi.jira.assets.Assets

def host = Assets.getByKey('SAM-4')

def referenceAssets = host.getReferences('Referenced Assets')*.getString('Key') as List

referenceAssets.each {
Assets.getByKey(it).update {
setAttribute('Host', 'Host1')
}
}

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below are screenshots before the Asset is updated:-

image1.pngimage2.png

Below are screenshots after the code is triggered:-

image3.pngimage4.png

If you observe the screenshots above I am able to update the value of the Host field in multiple Assets using the approach I have provided.

I hope this helps to answer your question. :)

I am looking forward to your feedback.

Thank you and Kind regards,

Ram

 

Ismael Jimoh January 27, 2025

Thanks a lot @Ram Kumar Aravindakshan _Adaptavist_ .

What I think I am attempting to achieve is the direct opposite of what you wrote.

Instead of Host1 being set on each connected device, I want each of the connected devices to be added to a Host1 attribute called Connected Devices and which allows multiple objects to be set in this attribute.

I had tried with a single entry and now wonder if this is meant to just accept an Array instead of parsing each individual value.

My understanding when looking at the documentation however says that if an attribute can take multiple values, then I should use the

SetAttribute(Attribute){add{value to add}}.

 

Let me know if you have a suggestion with how this is meant to work. 

Thanks.

Ismael Jimoh February 28, 2025

Thanks @Ram Kumar Aravindakshan _Adaptavist_ . 

I got the answer. The issue was to do with where I positioned the loop.

I need to loop within the Set Attribute method else it would always update the previous version I set.

Thanks again.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events