Automation to create 1 issue for every found object in the correct project based on object attribute
Asked by Danilo Simic:

Hello everyone,

I want to create an automation rule that will do the following:

1. Periodically check if there are Certificate objects whose expiration date is lower than 30 days
2. For those that fit this condition I want to create a ticket with some information:
   1. The project must be the correct project (Certificates have inbound relationship with Host objects in which there is an attribute with JSM projects filled in) - I have no idea how to pull this information in a smart value or something similar so that it is dynamic for each found Certificate
   2. In an Asset field save the related Certificate
3. Update the "Status" attribute of the related Certificate to "UNDER RENEWAL"

I have found this community post which helped me get started, but I got stuck quickly:

<https://community.atlassian.com/t5/Jira-Service-Management/Automation-loops-with-Assets-Objects/qaq-p/2267068>

Now, what I did (as suggested in the post I linked) was I added a branch on AQL component with the following AQL:  
*objecttype = "SSL Certificates" and "Expiry Date" \< now(30d) AND "Expiry Date" \>= now(-1d)*

This finds the correct objects which I confirmed with a log component.

Now my problem is how do I reference the linked "Host" object where the correct JSM project is stored in an attribute called "Related Project".

If anyone is wondering, our JSM structure is 1 project per client, that is why we have this attribute in Host objects.

Thank you!


Answers:
author: Kevin Patterson
body: Hi [@Danilo Simic](/t5/user/viewprofilepage/user-id/5456489)

If I understand your assets structure it sounds like you have an inbound relationship on your SSL Certificates objects from objects in an object type called Hosts that contains the JSM Project relationship? If my understanding is correct you will need to add a Lookup Objects Action inside of your current Branch on AQL that retrieves the related object. It might look something like this:

***objectType = "Host" AND object HAVING outboundReferences(objectType = "SSL Certificates AND key = "{{object.key}}")***

You might need to adjust the AQL statement above to get the results you are looking for, here is a link to some reference material I use when writing AQL statements: <https://support.atlassian.com/jira-service-management-cloud/docs/use-assets-query-language-aql/>

while inside the branch you'll be able to reference your Related project or any other attribute of your hosts object type via "LookupObject.ATTRIBUTE" in your example I think you'll want to use "LookupObjects.Related Project"

comments: author: Danilo Simic
body: Hello Kevin,

Your understanding is correct.

One concern here, if I use the lookup object as you described, will I get only the one host for the one certificate? I am asking because you are calling ***key = "{{object.key}}"***, it should only use the 1 certificate that is currently running through the branch, correct?


author: Kevin Patterson
body: Hi [@Danilo Simic](/t5/user/viewprofilepage/user-id/5456489)

Yes. Each object that is returned through your branch AQL will have the certificate retrieved by the lookupobjects action . If your end goal is to create an individual ticket for each expiring certificate you will want to have the action that creates the ticket inside the branch. I hope this helps.


author: Danilo Simic
body: Hello,

I was able to find the correct objects using this method. Later I will see if 1 tickets with information from 1 object is created while running the rule for every object found.

Thank you!


author: Danilo Simic
body: Hello [@Kevin Patterson](/t5/user/viewprofilepage/user-id/5497084)

Are you able to also help me with the following?

As I mentioned above, I am able to find the correct objects and get the information that I need, however I am unable to use the object smart values in order to create a ticket in the correct project (or to add some information from the object in the ticket for that matter).

I have this component:

![1.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/334942iBFFBAC264BBBDA8C/image-size/large?v=v2&px=999 "1.PNG")

When I run the rule, the smart value ({{lookupObjects.Related projects}}) finds the correct project but it is not recognized by the Create issue component.

Here is a screenshot from the rule audit log:

![2.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/334943i93588F5C602E4A1E/image-size/large?v=v2&px=999 "2.PNG")

As you can see, the smart value correctly found the project "Admin Test" but the component shows an error.

Similar problem for the Summary of the ticket that I entered in the component.

Thank you!


author: Kevin Patterson
body: Hi [@Danilo Simic](/t5/user/viewprofilepage/user-id/5456489) ,

Sure, happy to help. I've run into issues before programmatically creating issues in different projects with the Create Issue Action. First I would check your {{lookupObject.Related projects}} Is the value in this field the name of the project or the project ID? I believe the value the project field is looking for is the project ID and the asset object attribute you are sending it is the Project Name. You should be able to correct this by using {{lookupObjects.Related projects.key}}.

If that doesn't work you might want to pivot to the Jira Restapi to create your issue. When I build these types of automation rules most of the time I end up just using the Send Web Request Action and specifying the values of the issue in JSON. your Json will look something like the below:

{  
"fields": {  
"project": {  
"key": "{{lookupObject.Related projects.key}}"  
},  
"summary": "Certificate Renewal for {{lookupObjects.Name}}",  
"description": "Description Test",  
"issuetype": {  
"id": "100XX" //**++*Replace this with the actual issue type ID in your environment*++**   

}  
}  
}


author: Danilo Simic
body: Hello [@Kevin Patterson](/t5/user/viewprofilepage/user-id/5497084)

{{lookupObject.Related projects.key}} did not work, but {{lookupObject.Related projects.id}} did work. So the component was looking for the project ID.

Final problem that I am struggling with. How can I use smart values to add the SSL Certificate object in an Asset custom field?

Here is an audit log screenshot of my last attempt:

![1.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/335201i4F3082439B658B73/image-size/large?v=v2&px=999 "1.PNG")

The field is called "SSL Certificate", I have configured it as follows:

![2.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/335202i830C7B89B826531D/image-size/large?v=v2&px=999 "2.PNG")

The field works as intended when I try to manually add a SSL Certificate object.

The smart values I tried to add were these (result was the same):

1. {{lookupObjects.key}}
2. {{lookupObjects.name}}
3. {{lookupObjects}}

I did pay attention not to look at the Business Service, so in order to do this I created a variable which would save one of the 3 smart values I mentioned above.

![3.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/335205i028540046F7EF15D/image-size/large?v=v2&px=999 "3.PNG")

![4.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/335206i395DDAFE99802260/image-size/large?v=v2&px=999 "4.PNG")

If this is not possible I think I will have to use the Send web request component instead of the Create issue component.


author: Danilo Simic
body: Apologies, one more problem that I forgot to mention.

If I have more than 1 related projects, how can I only pick the first one from the list?

![5.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/335209iEFC7466D8FF2BC96/image-size/large?v=v2&px=999 "5.PNG")

In this case I have 2 related projects, AD (Admin Test) and DEMO (Demo project). Is it possible to somehow pick only the first one from the list?


author: Danilo Simic
body: I solved the problem with adding the object in an asset field during issue creation.

The smart values are ok. I was just being stupid with the screens. I added "SSL Certificates" fields in the request type but it never occurred to me to add it to the create screen of the relevant issue type.

The problem when there are multiple projects still remains though.


author: Kevin Patterson
body: For your issue with multiple projects, you might be able to retrieve the project you want with the get method.

I've used get to return a specific object in a list before, but i've never tried using it to retrieve a specific value from an list of attributes of the object. my best guess is the syntax will be something like this:

{{lookupObjects.Related projects.id.get(0)}}


author: Danilo Simic
body: Funny, {{lookupObjects.Related projects.id.get(0)}} gets me the entire list.

While {{lookupObjects.Related projects.id.get(1)}} gets nothing.

I will try to figure it out. Please let me know if you have any other ideas. You have been of massive help, I do not thing I would have managed to do this much without you, thank you!


author: Danilo Simic
body: Ok, it is very stupid for some reason.

***{{lookupObjects.first.Related projects.id.first}}***

This works. ChatGPT suggested this, it sounded like nonsense to me but I gave it a shot, what the hell and it worked.

[@Kevin Patterson](/t5/user/viewprofilepage/user-id/5497084) thank you for all of your help, could not have done it without you!


author: Kevin Patterson
body: No Worries, glad I was able to help. This is the first time I'm seeing ".first" used as a method, I don't recall seeing that in the AQL documentation provided by Atlassian. I'll have to do some of my own research to see what other methods work but are not documented. Thank you for sharing your lookupObjects string with the community!


Tags: assets

automation

cloud

jira-service-management

jira-service-management-cloud