new message on request, system sending 2 emails

Hello, 

I have a customer that wants to enable 'out of office' in Service. They want this auto-reply to be sent when you create a new ticket AND when a new message is created on a existing one. 

I have created a trigger on the 'new message on request'-event, which loads up a template and send an auto-reply to whoever is sending in the message. This event works in both scenarios. 
The script looks like this: 

#setLanguageLevel 3;

Integer ticketId = getVariable("ticketId").toInteger();
Integer messageId = getVariable("messageId").toInteger();
if(ticketId > 0)
{
  Parser p;
  // Get the ticket
  Ticket t;
  t.load(ticketId);
  t.toParser(p);

  Integer customerId = t.getValue("custId").toInteger();
  //Get customer linked to the ticket
  if(customerId > 0)
  {
    
    Customer c;
    c.load(customerId);
    c.toParser(p);
  }
  // Load the message and get the correct email-adress to send the auto-reply to
  Message m;
  m.load(messageId);
  String header = m.getValue("emailHeader");
  Integer createdBy = m.getValue("createdBy").toInteger();
  String mainEmail;

  if (header.getLength()<1 && createdBy>1)
  {
    User u;
    u.load(createdBy);
    mainEmail = u.getValue("email");
  }

  else if (header.getLength()>0)
    mainEmail = header.after("\nFrom: ").before(":").after("<").before(">");
  
  // Load the template
  String body = p.parseString(getReplyTemplateBody(48, true, -1)); //48 = TemplateID for "Julestengt" in the database
  // Send the email
  Email em;
  em.setValue("bodyHtml", body);
  em.setValue("to", mainEmail);
  em.setValue("from", "es@insight-as.suocrm.com");
  em.setValue("subject", "Julestengt!");
  em.send();

}

I have 2 questions:

1. Can i do this through a supermacro instead of script? The auto-reply should not be sent to the owner of the request, but whatever email-adress the message/email comes from. 

2. When i send an email in on the request i get 2 auto-replies back. If i reply on the request in Service it only send 1 (as it should).
Is there a reason why my trigger is 'triggered' 2 times? This happens in both my onsite test-installation and OnlineCRM. 

//Eivind



RE: new message on request, system sending 2 emails

Hi,

Why use "On new message on request" trigger? It will then trigger regardless of who's posting the message.

Why not instead use "System script - Mailbox - after saving message", then you know that it has been sent as from an e-mail externally.

And if it's going to happen on all incomming messages, why not use an e-mail filter instead?

Av: Simen Mostuen Iversen 30. apr 2018

RE: new message on request, system sending 2 emails

Hello, 

Hm, maybe "Mailbox - after saving message" is a better place to put the script. 
I found it easier to create the script under 'triggers' since i can trace it. I am not sure if the variables ticketId and messageId exist in that context?

I thought I could only set up a reply-template to the owner of the ticket, not the person who sent in the email? 
e.g. you have 2 persons replying to 1 ticket, then you only want the one sending in a new message to get the auto-reply, not allways the owner of the ticket.


//Eivind


Av: Eivind Johan Fasting 30. apr 2018