Custom client script on u_template truncates values at “=” (even after splitting on first “=” only)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
40m ago
Hi everyone,
I need help with a template issue on the Incident form.
Environment
- ServiceNow ITSM, classic UI (the same script also exists for Service Operations Workspace)
- Custom reference field u_template on Incident
- An onChange client script applies the selected template’s values to the form
Problem
When a template is applied through the custom u_template field, any text after an equals sign (=) is dropped.
- In the template’s Description I entered Network Account = Unlock. On the incident, only Network Account appears.
- In the template’s Additional comments I have an HTML link, <a href="https://...">. On the incident, the text stops at <a href.
The out-of-the-box template function does not have this issue. It only happens through the custom field.
What ServiceNow Support found
Support pointed to these lines in the client script. pair.split('=') splits on every = sign, and only entry[1] is kept:
var entry = pair.split('=');
var field = entry[0];
var value = entry[1];
Original script (before my change)
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// 1. Standard safety checks
if (isLoading || newValue === '' || isTemplate) {
return;
}
// 2. Get the current table name (e.g., 'incident', 'problem')
var currentTable = g_form.getTableName();
// 3. Get the template record using a callback
g_form.getReference('u_template', function(tempRecord) {
if (tempRecord) {
// 4. VALIDATION: Ensure the template's table matches the current record's table
if (tempRecord.table !== currentTable && tempRecord.table !== 'task') {
g_form.addErrorMessage('The selected template is for the ' + tempRecord.table + ' table and cannot be applied here.');
return;
}
if (tempRecord.template) {
var templateValues = tempRecord.template;
var pairs = templateValues.split('^');
pairs.forEach(function(pair) {
var entry = pair.split('=');
var field = entry[0];
var value = entry[1];
// 5. Apply values only if the field exists on the current form
if (field && value && g_form.hasField(field)) {
g_form.setValue(field, value);
}
});
g_form.addInfoMessage('Template "' + tempRecord.name + '" applied successfully.');
}
}
});
}
What I changed
I replaced the split('=') lines so the script splits only at the first =:
pairs.forEach(function(pair) {
if (!pair || pair === 'EQ') return;
var idx = pair.indexOf('=');
if (idx === -1) return;
var field = pair.substring(0, idx);
var value = pair.substring(idx + 1);
if (field && value && g_form.hasField(field)) {
g_form.setValue(field, value);
}
});
Result
After saving, clearing the cache and testing on a new incident, I get the same truncation.
Can any one please help me on this -
can any one plesse provide me updated script
NOTE - client want only OOB template field only
@Ankur Bawiskar - need your help here , kindly help me
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
22m ago - last edited 19m ago
Hello @nameisnani
Try below:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// 1. Standard safety checks
if (isLoading || newValue === '' || isTemplate) {
return;
}
// 2. Get the current table name
var currentTable = g_form.getTableName();
// 3. Get the selected template record
g_form.getReference('u_template', function(tempRecord) {
if (!tempRecord || !tempRecord.sys_id) {
return;
}
// 4. Validate that the template applies to the current table
var templateTable = tempRecord.getValue('table');
if (templateTable !== currentTable && templateTable !== 'task') {
g_form.addErrorMessage('The selected template is for the ' + templateTable + ' table and cannot be applied here.');
return;
}
// 5. Get the raw template value
var templateValues = tempRecord.getValue('template');
if (!templateValues) {
return;
}
// 6. Separate the template into individual field/value pairs
var pairs = templateValues.split('^');
pairs.forEach(function(pair) {
// Ignore empty entries and encoded-query terminator
if (!pair || pair === 'EQ') {
return;
}
// 7. Find ONLY the first "="
var idx = pair.indexOf('=');
if (idx === -1) {
return;
}
// Everything before first "=" is the field name
var field = pair.substring(0, idx);
// Everything after first "=" is the value
var value = pair.substring(idx + 1);
// Debugging - remove after testing
console.log('Template field: ' + field);
console.log('Template value: ' + value);
// 8. Apply only fields that exist on the current form
if (field && g_form.hasField(field)) {
g_form.setValue(field, value);
}
});
// 9. Notify user that the template was applied
var templateName = tempRecord.getValue('name');
g_form.addInfoMessage('Template "' + templateName + '" applied successfully.');
});
}
Validation Results:
Hope that helps!
