UI Action with client and server scrip to create incident from related list

kheller
Tera Contributor

Hi all,

I have an issue I can't quite figure out.
I need to create a UI Action on a related list to create a new Incident and return back to the form when saved. However, I need to check the selected records first before creating to ensure only one record is selected, as I do not want to create multiple Incidents simultaneously..

I have the following code and it works to verify that I only have a single selection. However, the creation of an incident record and opening that for editing, does not work. When I first wrote the code to create the incident, but without checking the row selection, it worked fine. After rewriting it to get the client side check of selected records - it just refreshes my page.:

find_real_file.png

Script:

function verifySelect() {
    var sCount = 0;
    var selected = g_list.getChecked();

    if (selected.length > 0) {
        sCount = selected.split(',').length;
    }

    if (sCount > 1) {
        alert('You cannot select more than 1 item when creating an Incident');
    }

    if (sCount == 1) {
        gsftSubmit(null, g_form.getFormElement(), 'cr_inc');
    }
}

if (typeof window == 'undefined')
    createIncident();

function createIncident() {
    var inc = new GlideRecord('incident');
    inc.initialize();
    inc.caller_id = current.parent.parent.assigned_to;
    inc.category = 'incident';
    inc.parent = current.parent.parent.sys_id;
    inc.short_description = 'Failed: ' + current.parent.short_description + ': ' + current.short_description;
    inc.u_is_template = current.u_is_template;
    inc.insert();

    current.u_incident = inc.sys_id;
    current.state = '6';
    current.update();

    action.setRedirectURL(inc);
    action.setReturnURL(current.parent.parent);
}

Any help in pointing me in the right direction would be greatly appreciated!

Regards,
-Ken

1 ACCEPTED SOLUTION

Hi,

you cannot dot walk multiple levels in client side

so send only parent and then query and get the details

Something like this but please enhance from your side

Script Include:

var ChecklistItemIncident = Class.create();
ChecklistItemIncident.prototype = {
	create: function() {

		var gr = new GlideRecord("table"); // give the table name here which is referred by parent field
		gr.addQuery("sys_id", this.getParameter('sysparm_parent'));
		gr.query();
		if (gr.next()) {
			var inc = new GlideRecord('incident');
			inc.initialize();
			gr.setValue('caller_id', this.getParameter('sysparm_caller_id'));
			gr.setValue('category', this.getParameter('sysparm_category'));
			gr.setValue('parent', this.getParameter('sysparm_parent'));
			gr.setValue('assignment_group', gr.parent.assignment_group);
			gr.setValue('short_description', 'Failed: ' + gr.parent.short_description + ': ' + this.getParameter('sysparm_short_description'));
			var sysID = inc.insert();
			return sysID;  

		}
	},

	type: 'ChecklistItemIncident'
};

Client Side:

if (sCount == 1) {
	var inc = new GlideAjax('ChecklistItemIncident');
	inc.addParam('sysparm_name', 'create');
	inc.addParam('sysparm_category', 'incident');
	inc.addParam('sysparm_parent', g_form.getValue('parent'));
	inc.addParam('sysparm_short_description', g_form.getValue('short_description'));
	inc.getXML(createParse);

	//gsftSubmit(null, g_form.getFormElement(), 'cr_inc');
}

function createParse(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	g_form.addInfoMessage(answer);
}

Regards
Ankur

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

View solution in original post

13 REPLIES 13

Hello Mohit and thanks for your input.
Unfortunately this did not solve my issue.

Regards,
-Ken

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you want to populate incident with field data of u_checklist_item?

u_is_template is field on u_checklist_item table?

Regards
Ankur

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

Hello Ankur,

Yes, I do want to populate the incident with data from u_checklist_item, and yes, u_is_template is a field on this table. However, I actually can skip setting this field now that you mention it - so I commented it out, but the problem remains the same.

Regards,
-Ken

try to use GlideAjax and keep the UI action as client side only

function verifySelect() {
	var sCount = 0;
	var selected = g_list.getChecked();

	if (selected.length > 0) {
		sCount = selected.split(',').length;
	}

	if (sCount > 1) {
		alert('You cannot select more than 1 item when creating an Incident');
	}

	if (sCount == 1) {
		// GlideAjax here
		// get the field values using g_form object
	}
}

Regards
Ankur

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

Hi again Ankur,

That sounds like a good approach!

However, Im not having any luck so far - would you care to have a look at my code?

Script include:

(In Global scope, Client callable)

var ChecklistItemIncident = Class.create();
ChecklistItemIncident.prototype = {
    create: function() {
        var inc = new GlideRecord('incident');
        inc.initialize();
        gr.setValue('caller_id', this.getParameter('sysparm_caller_id'));
        gr.setValue('category', this.getParameter('sysparm_category'));
        gr.setValue('parent', this.getParameter('sysparm_parent'));
        gr.setValue('assignment_group', this.getParameter('sysparm_assignment_group'));
        gr.setValue('short_description', this.getParameter('sysparm_short_description'));
        var sysID = inc.insert();
		
        return sysID;  
    },

    type: 'ChecklistItemIncident'
};

 

Updated UI Action:

 if (sCount == 1) {
        var inc = new GlideAjax('ChecklistItemIncident');
        inc.addParam('sysparm_name', 'create');
        inc.addParam('sysparm_caller_id', g_form.getValue('current.parent.parent.assigned_to'));
        inc.addParam('sysparm_category', 'incident');
        inc.addParam('sysparm_parent', g_form.getValue('current.parent.parent.sys_id'));
        inc.addParam('sysparm_assignment_group', g_form.getValue('current.parent.parent.assignment_group'));
        inc.addParam('sysparm_short_description', 'Failed: ' + g_form.getValue('current.parent.short_description' + ': ' + g_form.getValue('current.short_description')));
        inc.getXML(createParse);

        //gsftSubmit(null, g_form.getFormElement(), 'cr_inc');
    }

    function createParse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		g_form.addInfoMessage(answer);
    }

 

At this point the insert of the Incident is just not happening, the blank Info message is shown. Upon clicking the UI Action button. I even tried to stripping it down to the bare minimum with removing the field assignments - but nothing..

Any suggestion is greatly appreciated.

Regards,
-Ken