Adding Work Notes as a Comment to Existing Issues via Incoming Email in Jira Service Management (JSM
Asked by Nithin Revanna:

**Context:** I'm setting up an automation rule in Jira Service Management (JSM) to handle incoming emails. The objective is to check if the email body contains a specific issue key (formatted as `JSM Number: VPLSRV-xxxxx`). If the issue key is found, I want to add the **work notes** from the email as a comment on the existing issue. If no issue key is found, a new issue should be created.

### **Specific Requirements:** {#toc-hId--2124375958}

### **Email Format Example**: {#toc-hId-363136875}

Number: ABC123

JSM Number: ABCD-149

Work_notes: More info added  
![Screenshot 2024-09-20 183701.png](https://community.atlassian.com/t5/image/serverpage/image-id/348337iD5503AD0EC1DBE1D/image-size/large?v=v2&px=999 "Screenshot 2024-09-20 183701.png")

### **Objective**: {#toc-hId--1444317588}

1. **If the email contains an issue key** :
   * I want to check if an issue with that key already exists in JSM.
   * If the issue exists, I want to add the content under `Work_notes:` as a comment on the issue.
   * If the issue doesn't exist, I want to create a new issue.
2. **If no issue key is found in the email** :
   * I need to create a new issue in JSM using the email details.

### **Steps I've Taken So Far**: {#toc-hId-1043195245}

1. **Triggered the Rule** when an incoming email is received.
2. I use a **custom automation rule** to extract the issue key (formatted as `ABCD-xxxxx`) from the email body.
3. I use this issue key to search for an existing issue in JSM.
4. **If an issue is found** , I add the content of `Work_notes` from the email as a comment on the existing issue.
5. **If no issue is found**, I create a new issue with the details from the email.

### **Challenges**: {#toc-hId--764259218}

1. **Issue with Searching for Existing Issue by Key**: When I try to use the extracted issue key to search for the issue in JSM, I'm getting errors related to the search query being incomplete or invalid.
2. **Handling of Empty Issue Key**: I want to make sure that if no issue key is found in the email, the rule falls back to creating a new issue rather than failing.

### **Questions for the Community**: {#toc-hId-1723253615}

1. What is the best way to **extract the issue key** from the email body and use it in automation to check for an existing issue?
2. How can I ensure that the search for an issue by the extracted issue key works reliably?
3. Are there any **best practices** for handling cases where the issue key is missing from the email body?
4. What would be the best approach to **add the work notes** as a comment on the existing issue, once it is found?


Answers:
author: Walter Buggenhout
body: Hi [@Nithin Revanna](/t5/user/viewprofilepage/user-id/4706101) and welcome to the Community!

Could you take a step back for a second and explain how you got to the idea of trying to write an automation rule for this?

You seem to be trying to replicate the behaviour Jira's email handler is doing by default. If an email arrives in a connected mailbox, Jira will check if it references an existing issue and - if so - attach the email body as a comment to that existing issue. If no reference to an issue is found in the email, a new issue will be created.

As far as I know, there is no such thing in Jira automation as a trigger on *email received* . In your screenshot, you reference the *Issue created* event. If this was indeed an issue raised from an email channel, that means that there was no reference to an existing issue found in the first place and everything else you are trying to do after that has no point anymore, since you will never find a related issue in there. Unless of course people add random references to issue keys in emails about other issues. And that would be the part I am interested to understand ?

Hope this helps!

comments: author: Walter Buggenhout
body: Adding to the above; when you want to add a comment to a related issue, you need to use a branch on related issues, not to the current issue. The following frame setup might work (though it would probably require some tweaking for checking the condition):

![Screenshot 2024-09-23 at 08.42.38.png](https://community.atlassian.com/t5/image/serverpage/image-id/348647i13F82807E8F4636C/image-size/large?v=v2&px=999 "Screenshot 2024-09-23 at 08.42.38.png")