How do I create a variable that's default value is text and value of another variable?

Casey Bates
Kilo Contributor

Hey Community!

This is what I am looking to do and would like some guidance:
REQ and RITM titles to equal the value selected from multiple variables on the catalog item plus some text. Lastly, the SCTASKS to equal a value from each variable. I have a sweet example to explain. 

Variable1:

Type: Select Box

Question: Do you want a chocolate or vanilla cake?

Name: Flavor

Choices: Chocolate, Vanilla

 

Variable2:

Type: Select Box

Question: Do you want buttercream or whipped icing?

NameType

Choices: Buttercream, Whipped

 

I want the title of the REQ and RITM to be:

Bake a (flavor) cake with (Type) icing

I want the title of the SCTASKS to be:

Bake a (flavor) cake

Make and finish with (Type) icing

 

So I assume the variable (Title_REQ, Title_RITM, Title_SCTASK) are Single line text (types) and setting the Default value will consist of some js. 

Cheers and thanks in advance.

find_real_file.png

 

 

 

1 ACCEPTED SOLUTION

Brent Sutton
Mega Sage

Hi Casey,

You can achieve this in your item’s workflow. Setup a new workflow on the sc_req_item table and associate it with your catalogue item. The end result will look something like this:

find_real_file.png

 Your workflow will consist of two activities. The first activity will be a run script which will set the short description (I have assumed that this is the title you are referring to) for both the RITM and REQ.

Run Script Activity:

var sd = gs.getMessage("Bake a {0} cake with {1} icing",[current.variables.flavor.getDisplayValue(), current.variables.type.getDisplayValue()]);

current.short_description = sd;
var req = new GlideRecord("sc_request");

if (req.get(current.request)) {
	req.setValue("short_description", sd);
	req.update();
}

find_real_file.png

The second step will be a catalog task activity. The key here is to tick the Advanced option in the Script section. In here we will place the following code:

task.short_description = gs.getMessage("Bake a {0} cake. Make and finish with {1} icing",[current.variables.flavor.getDisplayValue(), current.variables.type.getDisplayValue()]);

find_real_file.png

Now all your short descriptions should contain your personalised text. The code assumes that both of your fields (flavor and type) are mandatory and will always contain values. If they are not mandatory then you will need to add some checks to ensure you cater for when no value has been provided.

find_real_file.png

Let me know if this worked for you. Tested fine in my London developer instance.

Brent

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.

View solution in original post

12 REPLIES 12

Brent Sutton
Mega Sage

Hi Casey,

You can achieve this in your item’s workflow. Setup a new workflow on the sc_req_item table and associate it with your catalogue item. The end result will look something like this:

find_real_file.png

 Your workflow will consist of two activities. The first activity will be a run script which will set the short description (I have assumed that this is the title you are referring to) for both the RITM and REQ.

Run Script Activity:

var sd = gs.getMessage("Bake a {0} cake with {1} icing",[current.variables.flavor.getDisplayValue(), current.variables.type.getDisplayValue()]);

current.short_description = sd;
var req = new GlideRecord("sc_request");

if (req.get(current.request)) {
	req.setValue("short_description", sd);
	req.update();
}

find_real_file.png

The second step will be a catalog task activity. The key here is to tick the Advanced option in the Script section. In here we will place the following code:

task.short_description = gs.getMessage("Bake a {0} cake. Make and finish with {1} icing",[current.variables.flavor.getDisplayValue(), current.variables.type.getDisplayValue()]);

find_real_file.png

Now all your short descriptions should contain your personalised text. The code assumes that both of your fields (flavor and type) are mandatory and will always contain values. If they are not mandatory then you will need to add some checks to ensure you cater for when no value has been provided.

find_real_file.png

Let me know if this worked for you. Tested fine in my London developer instance.

Brent

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.

Casey Bates
Kilo Contributor

This looks promising. It will be towards the end of the week when I can look at this closely based on the work remaining in the current sprint. I appreciate the response Brent and will look at doing this and getting back to you. - Marked the comment helpful for now.

No worries Casey, let me know if you run into any troubles.

Brent

Casey Bates
Kilo Contributor

Hey Brent,

Thanks for being patient. I was receiving an error on the Catalog task and failing to create the SCTASK. The flavor also returned undefined. This is when I learned case matters ;). 'Flavor'instead of 'flavor'

Thanks so much for your support!

If interested, I posted another question here that I would like your thoughts on - both the career advice and if you want to take a stab at my second part. 

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