Create and use array in email body in flow designer

s_renjarnbo
Tera Expert

Hi

 

I have created a flow (with subflow) that creates a request item (sc_req_item) for ordering IT Equipment for new hires (triggered from the HR Profiles table).

 

Flow loops through all new hires, finds out if a request for hardware has already been made and if not, create the request. All of this works perfectly.

 

What I would like at the end is to send an email where the body has some information about the requests that has been created, eg

 

RITM00123456 - <<user name1>> - <<hire date1>> - <<manager name1>>

RITM00123457 - <<user name2>> - <<hire date2>> - <<manager name2>>

RITM00123458 - <<user name3>> - <<hire date3>> - <<manager name3>>

 

You get the picture 🙂

 

What I would like to capture is above information for each request created somehow and use that in the email body.

 

I have searched and searched but have not been able to find something useful

 

Kind Regards

Søren

1 ACCEPTED SOLUTION

Rajdeep Ganguly
Mega Guru


Here are the steps you can follow to achieve this:

1. Create a variable to store the information for each request. This could be a string variable that you append to for each new hire processed. For example, you could name this variable emailBody.

2. In your flow, after creating the request item (sc_req_item), append the necessary information to the emailBody variable. You can use the + operator to append strings in ServiceNow. For example:


emailBody = emailBody + 'RITM' + ritmNumber + ' - ' + userName + ' - ' + hireDate + ' - ' + managerName + '\n';


Replace ritmNumber, userName, hireDate, and managerName with the appropriate variables or field values from your flow.

3. After your loop finishes processing all new hires, you can use the emailBody variable in your email notification. In the email notification record, you can insert the variable into the message body using the ${emailBody} syntax.

4. To ensure that the email is sent only once after all the requests have been created, place the email notification action outside the loop, after all the processing is done.

5. Test your flow to ensure that the email is sent correctly and contains the expected information for each request.

Remember to reset the emailBody variable to an empty string at the start of your flow to ensure that it doesn't contain information from previous runs.


nowKB.com

For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - https://nowgpt.ai/

For the ServiceNow Certified System Administrator exams try this :
https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER

View solution in original post

10 REPLIES 10

@s_renjarnbo Following are the supported flow variable types.

 

Screenshot 2024-01-19 at 2.43.51 PM.png

Since JSON is also supported, you can add a key value pair in your flow variable. 

Rajdeep Ganguly
Mega Guru


Here are the steps you can follow to achieve this:

1. Create a variable to store the information for each request. This could be a string variable that you append to for each new hire processed. For example, you could name this variable emailBody.

2. In your flow, after creating the request item (sc_req_item), append the necessary information to the emailBody variable. You can use the + operator to append strings in ServiceNow. For example:


emailBody = emailBody + 'RITM' + ritmNumber + ' - ' + userName + ' - ' + hireDate + ' - ' + managerName + '\n';


Replace ritmNumber, userName, hireDate, and managerName with the appropriate variables or field values from your flow.

3. After your loop finishes processing all new hires, you can use the emailBody variable in your email notification. In the email notification record, you can insert the variable into the message body using the ${emailBody} syntax.

4. To ensure that the email is sent only once after all the requests have been created, place the email notification action outside the loop, after all the processing is done.

5. Test your flow to ensure that the email is sent correctly and contains the expected information for each request.

Remember to reset the emailBody variable to an empty string at the start of your flow to ensure that it doesn't contain information from previous runs.


nowKB.com

For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - https://nowgpt.ai/

For the ServiceNow Certified System Administrator exams try this :
https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER

Thanks @Rajdeep Ganguly 

 

Very helpful 🙂

 

Just a few more questions:

Where should I put the "emailBody = emailBody + 'RITM' + ritmNumber + ' - ' + userName + ' - ' + hireDate + ' - ' + managerName + '\n';" ???
In the scripted data:

s_renjarnbo_0-1705656568046.png

 

or directly in the "text" line:

s_renjarnbo_1-1705656804634.png

Kind Regards
Søren



 

The line of code you provided is used to concatenate or add together strings and variables to form a larger string, which in this case, is used to form the body of an email. This line of code should be placed in a script where you are creating or modifying an email notification in ServiceNow. Here's a general example:

1. Navigate to the Email Notifications module in ServiceNow.
2. Open the email notification you want to modify or click New to create a new one.
3. In the Message HTML field or the Message field, you can use this line of code to form the body of the email.
4. However, you need to ensure that the variables ritmNumber, userName, hireDate, and managerName are defined and have values before this line of code.
5. If these variables are not defined, you can use GlideRecord queries to get the necessary data from the relevant tables in ServiceNow.
6. After defining the variables and forming the email body, you can use the emailBody variable in the body of the email notification.

Please note that this is a general example and the exact steps may vary depending on your specific requirements and the version of ServiceNow you are using.


nowKB.com

For asking ServiceNow-related questions try this :
For a better and more optimistic result, please visit this website. It uses a Chat Generative Pre-Trained Transformer ( GPT ) technology for solving ServiceNow-related issues.
Link - https://nowgpt.ai/

For the ServiceNow Certified System Administrator exams try this :
https://www.udemy.com/course/servicenow-csa-admin-certification-exam-2023/?couponCode=NOW-DEVELOPER

I managed to find a solution based on all the replies in the thread, hence sharing for helping out others if need be.

 

So I have created a flow variable of type string.

In my workflow - after creating the request - I am setting flow variable "emailBody" as below:

s_renjarnbo_0-1705935517134.png

Here is the excact code:


var emailBody = fd_data.flow_var.emailbody;

var RITM = fd_data._15__update_record.record.number;
var requestedFor = fd_data._15__update_record.record.requested_for.name;
var country = fd_data._11__identify_what_to_order.delivery_country;
var countryCode = fd_data.flow_var.country;
var role = fd_data._6__for_each.item.user.u_user_type;
var hiredate = fd_data._6__for_each.item.employment_start_date;
var manager = fd_data._6__for_each.item.user.manager.name;
var laptop = fd_data._11__identify_what_to_order.laptop;;
var iPhone = fd_data._11__identify_what_to_order.mobile;
var iPad = fd_data._11__identify_what_to_order.ipad;
var printer = fd_data._11__identify_what_to_order.home_printer;

emailBody = emailBody + "Request created: " + RITM + " - " + requestedFor +
" - " + country + " - " + countryCode + " - " + role + " - " + hiredate +
" - " + manager +
" - (" + laptop+"/"+iPhone+"/"+iPad+"/"+printer+")" +'\n';
return emailBody;
 
To use this is an email I am simply adding the flow variable in the body of the email:
s_renjarnbo_1-1705935706947.png

Here is the result:

s_renjarnbo_2-1705935752514.png

Hope it help others

 

Regards
Søren