Event Handler Script

Hello world

I am working on an Event Handler-script that should trigger for a spesific category if the request is closed, and should send an email to the customer if the request is not older then 1 month (i compare dateNow to dateCreated). 

Here is my code:

#setLanguageLevel 3;

//Get ticket
Ticket t;
Integer ticketId = getVariable("ticketId").toInteger();
t.load(ticketId);

//Get customer linked to the ticket
Customer c;
Integer customerId = t.getValue("custId").toInteger();
c.load(customerId);

if(t.getValue("category") == "53")  // 53 = the category this script is valid for
{
  //DateCreated and DateToday
  Date ticketDate = DateTime(t.getValue("created_at")).getDate();
  Date currentDate = Date(getCurrentDate());

  //Checks if ticketDate is newer then 'currentDate - 1month'
  if(ticketDate > currentDate.addMonth(-1))
  {
    Email em;
    em.setValue("bodyHtml", getReplyTemplateBody(39, true, -1)); //39 = The replyTemplateId
    em.setValue("to", c.getValue("mainEmail"));
    em.setValue("subject", "Testepost");
    em.send();
  }
}

My question is if this way of sending an email supports using variables (e.g. %ticket.id%), or if i have to edit the template/Html in the script before setting it into the bodyHtml of the email?

Any 'hacks' i can use to make it work? :P

//Eivind

RE: Event Handler Script

You could use a parser to parse the template variables, just send the Ticket and Customer objects to the parser, and it will change the variables automatically

#setLanguageLevel 3;

Integer ticketId = getVariable("ticketId").toInteger();
if(ticketId > 0)
{
  Parser p;

  //Get ticket
  Ticket t;
  t.load(ticketId);
  t.toParser(p);

  Integer customerId = t.getValue("custId").toInteger();

  if(customerId > 0)
  {
    //Get customer linked to the ticket
    Customer c;
    c.load(customerId);
    c.toParser(p);
    String mainEmail = c.getValue("mainEmail");

    if(t.getValue("category") == "53" && mainEmail != "") // 53 = the category this script is valid for
    {
      //DateCreated and DateToday
      Date ticketDate = DateTime(t.getValue("created_at")).getDate();
      Date currentDate = Date(getCurrentDate());

      //Checks if ticketDate is newer then 'currentDate - 1month'
      if(ticketDate > currentDate.addMonth(-1))
      {
        String body = p.parseString(getReplyTemplateBody(39, true, -1));//39 = The replyTemplateId

        Email em;
        em.setValue("bodyHtml", body);
        em.setValue("to", mainEmail);
        em.setValue("subject", "Testepost");
        em.send();
      }
    }
  }
}
Av: Simen Mostuen Iversen 18. okt 2017

RE: Event Handler Script

Ah, brilliant, exactly what I was looking for :)

Thanks!

//Padawan-Eivind


Av: Eivind Johan Fasting 18. okt 2017