logo

NJP

UI ACTION to create a parent incident on the basis of selected records[act as child incidents] from table || Many to One or One to One Relationship

Import · Apr 08, 2022 · article

USE CASE
To create a parent incident on the basis of child incidents selected in the incident table by clicking a button in the banner with many to one relation(i.e. for multiple incidents selected one parent incident to be created).

The button can be placed - List Choice, Banner button, List context menu, List bottom button

Below Article can be used for both one to one & many to one relationship [Child to Parent]

Why I am creating this article?

I was doing the same kind of use case but I didn't get any proper solution, I did find the solution to create one to one relationship but didn't find many to one relationship. Hope this article will help you to go through it. I faced the problem of getting the selected records and tried writing the GlidAjax and many things.

How we are going to achieve it?

As we are creating a button to create many to one relationship between the selected child to one new parent. We are going to create UI Action which will help users to perform our client-side work & script include which will help us to create a new record in our table with the relationship.

1) UI Action
Name: Create Parent

Tabel: Incident

Checkbox Checked: List banner button, Show insert, Show update, Client, List v3 Compatible, List v2 Compatible

OnClick : OnClick();

image

If you have some requirement that only the admin or any other can perform the action, you can mention your condition in the condition box.

Script :

function OnClick() {
    var id;
    var sysid_array = [];
    sysid_array = g_list.getChecked(); //only be used in client side 
    var g_listlength = sysid_array.length;
    if (g_listlength > 0) {
        var usrResponse = confirm('Are you sure you want to make a new parent & make selected reocords as child?'); //User confirmation to create a new parent 
        if (usrResponse == true) {
            var ga = new GlideAjax('MapChildParent');
            ga.addParam('sysparm_name', 'create');
            ga.addParam('sysparm_ids', sysid_array); //sending the sys id of selected records in list view 
            ga.getXML(answer);
            function answer(response) {
                var answer = response.responseXML.documentElement.getAttribute("answer");
                alert("The new Incident is created with " + answer);
            }
        } else {
            return false;
        }
    } else {
        alert("No records has been selected");
    }
}

Till here we are done with the UI action creation. let's move toward our Script include.

2) Script Include

Name: MapChildParent

Client callable: True or checked

Description: To create one-to-one or Many to one relationships.

Script :

var MapChildParent = Class.create();
MapChildParent.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    create: function() {
        var id;
        var ids = this.getParameter('sysparm_ids'); //get the sys if of selected records
        var id_array = ids.split(',');
        var gr = new GlideRecord('incident');
        gr.initialize(); //initialize the object
        gr.short_description = 'parent is created with the selected child records'; //setup the description for the new parent;
        id = gr.insert(); //insert the record
        for (var i = 0; i < id_array.length; i++) {
            var inc = new GlideRecord("incident");
            inc.get(id_array[i].toString());
            inc.parent_incident = id;
            inc.update();
        }
        return gr.number; //return the created parent number
    },
    type: 'MapChildParent'
});

Let's check how it is working now!

The UI Button is ready

image

Let's select the Records & see.....After selecting the records, An alert is popping up for the confirmation.

image

If we will click on cancel it won't proceed further & if we press ok. New Parent Incident has been generated with the new number.

image

Both selected records are under the new parent

image

If the above article helps you, Please Mark Helpful & give your valuable comments for any improvement.

Cheers...!

Happy Learning

Tushar

Labels:

image

View original source

https://www.servicenow.com/community/itsm-articles/ui-action-to-create-a-parent-incident-on-the-basis-of-selected/ta-p/2304043