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

Scriptrunner Behaviour copy Insight object attributes to Insight customfield

Aimane SBAI
Contributor
October 25, 2023

Hello community,

 

I have 2 objectTypes "Application" and "Server" in my Insight Schema.

"Application" object has attribute of type "object" called "Application hosts", referencing "Server" objects.

I have also created 2 Insight customfields "CMDB - Server" and "CMDB - Application" linked to these 2 objectTypes and I display them in an "create issue" screen.

I would like to code a behaviour script that displays the "Application hosts" attribute values in the "CMDB - Servers" customfield when an application is selected in the "CMDB - Application" customfield.

Exemple :

object "Application X" has 2 values in attribute "Application hosts" : "Server A" and "Server B".

Then, in the "create issue" screen, when I select "Application X" in the "CMDB - Application" customfield, I would like to have the objects "Server A" and "Server B" automatically selected in the "CMDB - Server" customfield.

 

Thanks a lot for your help,

Aimane 

 

 

2 answers

1 accepted

1 vote
Answer accepted
Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 25, 2023

You don't need behaviours for this.

This is possible out of the box with Insight Custom field configuration.

Try configuring your "CMDB - Server" as such:

  • Filter Scope (IQL): objectType=Server
  • Filter Issue Scope: object having inR(objectType=Application and Key in (${CMDB - Application${0}})) 
  • Filter Assign Scope (IQL): object having inR(objectType=Application and Key in (${CMDB - Application${0}}))

Now, just leave your CMDB - Server field off the screen and when you create an issue, server objects will be added to the field automatically.

If you absolutely need for the field to be visible and editable during creation, then yes, a behaviour script will be needed.

Create a behaviour with appropriate mapping and add the CMDB Application field.

Create a server-side script and start with a script that looks somewhat like this:

 

if(underlyingIssue){
//don't do anything if the issue exists (this means we're currently editing an issue)
return
}
def applicationField = getFieldById(fieldChanged)
def serverField = getFieldByName('CMDB - Server')

def selectedApplications = applicationField.value

if(!applicationField.value){
//no value in Application field, remove server values and exist
serverField.setFormValue(null)
return
}
if(selectedApplications instanceof String){
selectedApplications = [selectedApplications]
}

def iqlServer = "objectType = Server and inR(objectType = Application and Key in (${selectedApplications.join(',')}"
def serverObjects = Assets.search(iqlServer)
serverField.setFormValue(serverObjects*.objectKey)
Aimane SBAI
Contributor
October 25, 2023

Hi @Peter-Dave Sheehan 

Thanks a lot for taking time and for this very complete answer but still does not fully resolves my problem.

I admit that I did not fully describe my use case ... :( 

In fact, I have actually 2 attributes in object "Application" .

  • "Application hosts
  • "Database hosts"

And both of them are referencing "Server" objects. (same objectType) ...

I have then 2 Insight custom fields "CMDB - Application Server" and "CMDB - Database Server" that I want to fill in the creation screen (they must be visible and editable yes -> So, I need a behaviour).

  • "Application hosts" attribute values should go to "CMDB - Application Server" customfield
  • "Database hosts" attribute values should go to "CMDB - Database Server" customfield

That's why IQL does not work for my usecase. I really need like a script that copies the attribute values in the corresponding insight customfield.

Any idea ?

 

Thanks a lot !

Aimane  

Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 25, 2023

That is when the Reference Type in your attribute configuration comes in handy. 

The two attributes that point to Server should have different Reference Types.

Then in the IQL (either type of solution), you can add a second argument to the inboundReference() function (abbreviated to inR() in my earlier response) in the IQL.

E.g.

object having inR(objectType=Application and Key in (${CMDB - Application${0}}), refType in ("Name of unique Reference Type"))

You can read more about this in https://confluence.atlassian.com/display/SERVICEDESKSERVER/Advanced+searching%3A+AQL+-+Assets+Query+Language (search for referenceType and inboundReferences

Aimane SBAI
Contributor
October 26, 2023

Thank you so much @Peter-Dave Sheehan !!!

refType in() was exactly what I was looking for !

Everything is working well now.

 

Thanks again.

Aimane 

0 votes
Bobby Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 25, 2023

Hi @Aimane SBAI ,

 

ScriptRunner behaviours on Server/DC support Asset custom fields, so you should be able to get the value of the "CMDB - Application", and set the values of the "CMDB - Server" in the same way you would normal fields. 

If you are on version 7.11 or above of ScriptRunner, you are able to use the HAPI Assets/Insight module to easily access the information of the Asset you have selected in the "CMDB-Server" field. You can find the documentation of how to do this here: https://docs.adaptavist.com/sr4js/latest/hapi/work-with-assets-insight

Does this help?

Suggest an answer

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

Atlassian Community Events