Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Throw an error message if two INCTASK has same assignment group under the Parent Incident

SanikaK
Tera Contributor

Requirement:
For a given Parent Incident, the system must ensure that no two Incident Tasks (INCTASK) have the same Assignment Group.

Behavior:

  • When a user attempts to create or update an Incident Task with an Assignment Group that already exists on another Incident Task under the same Parent Incident, the system must:
    • Prevent the record from being saved.
    • Display an error message informing the user that an Incident Task already exists for that Assignment Group under the same Parent Incident.
    • Display the existing Incident Task number and link in the error message.


4 REPLIES 4

Huynh Loc
Mega Sage

Hello @SanikaK ,

Please implement a Before Insert Business Rule that checks whether an Incident Task (INCTASK) with the same Assignment Group already exists under the same Parent Incident. If a duplicate is found, display an error message and abort the insert action.

Example:
/ Ensure Parent Incident and Assignment Group are present
    if (!current.incident || !current.assignment_group) {
        return;
    }

 

    // Check for an existing incident task with same Incident and Assignment Group
    var incTaskGR = new GlideRecord('incident_task');
    incTaskGR.addQuery('incident', current.incident);
    incTaskGR.addQuery('assignment_group', current.assignment_group);
    incTaskGR.query();

 

    if (incTaskGR.hasNext()) {
        gs.addErrorMessage(
            'An Incident Task with the same Assignment Group already exists for this Incident.'
        );
        current.setAbortAction(true);
    }



If this response was helpful, please consider marking it as Correct and Helpful. You may mark more than one reply as an accepted solution.



Ankur Bawiskar
Tera Patron

@SanikaK 

this is an easy requirement.

what did you start and where are you stuck?

Unless you start you won't learn.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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

if (!current.parent || !current.assignment_group) {
return;
}

var gr = new GlideRecord('u_incident_task');
gr.addQuery('parent', current.parent);
gr.addQuery('assignment_group', current.assignment_group);
gr.addQuery('sys_id', '!=', current.sys_id); // exclude current record
gr.query();

if (gr.next()) {

var instanceURL = gs.getProperty('glide.servlet.uri');
var link = instanceURL + 'incident_task.do?sys_id=' + gr.getUniqueValue();

var msg = "An Incident Task already exists for this Assignment Group.\n";
msg += "Task Number: " + gr.getValue('number') + "\n";
msg += "Link: " + link;

gs.addErrorMessage(msg);

current.setAbortAction(true); // 

}

})(current, previous);

I have written this code but not working

Tanushree Maiti
Kilo Patron

Hi @SanikaK 

 

You can start with a Before Insert/Update Business Rule on the incident_task  table. This approach ensures the validation occurs server-side before the record is saved to the DB.

 

Trigger condition could be:    

Assignment Group - Changes OR

Parent - Changes

 

Sample code :

 

 if (current.assignment_group.nil() || current.incident.nil()) {
        return;
    }
   var incTask = new GlideRecord('incident_task');
    incTask.addQuery('incident', current.incident); // parent    incTask.addQuery('assignment_group', current.assignment_group);      
   incTask.query();
        if (incTask.next()) {
        var message = 'An Incident Task already exists for this Assignment Group (' + incTask.assignment_group.getDisplayValue() + ') under the Parent Incident';
        gs.addErrorMessage(message);
        current.setAbortAction(true);     }

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: