Forums

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

can I use javascript inside html in automated email?

Danielle Cambridge November 13, 2024
I found this super helpful article in the forum to help write automation to send out the Customer Survey based on a specific resolution:
Current setup:
  • Customer Notification Request Resolved: turned off
  • Workflow fires when Status --> Completed
    • Open Screen:
      • Resolution (required)
      • Comment
  • Automation written that:
    • Fires upon "Done" resolution
    • Sends an email to the Customer that has:
      • Which Agent closed it
      • Last comment noted (this is the issue)
      • Boilerplate response/info
      • Survey
As the automation fires AFTER the Workflow, the above all works, EXCEPT :
  • If the Agent write a comment in the Completed Screen, everything works perfectly.
  • If the Agent does NOT write a comment, the code grabs the last comment from history (based off its name, this is expected behavior). The problem is that the last comment might be irrelevant and thus confusing.
 
Possible solutions:
  • add language to email that says “Last comment made by Agent (you might have already seen):”(clunky; not ideal)
  • making sure the Agent writes their final comments in the resolution screen and not in the body comments section (prone to human error, relies on education; not ideal)
  • Require the comments section on the Completed Screen (applies to ALL resolutions; do not want a comment when closing a ticket with a "duplicate" resolution; no ideal)
  • Add code to compare timeStamp of last comment to current time and if within determined range (say 30 seconds) then output the comment, if not don't. (ideal) 
After researching it appears that javascript can be embedded in html code to perform actions.
However, I cannot get it to work. It could completely be user error (I am a total newb at this) and I missed something. OR javascript in html in an automated email script doesn't work. I did see one comment that it's not a good idea in general to embed javascript in html, so maybe I need to abandon this wish regardless.

 

Culled down to what you need, here is the code:
<pre><code>
<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <base href="removed">
 </head>
 <body>
  <div class="jsm-message-content" style="margin-bottom: 30px">
    <div class="jsd-activity-item-content">
        <p>Your Agent: {{assignee.displayName}} changed the status to Done.</p>
        <p>Last comment from history: {{issue.comments.last.body}} </p>
       <p>Timestamp of last comment from history: {{issue.comments.last.created}} </p>
       <p>script output should be here:</p>
<script>
        window.onload = function() {
            // Simulate the {{issue.comments.last.created}} variable (replace this with the actual value in your context)
            var commentTime = "{{issue.comments.last.created}}"; // This will be replaced by the actual timestamp
            var commentDate = new Date(commentTime);
            // Get current time
            var currentTime = new Date();
            // Calculate the difference in seconds
            var timeDiffInSeconds = (currentTime - commentDate) / 1000;
            // Check if the difference is less than 30 seconds
            if (timeDiffInSeconds < 30) {
                document.getElementById('result').innerText = "Comment entered on closure";
            } else {
                document.getElementById('result').innerText = "Comment not entered within 30 seconds of closure";
            }
        };
    </script>
  <div id="result"></div>
    </div>
 </body>
</html>
</code></pre>
Here's the output:

----

Your Agent: AgentNameRemoved changed the status to Done.

Last comment from history: comment entered in resolution screen

Timestamp of last comment from history: 2024-11-13T21:18:33.0+0000

script output should be here:

----
No output after that last line.
I have read the timestamp isn't the right format, but I replaced it with the hardcoded proper format and still nothing.
Any other ideas? Or will this just not work or is not allowed?
Thanks for any advice!

1 answer

1 accepted

1 vote
Answer accepted
Danielle Cambridge November 13, 2024

Answering my own question and putting here for others to learn from my mistakes/efforts!

Despite a few hours of research, trials, and using chatGPT every which way, only when I asked it to test the code differently did it confirm that jsm automation won't allow the java code and it proposed an alternate way to achieve what I want:

I now use an If/Then statement (in the automation) that does the comparison:

  • Condition: Advanced Compare Condition
    • First value: {{issue.comments.last.created}}
    • Condition: greater than
    • Second value: {{issue.resolutionDate}}

This ensures that the last comment was added after the resolution date, meaning it was added during or after the resolution process.

And it worked!

 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events