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

Hi,

Script include should extend "AbstractAjaxProcessor" class as shown below,

find_real_file.png

Please mark this as Correct or Helpful if it helps.

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

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

Fantastic! Thanks a million, Ankur - this solved the issues!
One minor final question, is there a way to set a return URL using g_navigation ? I could not seem to find one - and action.setReturnURL apparently does not work other than server side ?

 

For future reference - here is the final working code and brief description for those interested:

Functionality:
A button on a Related list of checklist records  (placed on a ritm, inc, problem etc.) creates an incident for the selected row. Checks are validating that only a single row is selected. Then checks if there is an existing Incident for the selected item.

If not, then creates an incident setting the respective fields, relating the incident to the parent record as well as the list record.

UI Action:

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

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

    if (sCount == 0) {
        g_form.addInfoMessage('You must select the Checklist item you want to create an Incident for');
    }

    if (sCount > 1) {
        g_form.addErrorMessage('You cannot select more than one Checklist item when creating an Incident');
    }

    if (sCount == 1) {

        var chkitm = new GlideRecord('u_checklist_item');
        chkitm.get(selected);
        if (chkitm.u_incident !== '') {
            g_form.addErrorMessage('An Incident for this Checklist item already exists. Go to incident ');
        }

        if (chkitm.u_incident == '') {
            var inc = new GlideAjax('ChecklistItemIncident');
            inc.addParam('sysparm_name', 'create');
            inc.addParam('sysparm_category', 'incident');
            inc.addParam('sysparm_sysid', selected);
            inc.getXML(createParse);
        }
    }

    function createParse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        
		var url = '/incident.do?sys_id=' + answer;
        g_navigation.open(url);
    }
}

 

Script include:

var ChecklistItemIncident = Class.create();
ChecklistItemIncident.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    create: function() {

        var sysID;
        var citm = new GlideRecord('u_checklist_item');
        citm.addQuery('sys_id', this.getParameter('sysparm_sysid'));
        citm.query();
		
        if (citm.next()) {
            var inc = new GlideRecord('incident');
            inc.initialize();
            inc.setValue('caller_id', citm.parent.parent.caller_id);
            inc.setValue('category', this.getParameter('sysparm_category'));
            inc.setValue('parent', citm.parent.parent.sys_id);
            inc.setValue('assignment_group', citm.parent.parent.assignment_group);
			inc.setValue('assigned_to', citm.parent.parent.assigned_to);
            inc.setValue('short_description', 'Failed: ' + citm.parent.short_description + ': ' + citm.short_description);
            sysID = inc.insert();
			
			citm.u_incident = sysID;
			citm.state = '6';
			citm.update();
			
        }
        return sysID;
    },

    type: 'ChecklistItemIncident'
});

 

Related list:

find_real_file.png

Hi,

return url won't be possible on client side.

Regards
Ankur

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