eumak
Tera Guru

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 v2 Compatible
OnClick : OnClick();

find_real_file.png

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
find_real_file.png

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

find_real_file.png

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.

find_real_file.png

Both selected records are under the new parent
find_real_file.png


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

Cheers...!
Happy Learning
Tushar 

Comments
Murthy Ch
Giga Sage

@Tushar

Nice article:)

Learned the use of g_list.getChecked()

 

Thanks,

Murthy

eumak
Tera Guru

Thanks, @Murthy Chintalapudi ,

I too learned when I was looking for the above use case.

Cheers..!
Tushar

M Lohith Datta
Tera Contributor

Hi Tushar, 

I am grouping the incidents based on CI and your method helped me a lot I am able to group them but here comes the case that if I again want to group other incidents with the same CI then I should get a new pop up stating that already parent exists along with that a button to link and also an option to create new parent.

Kindly help me with this case 

Thanks
M Lohith Datta

eumak
Tera Guru

Hello @M Lohith Datta ,

Good to know that it helped you while working on use case. I will try to replicate the use case explained by you.

Cheers..!
Tushar

M Lohith Datta
Tera Contributor

Hi @Tushar,

After implementing this newly created parent is getting fields copied from the last selected incidents but in my case, it should be like the Parent should copy most of the fields from the latest incident, can you please help me with this case also I want to show a link to open parent incident on the alert box so that user can quickly open the newly created parent incident from list view only.

Please help me with this case

Thanks 

M Lohith Datta

Sriram28
Tera Contributor

Hello @Murthy Ch @eumak @M Lohith Datta 

 

Could you please check if any script for similar requirement:

 

Our  requirement is that we want to create child incident based on all affected CI?

 

For that we have to create a tag in "Action on selected rows" called "create child incident".

 

Once we click on that "create child incident" then it should create child incidents with all the affected CI's.

 

 

 

Sriram28_20-1712315124866.png

 

 

Thanks in advance

Version history
Last update:
‎04-07-2022 11:39 PM
Updated by: