Jim Coyne
Kilo Patron
Included in the Developer Toolbox Update Set available on Share (link to Share in the post).

 

Another one of those lazy/productivity tools with a similar function:

I usually set the Name field for variables to something very similar to the Question field.  This Related Link UI Action will populate the "Name" field with a lowercase version of the Question field.  It will also replace any special characters with an underscore character (anything other than a-z, 0-9).

 

find_real_file.png

 

Saves on potential typos and a bit of time, especially when you have to create a lot of variables.

 

Here are the details of the UI Action:

Name:         Set Name from Question
Table:        Variable [item_option_new]
Order:        100,000
Action name:  u_fpc_set_name_from_question
Active:       checked
Show insert:  checked
Show update:  checked
Client:       checked
Form link:    checked
Hint:         Creates a string for the Name field based on the Question field (FPC)
Messages:     u_fpc_set_name_from_question.confirm
              u_fpc_set_name_from_question.error.field.empty
              u_fpc_set_name_from_question.message.populated
Onclick:      uFpcSetNameFromQuestion();

Condition:    current.canWrite();
Script:

 

 

 

function uFpcSetNameFromQuestion(){
	if (g_form.getValue("name").trim() != ""){
		if (!confirm(getMessage("u_fpc_set_name_from_question.confirm")))
			return;
	}

	var msg = getMessage("u_fpc_set_name_from_question.error.field.empty");
	var type = "error";
	var value = (g_form.getValue("question_text").toLowerCase().replace(/[^a-z0-9]/g, "_")).trim();
	value = value.replace(/_+/g,"_");   // replaces any one or more instances of "__" with only one "_"
	value = value.replace(/^\_+|\_+$/g, "");   // replaces all leading or trailing "_" with an empty string
	if (value != ""){
		g_form.setValue("name", value);
		msg = getMessage("u_fpc_set_name_from_question.message.populated");
		type = "info";
	}
	g_form.showFieldMsg("name", msg, type);
}

 

 

 

I've attached the XML of the record so you can just import it.  As always, try it out in your company's development instance first, or better yet, your own personal development instance.

 

NOTE:  ServiceNow added an onChange Client Script that auto-populates the Name field when you enter the Question, but only on the initial setting of the Question field, so this UI Action is still handy.