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

Maik Skoddow
Tera Patron
Tera Patron

Hi @yunus shadman 

sorry, but I cannot recommend this. 

What if, in the future, more than one incident task should be created?

And this related list is not the only place where you can add incident tasks to an incident.

So, if you really want to prevent creating more than one incident task per incident, you should implement a Business Rule.

Maik

Ratnakar7
Mega Sage
Mega Sage

Hi @yunus shadman ,

 

To achieve your requirement and hide the "New" button in the related list if there's already one record present, you can use the "Omit New Condition" script. Here's an example script that you can use:

var answer = false; // Default: Show the "New" button

// Check if there is already at least one record in the related list
var incidentTaskCount = new GlideAggregate('incident_task');
incidentTaskCount.addQuery('incident', parent.sys_id); // Assuming 'incident' is the reference field to the incident in incident_task
incidentTaskCount.addAggregate('COUNT');
incidentTaskCount.query();

if (incidentTaskCount.next() && incidentTaskCount.getAggregate('COUNT') > 0) {
    // If there is at least one incident task record, hide the "New" button
    answer = true;
}

answer;

 

Also, refer Advanced list control with scripts 

 

Thanks,

Ratnakar

Unique45
Mega Sage

Hello @yunus shadman ,

1) Open any incident and right click on the any column of Related List of Incident Task.

Select configure -> List Control: 

Pratiksha_PP_0-1703846144139.png

2) It will open the List control record, Add Following code in the Omit new condition :

code :

var incident = new GlideRecord("incident");
incident.addQuery("sys_id", parent.sys_id);
incident.query();
if((incident.getRowCount()) >= 1){
answer = true;
}

 

Pratiksha_PP_1-1703846351035.png

 

It will hide the New button if there is minimum one incident task is present.

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

Please mark correct/helpful if this helps you!

Aniket Chavan
Tera Sage
Tera Sage

Hello @yunus shadman ,

You can achieve this by using following script in "Omit new condition" and I have tested this in my instance and its working as expected.

 

var hideNewButton = false; // Default: Show the "New" button

// Check if there is already at least one record in the related list
var incidentTaskCount = new GlideAggregate('incident_task');
incidentTaskCount.addQuery('incident', parent.sys_id); // Assuming 'incident' is the reference field to the incident in incident_task
incidentTaskCount.addAggregate('COUNT');
incidentTaskCount.query();

if (incidentTaskCount.next() && incidentTaskCount.getAggregate('COUNT') > 0) {
    // If there is at least one incident task record, hide the "New" button
    hideNewButton = true;
}

hideNewButton;

 

AniketChavan_0-1703846694885.png

AniketChavan_1-1703846809814.png

 

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket