Restrict creation of record if one is already present
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2023 02:07 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2023 02:25 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2023 02:28 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2023 02:40 AM - edited 12-29-2023 02:42 AM
Hello @yunus shadman ,
1) Open any incident and right click on the any column of Related List of Incident Task.
Select configure -> List Control:
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;
}
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2023 02:47 AM
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;
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