Blog

Adding a Prefix or Suffix to an Existing Field

You can add a Prefix to a field with Number Maintenance.

However what if you want to add a prefix or suffix to existing data or fields.  This script will help you do that!

Remember to run in a development instance and test before running in production ServiceNow instance.

1. Elevate your privilege to security_admin.
2. In the Left Navigator > Scripts - Background
3. Paste this in the Run Scripts area. 

//Replace the function parameters below call with your own needs
addPrefixSuffixToField('prefix','incident','active=true^caller_id=681ccaf9c0a8016234234400b98a06818d57c7','short_description','Urgent: ');

function addPrefixSuffixToField(prefixOrSuffix, myTableName, myQuery, myFieldName, valueToAdd) {
var gr = new GlideRecord(myTableName);
gr.addEncodedQuery(myQuery)
gr.query();
var updateCount = 0;
while (gr.next()) {
if (prefixOrSuffix == 'prefix') {
gr[myFieldName] =valueToAdd+gr[myFieldName];
}
if (prefixOrSuffix == 'suffix') {
gr[myFieldName] = gr[myFieldName]+valueToAdd;
}
gr.setWorkflow(false);
gr.autoSysFields(false);
gr.update();
updateCount++;
}
gs.print('Records Updated: '+ updateCount);
}

4. Run Script