Email notifications - how to get chosen Select Box question Price variable?

jimmy jensen
Kilo Contributor

[CHALLENGE]

I have a catalog item with a select box containing multiple items to choose from, that has a price variable assigned, and I need to template.print() that price in an email notification or an email notification script.

[QUESTION]

How do I go about doing that? I spend most of a day trying to figure it out (dot walk, current.variables, glide records) but I have not been successful, other then getting the total price of all selected question topics on the catalog item by using ${current.price}.

[VARIABLE SETUP]

find_real_file.png

I hope someone can help me out 🙂 I am fairly new to javascript and service-now, but have experience in html5, php, sql

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you will require email script for this

I hope you are aware on how to include email script in email body

Something like this

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

	var variableValue = current.variables.hvilken_model;

	var gr = new GlideRecord("question_choice");
	gr.addQuery("value", variableValue);
	gr.query();
	if (gr.next()) {
		template.print("Price " + gr.misc);
	}

})(current, template, email, email_action, event);

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Harish KM
Kilo Patron
Kilo Patron

Check the answer here

https://community.servicenow.com/community?id=community_question&sys_id=3ec183a9db98dbc01dcaf3231f9619dd

Regards
Harish

Mohith Devatte
Tera Sage
Tera Sage

Hello,

You need to glide record  question choice table to get that price value as price field is on question choice table form 

In the email script please try this script

var gr = new GlideRecord('question_choice');

gr.addQuery('question','your question sys_id'); // compare it with your question 

gr.query();

while(gr.next())

{

var price = gr.misc; // replace it with price field back end name in your instance

}

 

Please accept the solution if youu find it  helpful

Thanks

 

Hi Mohith, 

I love your solution, however I get all my price variables printed for all the items available in the select box, and I only want the one choice that is selected to print.

find_real_file.png

And I am also trying to get the Text variable printed as well, so I can have 

gs.log(+ text + " Price: " + price);

which should look like this when i run it in Scripts - Background to test :

iPhone 13 (128gb) Price: 4.696

find_real_file.png

 

[My script + execution log]

var gr = new GlideRecord('question_choice');
gr.addQuery('question','8a21238c1b481110fceeece5604bcb26'); // compare it with your question 
gr.query();

while(gr.next())
{
var price = gr.misc; // replace it with price field back end name in your instance
var text = gr.text; // I saw you used g.mics for price field and that this matched the table, so I went with gr.text as that seems to be the name of the text variable

gs.log(+ text + " Price: " + price); // prints to log in scripts - background
}


---

[0:00:00.032] Script completed in scope global: script
Script execution history and recovery available here
*** Script: NaN Price: 6552
*** Script: NaN Price: 5407
*** Script: NaN Price: 2776
*** Script: NaN Price: 3096
*** Script: NaN Price: 3928
*** Script: NaN Price: 2080
*** Script: NaN Price: 6999
*** Script: NaN Price: 4575
*** Script: NaN Price: 3847
*** Script: NaN Price: 4696
*** Script: NaN Price: 1273

As you can see I am getting NaN from gr.text value and I get all choice prices from the select box instead of the actively chosen.

 

 

Sagar Pagar
Tera Patron

Hi,

First, you have to get the variable values in email scripts. then you have to glide the Question Choices [question_choice] table.

Query with Value field and get the Price for selected choice.

 

Thanks,

Sagar Pagar

The world works with ServiceNow