logo

NJP

"Set Value from Text" Tool

Import · Oct 14, 2018 · article

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:

Like the other tools, this one grabs the current string in the Text field and populates the "Value" field with a lowercase version it. It will also replace any special characters with an underscore character (anything other than a-z, 0-9).

image

Here are the details of the UI Action record:

Name: Set Value from Text

Table: Question Choice [question_choice]

Order: 100,000

Action name: u_fpc_set_value_from_text

Active: checked

Show insert: checked

Show update: checked

Client: checked

Form link: checked

Hint: Create a string for the Value field based on the Text field (FPC)Messages: u_fpc_set_value_from_text.confirm u_fpc_set_value_from_text.error.field.empty

u_fpc_set_value_from_text.message.populated

Onclick: uFpcSetValueFromText();

Condition: current.canWrite();

Script:

function uFpcSetValueFromText() {
    //hide previous field messages
    try {
        g_form.hideFieldMsg("value", true);
    } catch(err) {}

    //check to see if the Text field is populated or not
    var text = (g_form.getValue("text").toLowerCase()).trim();
    if (text == ""){
        g_form.showFieldMsg("value", getMessage("u_fpc_set_value_from_text.error.field.empty"), "error");
        return;
    }

    //make sure the user wants to replace the Value field
    if (g_form.getValue("value").trim() != ""){
        if (!confirm(getMessage("u_fpc_set_value_from_text.confirm")))
            return;
    }

    var value = (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
    g_form.setValue("value", value);
    g_form.showFieldMsg("value", getMessage("u_fpc_set_value_from_text.message.populated"), "info");
}

I've attached XML files of the UI Action and UI Message records so you can just import it into your instance. As always, try it out in your company's development instance first, or better yet, your own personal development instance.

I've standardized on using UI Messages now for any message visible to users to make it easier to localize the tools.

View original source

https://www.servicenow.com/community/developer-blog/quot-set-value-from-text-quot-tool/ba-p/2291794