Inbound Email Action - Help Me Please
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi everyone! I'm having trouble with email approval for my ServiceNow registration. I have one Email Script record and one Inbound Email Action record:
Email Script:
(function runMailScript(current, template, email, email_action, event) {
var toAddress = gs.getProperty('glide.email.username'); // ou coloque fixo
var styleApprove = "background:#278ef3;color:#fff;text-decoration:none;padding:12px 25px;border-radius:4px;font-weight:bold;";
var styleReject = "background:#e14332;color:#fff;text-decoration:none;padding:12px 25px;border-radius:4px;font-weight:bold;";
// Pega o sys_id do registro de aprovação
var approvalSysId = "";
if (current.getTableName() === "sysapproval_approver") {
approvalSysId = current.sys_id.toString();
} else {
var gr = new GlideRecord("sysapproval_approver");
gr.addQuery("sysapproval", current.sys_id);
gr.query();
if (gr.next()) {
approvalSysId = gr.sys_id.toString();
}
}
// Inclui sys_id no assunto e no corpo
var subject = encodeURIComponent("Aprovação – Eliminação de Carta de Risco [" + approvalSysId + "]");
var bodyApprove = encodeURIComponent("ACTION:APPROVE\nSYS_ID:" + approvalSysId);
var bodyReject = encodeURIComponent("ACTION:REJECT\nSYS_ID:" + approvalSysId);
var approveHref = "mailto:" + toAddress + "?subject=" + subject + "&body=" + bodyApprove;
var rejectHref = "mailto:" + toAddress + "?subject=" + subject + "&body=" + bodyReject;
template.print(
'<table style="margin:20px 0;"><tr>' +
'<td style="padding-right:20px;">' +
'<a href="' + approveHref + '" style="' + styleApprove + '">Aprovar</a>' +
'</td>' +
'<td>' +
'<a href="' + rejectHref + '" style="' + styleReject + '">Rejeitar</a>' +
'</td>' +
'</tr></table>'
);
})(current, template, email, email_action, event);
Inbound Email Action:
(function runInboundAction(email, action, event, logger) {
var body = (email.body_text || "").toUpperCase();
var sysIdMatch = body.match(/SYS_ID:([0-9A-F]+)/);
if (!sysIdMatch) {
logger.log("Nenhum SYS_ID encontrado no corpo do e-mail.");
return;
}
var sysId = sysIdMatch[1];
var gr = new GlideRecord("sysapproval_approver");
if (gr.get(sysId)) {
if (body.indexOf("ACTION:APPROVE") > -1) {
gr.state = 2; // Approved
gr.comments = "Aprovado via e-mail";
} else if (body.indexOf("ACTION:REJECT") > -1) {
gr.state = 3; // Rejected
gr.comments = "Rejeitado via e-mail";
}
gr.update();
logger.log("Ação executada para sys_id: " + sysId + " | Novo estado: " + gr.state);
} else {
logger.log("Registro não encontrado para sys_id: " + sysId);
}
})(email, action, event, logger);
I've tried several methods in ServiceNow, sending out emails that meet the requirements, but the registry isn't updating. Can anyone help me?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
registry is't updating for a record /email/ approval ? also share error's..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
This already exists oob, see the "Approval request" notification and the "Update Approval Request" inbound action. The scripted inbound actions automatically parse the key:value format into an object which you can access by body.key in the script. You don't need to include the sysid for the approval record as the email engine handles getting the correct record for you. Using current instead of querying a record also makes sure acls are applied.
Other than that if the query in your inbound action returns a record then it should only be a matter of using the appropriate values for the state field. They should be "approved" and "rejected" not numbers.