Question on inbound action creating request

Mani60
Tera Contributor

Hi All,
I have one requirement on creating request via inbound action, here i have written inbound action sc_request table to create a request, please find the below code which i have written:

 

gs.include('validators');
gs.info('Junk - In Junk');
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('030bfcd11b3f6050772853de034bcbb5');
cart.setVariable(item, 'contractor_name', '28f8161e1be68150d9d21f5604bcb54');
cart.setVariable(item, 'manager', '3cf8161e1be68150d9d921f604bcbbd');

cart.setVariable(item, 'request_description', 'ticket created via inbound action');
gs.info('Junk - Placing Order');
var rc = cart.placeOrder();
gs.info('Junk - rc.number = ' + rc.number);
 
this inbound action is working fine request is created but here i want to fetch item variable which have in email body , so anyone please help me on this to achieve the proper solution.
 
Thanks
 
2 REPLIES 2

Ratnakar7
Mega Sage
Mega Sage

Hi @Mani60 ,

 

To fetch item variables from the email body and dynamically create a request in ServiceNow using an inbound action, you can follow these steps:

  1. Parse the email body to extract the item variables.
  2. Use the extracted variables to populate the request item in the cart.
  3. Create the request using the populated cart.

Here's an updated version of your script that includes parsing the email body to extract item variables:

// Parse the email body to extract item variables
var emailBody = current.body_text; // Assuming 'current' is the GlideRecord representing the email record
var contractorName = ''; // Initialize variables
var manager = '';
var requestDescription = '';

// Use regular expressions or other parsing methods to extract item variables from the email body
// Example regular expression to extract contractor name
var contractorNameRegex = /Contractor Name: (.*)/i;
var contractorNameMatch = emailBody.match(contractorNameRegex);
if (contractorNameMatch) {
    contractorName = contractorNameMatch[1];
}

// Similar parsing for other variables like manager and request description

// Create the request item in the cart
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('030bfcd11b3f6050772853de034bcbb5'); // Assuming this is the item ID
cart.setVariable(item, 'contractor_name', contractorName);
cart.setVariable(item, 'manager', manager);
cart.setVariable(item, 'request_description', requestDescription);

// Place the order and create the request
var rc = cart.placeOrder();
gs.info('Request created: ' + rc.number);

 

Replace 'current.body_text' with the appropriate field containing the email body in your environment. Adjust the regular expressions and parsing logic to match the format of the email body where the item variables are specified.

This script assumes that the email body contains specific markers or patterns for each item variable (e.g., "Contractor Name: John Doe"). Adjust the regular expressions and parsing logic as needed based on the actual format of your email body.

 

Thanks,

Ratnakar

Mani60
Tera Contributor

Hi@Ratnakar7 Thanks For Reply

I have try this code but item variable value showing empty.
Please find below snapshot.

Mani60_0-1709557036882.png

Mani60_2-1709557276406.png

 

 

Could you please help me on where i can make mistake.

Thanks,