Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Restrict creation of record if one is already present

yunus shadman
Tera Contributor

I have to restrict creation of "Incident_task" record in the related list of incident if one record for the same incident is already present. 

i wanted to achieve this requirement by removing the "New" button after creation of one record. so that no one can create more than one record in "Incident_task" related list . 

Suppose in the below screenshot , i have crated one record for incident task then just after creation i should not be able to see "New" button to create another task. 

yunusshadman_0-1703844373035.png

 

 

5 REPLIES 5

Tai Vu
Kilo Patron
Kilo Patron

Hi @yunus shadman 

Let's define a Business Rule Before Insert.

You can validate if already existed Incident Task for the Incident.

Sample.

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

    var grIncidentTask = new GlideRecord('incident_task');
	grIncidentTask.addNotNullQuery('incident');
    grIncidentTask.addQuery('incident', current.getValue('incident'));
    grIncidentTask.addQuery('sys_id', '!=', current.getUniqueValue());
    grIncidentTask.setLimit(1);
    grIncidentTask.query();
    if (grIncidentTask.next()) {
		gs.addErrorMessage(gs.getMessage("Incident Task already exists for this Incident " + current.getValue('incident')));
		current.setAbortAction(true);
    }

})(current, previous);

 

Cheers,

Tai Vu