I have a crm script on the trigger "Outbound email or SMS created" that overrides the message template to force my customers to log into the self service solution to read the content of the message. This trigger works fine and has been working for a long time, except for one anoying thing: The request occationally does not respect the request status and even if the case worker choses to close the request when replying, the request is still Open. At first sight it looks like it is closed, but when refreshing the page it is apparent that it isstill open. When fetching the Ticket in the CRM script i see that the status is wrong, so I guess this is some kind of race condition.
Any sugestion as to how to fix this? Should I add a sleep() somewhere?
EventData ed = getEventData();
if (ed.getInputValue("outbox.eventName") == "addMessage") {
Integer ticketId = Integer(ed.getInputValue("outbox.ticketId"));
NSTicketAgent ticketAgent;
NSTicketEntity ticket = ticketAgent.GetTicketEntity(ticketId);
NSTicketCategoryEntity c = ticket.GetCategory();
NSTicketStatusEntity status = ticket.GetStatus();
log(status.GetName());
//wrong status is logged
....
ticketAgent.SaveTicketEntity(ticket);
}
Alle Svar (2)
The outbox event is probably triggered by the ticket message save action and could start processing before the ticket save action is done, when you then retrieve the full ticket entity and save it again, you overwrite the status.
Try pulling any data you need from the ticket entity through SearchEngine/ArchiveAgent, and only if you really need to save the ticket entity again, retrieve it at the end of your logic, apply the change and immediately save again. Sadly there is no way to do a PATCH operation natively.
Thanks. I didn't find a good way to optimize the order of things. Not much time consuming stuff happening between the load and the save, so i ended up with this loop. I know that in most cases the ticket should be closed, so I think this would work. I tried to use the GetRepliedAt() as a condition, but this seems to have been changed before the Status so it is updated even for the first fetch.
Integer ticketId = Integer(ed.getInputValue("outbox.ticketId"));
NSTicketAgent ticketAgent;
NSTicketEntity ticket;
for(Integer i = 0; i < 5; i++)
{
ticket = ticketAgent.GetTicketEntity(ticketId);
NSTicketStatusEntity status = ticket.GetStatus();
if(status.GetName()=="Closed")
{
break;
}
sleep(0.1);
}