Need help

abjaffrey
Giga Guru

Hi all,

 

I have a scenario:

in incident form, if the priority changes to 1, a new incident should be created with the same details of the current incident but the assigned to should be the 'manager of the assignment group'. the incident is getting created but assigned to is not getting updated.

 

I've written an after BR with conditions.

 

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

    // Add your code here

    var newInc = new GlideRecord('incident');
    newInc.initialize();
    newInc.description = 'This is created as the priority was changed to 1'+'\n'+current.description;
    newInc.assignment_group = current.assignment_group;
    newInc.impact = 1;
    newInc.urgency = 1;
    newInc.caller_id = current.caller_id;
    newInc.category = current.category;
    newInc.short_description = current.short_description;
    // newInc.insert();
    var grpDetails = new GlideRecord('sys_user_group');
    var assignedTo = '';
    grpDetails.addEncodedQuery('nameINNetwork,Hardware,Network CAB Managers');
    grpDetails.query();

    while(grpDetails.next())
    {
        if(grpDetails.name == current.assignment_group)
        {
            assignedTo = grpDetails.manager;
        }
    }
   
    newInc.assigned_to = assignedTo;
    newInc.insert();


})(current, previous);
2 ACCEPTED SOLUTIONS

Abhishek_Thakur
Mega Sage

Hello @abjaffrey ,

 

You can use this to achieve your requirement.

 

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

// Add your code here

var newInc = new GlideRecord('incident');
var assignment = current.assignment_group.manager;
newInc.initialize();
newInc.description = 'This is created as the priority was changed to 1'+'\n'+current.description;
newInc.assignment_group = current.assignment_group;
newInc.impact = 1;
newInc.urgency = 1;
newInc.assign_to = assignment; //This will set the manager in assigned_to field.
newInc.caller_id = current.caller_id;
newInc.category = current.category;
newInc.short_description = current.short_description;
newInc.assigned_to = assignedTo;
newInc.insert();})(current, previous);

View solution in original post

HrishabhKumar
Kilo Sage

Hi @abjaffrey ,

There is a minor mistake in your code, in the following section. I have commented down the correct condition and highlighted it with bold font. Change "grpDetails.name" to "grpDetails.sys_id" and your code should work.

===============================================

  while(grpDetails.next())
    {
        if(grpDetails.name == current.assignment_group)
       //if(grpDetails.sys_id == current.assignment_group)
        {
            assignedTo = grpDetails.manager;
        }
    }
============================================
NOTE:
Additionally you don't even have to Glide the Groups table to get the manager you can directly access it as follows:
current.assignment_group.manager;
 
Hope this solves your issue,
 
Thanks & Regards
HRISHABH KUMAR

View solution in original post

6 REPLIES 6

Sandeep Rajput
Tera Patron
Tera Patron

@abjaffrey Here is the updated script, please see if it works.

 

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

    // Add your code here

    var newInc = new GlideRecord('incident');
    newInc.initialize();
    newInc.description = 'This is created as the priority was changed to 1'+'\n'+current.description;
    newInc.assignment_group = current.assignment_group;
    newInc.impact = 1;
    newInc.urgency = 1;
    newInc.caller_id = current.caller_id;
    newInc.category = current.category;
    newInc.short_description = current.short_description;
    // newInc.insert();
    var grpDetails = new GlideRecord('sys_user_group');
    var assignedTo = '';
    grpDetails.addEncodedQuery('nameINNetwork,Hardware,Network CAB Managers');
    grpDetails.query();

    while(grpDetails.next())
    {
        if(grpDetails.getValue('sys_id') == current.getValue('assignment_group'))
        {
            assignedTo = grpDetails.manager+'';
        }
    }
   
    newInc.assigned_to = assignedTo;
    newInc.insert();


})(current, previous);

Vaishnavi Lathk
Mega Sage
Mega Sage

 

Your script looks mostly correct, but there are a few improvements and corrections needed to ensure that the assigned_to field is properly updated with the manager of the assignment group. Here are the key changes:

  1. Ensure you are comparing the correct fields when matching the assignment_group.
  2. Fetch the manager field value correctly from the sys_user_group record.
  3. Insert the new incident record only after setting all required fields.

Here is the revised script:

 

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

    // Add your code here

    // Create new incident
    var newInc = new GlideRecord('incident');
    newInc.initialize();
    newInc.description = 'This is created as the priority was changed to 1' + '\n' + current.description;
    newInc.assignment_group = current.assignment_group;
    newInc.impact = 1;
    newInc.urgency = 1;
    newInc.caller_id = current.caller_id;
    newInc.category = current.category;
    newInc.short_description = current.short_description;

    // Fetch manager of the assignment group
    var grpDetails = new GlideRecord('sys_user_group');
    if (grpDetails.get(current.assignment_group.sys_id)) {
        var assignedTo = grpDetails.manager;
        newInc.assigned_to = assignedTo;
    }

    newInc.insert();

})(current, previous);