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

Abhijit4
Mega Sage

Does script creating incident successfully, are you having only redirection problem?

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

kheller
Tera Contributor

Hello Abhijit,

No, the incident is not created successfully at this point. 

Regards,
-Ken

 

Script looks completely fine.

Try adding some info message on server side code and see what's happening there.

Thanks and Regards

Abhijit 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Mohit Kaushik
Mega Sage
Mega Sage

Hi Kheller,

Use the below code in script to redirect it to created incident for editing:

var url = '/incident.do?sys_id=inc.sys_id';
action.setRedirectURL(url);

 

Please mark this as correct and helpful as per impact.

 

Thanks,

Mohit Kaushik

 

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)