RITM created from email with inbound action does not bring variables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-21-2024 09:49 PM
I created the following script in an inbound email action, with the intention of creating a RITM record from an email
gs.log('entrou no inbound action', 'teste inbound 1');
createRequest();
function createRequest() {
var body = email.body_html;
//Create Request
var grRequest = new GlideRecord("sc_request");
grRequest.initialize();
//substitute your requested for
grRequest.requested_for = email.user_id; //sys_id do user que criou o email
grRequest.short_description = email.subject.toString();
grRequest.description = "received from: " + email.origemail + "\n\n" + email.body_text;
var requestSysId = grRequest.insert();
//Create Request Item, substitute your requested for
current.requested_for = email.user_id; //sys_id do user que criou o email
current.short_description = email.subject.toString();
current.description = "received from: " + email.origemail + "\n\n" + email.body_text;
//substitute your cat item
current.cat_item = 'b34889ca472202104cd013ff016d431a'; //sys_id do cat item , nesse caso é o catalog item de teste
current.parent = requestSysId;
current.request = requestSysId;
//preenchimento das variáveis
//----------------------------------------------------------------------------------------------------------
//corpo do email guardado em variavel/ email body set in a var
var htmlcode = body;
// remove all the HTML tags from the HTML body
htmlcode = htmlcode.replace(/<style([\s\S]*?)<\/style>/gi, '');
htmlcode = htmlcode.replace(/<script([\s\S]*?)<\/script>/gi, '');
htmlcode = htmlcode.replace(/<\/div>/ig, '\n');
htmlcode = htmlcode.replace(/<\/li>/ig, '\n');
htmlcode = htmlcode.replace(/<li>/ig, ' * ');
htmlcode = htmlcode.replace(/<\/ul>/ig, '\n');
htmlcode = htmlcode.replace(/<\/p>/ig, '\n');
htmlcode = htmlcode.replace(/<br\s*[\/]?>/gi, "\n");
htmlcode = htmlcode.replace(/<[^>]+>/ig, '');
htmlcode = htmlcode.replace(/\r?\n|\r/g, '\n');
// Dividir o texto em linhas / divide text in lines
var lines = htmlcode.split('\n');
// Inicializar variáveis para armazenar os valores extraídos / initialize set cat item variables values got from html email body
var variavel_1_de_teste_insira_infos_aquiInEmail = '';
var first_taskInEmail = '';
// Iterar sobre as linhas para extrair os valores após os dois pontos / iterate over the rows to extract the values after the colon
lines.forEach(function(line) {
if (line.includes('(variavel 1 de teste) - insira infos aqui:')) {
variavel_1_de_teste_insira_infos_aquiInEmail = line.split('(variavel 1 de teste) - insira infos aqui:')[1].trim();
} else if (line.includes('first task:')) {
first_taskInEmail = line.split('first task:')[1].trim();
}
});
var storeFirst_taskInEmail = current.variables.first_task = first_taskInEmail;
var storeVariavel_1_de_teste_insira_infos_aquiInEmail = current.variables.variavel_1_de_teste_insira_infos_aqui = variavel_1_de_teste_insira_infos_aquiInEmail;
var requestedFor = current.variables.requested_for = email.user_id; //sys_id do user que criou o email;
current.insert();
//Workflow Trigger
//More information http://wiki.servicenow.com/index.php?title=Workflow_Script
var w = new Workflow();
wfSysId = w.getWorkflowFromName("Service Catalog Item Request");
w.startFlow(wfSysId, current, current.operation());
// Imprimir os valores extraídos/ log the values
gs.log(variavel_1_de_teste_insira_infos_aquiInEmail, 'log de inbound p v valor da var varDeTeste esta sendo tratado após ser pego na html');
gs.log(first_taskInEmail, ' log de inbound p v valor da var firstTask esta sendo tratado após ser pego na html');
gs.log(htmlcode, 'log de inbound para ver como html está sendo tratado após ser passar pelos regex e ser retirada suas tags');
//logs para validações /logs for validation
gs.log(storeFirst_taskInEmail, 'teste inbound 2 var firstTaskVar do cat item');
gs.log(storeVariavel_1_de_teste_insira_infos_aquiInEmail, 'teste inbound 3 var varDeTest do cat item');
gs.log(requestSysId, 'teste inbound 4 sysid do req');
gs.log(requestedFor, 'teste inbound 5 var req for do cat item');
}
and it's working, the records are being crated , but when I go to visualize the record, it does not shows neither the workflow nor the variables, could you please help me? What am I missing?
This is how the records created from an email are
And this is how it suppose to be
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2024 12:17 AM
Hi @Erlan ,
I have done some small modifications in your script and pasted it below. Please consider the following adjustments and check after implementing the code:
- Field Names: Ensure the variable names in the catalog item (like u_first_task, u_variavel_1_de_teste_insira_infos_aqui) are correct. Replace them with the actual variable names from your catalog item.
- Logging: Continue to use gs.log to debug and verify that values are being set correctly.
- Workflow Association: Ensure that the workflow name "Service Catalog Item Request" is correct. Adjust it if your workflow has a different name.
gs.log('entrou no inbound action', 'teste inbound 1');
createRequest();
function createRequest() {
var body = email.body_html;
// Create Request
var grRequest = new GlideRecord("sc_request");
grRequest.initialize();
// Substitute your requested for
grRequest.requested_for = email.user_id; // sys_id do user que criou o email
grRequest.short_description = email.subject.toString();
grRequest.description = "received from: " + email.origemail + "\n\n" + email.body_text;
var requestSysId = grRequest.insert();
// Create Request Item, substitute your requested for
var grItem = new GlideRecord('sc_req_item');
grItem.initialize();
grItem.requested_for = email.user_id; // sys_id do user que criou o email
grItem.short_description = email.subject.toString();
grItem.description = "received from: " + email.origemail + "\n\n" + email.body_text;
grItem.cat_item = 'b34889ca472202104cd013ff016d431a'; // sys_id do cat item
grItem.parent = requestSysId;
grItem.request = requestSysId;
// Process email body to extract variable values
var htmlcode = body;
htmlcode = htmlcode.replace(/<style([\s\S]*?)<\/style>/gi, '');
htmlcode = htmlcode.replace(/<script([\s\S]*?)<\/script>/gi, '');
htmlcode = htmlcode.replace(/<\/div>/ig, '\n');
htmlcode = htmlcode.replace(/<\/li>/ig, '\n');
htmlcode = htmlcode.replace(/<li>/ig, ' * ');
htmlcode = htmlcode.replace(/<\/ul>/ig, '\n');
htmlcode = htmlcode.replace(/<\/p>/ig, '\n');
htmlcode = htmlcode.replace(/<br\s*[\/]?>/gi, "\n");
htmlcode = htmlcode.replace(/<[^>]+>/ig, '');
htmlcode = htmlcode.replace(/\r?\n|\r/g, '\n');
var lines = htmlcode.split('\n');
var variavel_1_de_teste_insira_infos_aquiInEmail = '';
var first_taskInEmail = '';
lines.forEach(function(line) {
if (line.includes('(variavel 1 de teste) - insira infos aqui:')) {
variavel_1_de_teste_insira_infos_aquiInEmail = line.split('(variavel 1 de teste) - insira infos aqui:')[1].trim();
} else if (line.includes('first task:')) {
first_taskInEmail = line.split('first task:')[1].trim();
}
});
// Set variables
grItem.setValue('u_first_task', first_taskInEmail);
grItem.setValue('u_variavel_1_de_teste_insira_infos_aqui', variavel_1_de_teste_insira_infos_aquiInEmail);
grItem.setValue('u_requested_for', email.user_id);
var ritmSysId = grItem.insert();
// Workflow Trigger
var w = new Workflow();
wfSysId = w.getWorkflowFromName("Service Catalog Item Request");
w.startFlow(wfSysId, grItem, grItem.operation());
// Log values for debugging
gs.log(variavel_1_de_teste_insira_infos_aquiInEmail, 'log de inbound p v valor da var varDeTeste esta sendo tratado após ser pego na html');
gs.log(first_taskInEmail, ' log de inbound p v valor da var firstTask esta sendo tratado após ser pego na html');
gs.log(htmlcode, 'log de inbound para ver como html está sendo tratado após ser passar pelos regex e ser retirada suas tags');
gs.log(requestSysId, 'teste inbound 4 sysid do req');
gs.log(ritmSysId, 'teste inbound 5 sysid do ritm');
}
Thanks,
Hope this helps.
If my response proves helpful please mark it helpful and accept it as solution to close this thread.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2024 12:41 AM
Hi @Erlan,
First of all, I would recommend using a Flow instead. It just makes it much easier without any scripting (or minimal).
e.g.
If you insist on using the Inbound Action, you shouldn't be using GlideRecord API to generate a Request/RITM.
Instead, use the CartJS API - https://docs.servicenow.com/bundle/washingtondc-api-reference/page/app-store/dev_portal/API_referenc...
Looks like you know your way around scripting so I won't go into the details but I believe you should be using the orderNow function.
And do not trigger your Workflow via a script, if the Workflow is attached to the Catalog Item, it should automatically be triggered.
Cheers