We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Limit short description to max 40 characters for Projects and Demands

PaulaaO
Mega Sage

Hi Community,

 

I have been asked to configure the fields that represent project and demand names so users would be restricted to setup names longer than 40 characters.

 

The field is short_description and it's a shared field from the extended tables (task for demand and planned_task for project) which means I cannot update the dictionary record.

 

I have created a Client Script and a Business Rule to try and drive the behaviour, however I am struggling to enforce it when I create the project from a template (link: To create project from a template click here.)

 

The Client Script (onChange) is as following (it's similar for Demand and for Project)

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    if (newValue.length > 40) {
        alert(getMessage('Project name must be 40 characters or less.'));
        // Reset the field value to the previous value
        g_form.setValue('short_description', oldValue);
    }
}

 

The Business Rule (before insert) is as following (only for Project)

 

(function executeRule(current, previous /*null when async*/) {

	// check if Project name exceeds 40 characters
	if (current.short_description.length > 40) {
		current.setAbortAction (true);
		gs.addErrorMessage('Project name must be 40 characters or less');
	}

})(current, previous);

 

What am I missing and what do I need to do to make this work?

 

Thank you.

 

Paula

 

 

8 REPLIES 8

Have you added any logs to see if the business rule executes ?

Can I see a screenshot of the business rule, conditions and when to run

 

Run this BR and see if you see anything in the logs

(function executeRule(current, previous /*null when async*/) {
gs.log('(current.short_description.length=> ' + (current.short_description.length );
	// check if Project name exceeds 40 characters
	if (current.short_description.length > 40) {
		current.setAbortAction (true);
		gs.addErrorMessage('Project name must be 40 characters or less');
	}

})(current, previous);

 

 

-Anurag

No, I haven't added logs (not too sure how I would do that). I'm more of a beginner with dev side of things, especially if it involves scripting.

 

Happy to take on any guidance

 

Here you are:

PaulaaO_0-1704819381149.pngPaulaaO_1-1704819396339.png

 

thanks Anurag, however nothing was picked up in the system logs.

 

May I check if the script line is correct, as I had an error?

 

Should it be:

gs.log('current.short_description.length=> ' + current.short_description.length);

 

or 

gs.log('current.short_description.length=> ' + (current.short_description.length));

 

I've tried various versions but with no success

PaulaaO
Mega Sage

I also came across 2 OOTB UI Macros - not sure if maybe these could be configured to incorporate the character limit restriction (or if it's advisable at all)..

 

I've had a go using Bard and Chatgpt, but with no success 😞

 

This is the OOTB version

 

<?xml version="1.0" encoding="utf-8"?>
<j:jelly xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null" trim="false">
	<table width="350px">
		<j:if test="${sysparm_template_action != 'apply_template'}">
			<tr id="project_name">
				<td nowrap="true" data-type="label" class="label label_spacing">
					<label><span class="mandatory_populated label_description">*</span>${gs.getMessage('Project name')} </label>
				</td>
				<td nowrap="true" class="input_controls">
					<input name="project_name" placeholder="${gs.getMessage('Project name')}" autocomplete="off"/>
				</td>
			</tr>
		</j:if>
		<j:if test="${sysparm_has_children != 'true'}">
			<tr id="start_date">
			<td nowrap="true" data-type="label" class="label label_spacing">
				<label><span class="mandatory_populated label_description">*</span>${gs.getMessage('Start Date')} </label>
			</td>
			<td nowrap="true" class="input_controls test">
				<g:ui_date_time name="project_start_date" value="${jvar_start_date}"/>
			</td>
		</tr>
		</j:if>
		<tr id="project_template">
			<td nowrap="true" data-type="label" class="label label_spacing">
				<label><span class="mandatory_populated label_description">*</span>${gs.getMessage('Project template')} </label>
			</td>
			<td nowrap="true" class="input_controls">
				<g:ui_reference name="template_id" table="${sysparm_template_table}" value="${sysparm_template_id}" query="active=true^table=${sysparm_table_name}" displayvalue="${sysparm_template_name}" />
			</td>
		</tr>
		<tr id="dialog_buttons">
			<td align="right" colspan="2">
				<g:dialog_buttons_ok_cancel ok="return actionOK()" ok_id="ok_button" cancel_type="button"/>
			</td>
		</tr>
		<tr><td colspan="2"><br/></td></tr>
	</table>
</j:jelly>