UI Action on custom table to create incident, need to return incident.number
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-16-2013 12:02 PM
I have a custom table/form () with a UI Action that creates an incident whenever clicked. Neither the task nor incident tables are extended to this table. Everything regarding the incident creation works great. All fields are inserted into the incident and the incident is created. Only problem is, I'm unable to return the incident number that was created back to the form using 'g_form.addInfoMessage'
Using the code excerpt you see below, I get 'Incident [object Object] created.',
//creates incident and injects into Incident table
var incident = new GlideRecord('incident');
<SOME CODE THAT ISNT RELEVANT>
var sysID = incident.insert();
g_form.addInfoMessage("Incident " + incident + " created.");
If I change the addInfoMessage to the below, I receive 'Incident undefined created.'
g_form.addInfoMessage("Incident " + incident.number + " created.");
What simple thing am I missing here? Is the fact that I did not extend the task or incident table out causing this?
Any help is appreciated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-17-2013 04:07 AM
So there seems to be a problem with Number field itself. First of all, As you said its a custom table where you are inserting the record so make sure the number field has the database name is "number", may be it start from "u_".
Second, If you notice that Number field on tables like Problem or Incident auto populates by itself whenever a new record is inserted. So Make sure you have the same functionality for Number Field in your custom table as well.
I have gave it a try on demo010 and is working fine. Created a custom table u_tester16 and a ui action on incident form named "create tester" which creates a record on tester table and return the Number(u_number)'s value present in the tester16 table for the newly inserted record.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-17-2013 03:34 PM
Not sure if I described the set up properly.
I don't actually have a 'number' field on my custom table. I'm trying to create an incident on the 'Incident' table using '
'. That newly created incident will obviously have an incident number assigned to it. That incident number is what I'm trying to pull back and display on my custom form.
var incident = new GlideRecord('incident');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-17-2013 11:44 PM
tried incident.number, but 'undefined' was returned. Here is the UI Action (new at this, pardon the amateur coding):
Client: true
Onclick: createincident()
function createincident() {
//gather info + verify all required fields are set before incident creation
var status = g_form.getValue('u_laptop_status');
var prep = g_form.getValue('u_prepared_by');
var user = g_form.getValue('u_checked_out_to');
var dateout = g_form.getValue('u_date_checked_out');
var dateback = g_form.getValue('u_date_due_back');
var reason = g_form.getValue('u_business_reason');
if (status == 'Checked Out' && prep != '' && to != '' && dateout != '' && dateback != '' && reason != '')
{
// calculate truncated times for incident description
var length = 10;
var truncatedout = dateout.substring(0,length);
var truncatedback = dateback.substring(0,length);
// get values from loaner form fields to put in the incident
var laptop = g_form.getValue('u_laptop_name');
var tech = g_form.getValue('u_prepared_by');
var description = "Checked Out To - " + user + "\nChecked Out - " + truncatedout + "\nDue Back - " + truncatedback + "\nReason - " + reason;
//creates incident and injects into Incident table
var incident = new GlideRecord('incident');
incident.priorty = '3';
incident.impact = '3';
incident.category = 'Computer';
incident.subcategory = 'Windows Platform - Hardware';
incident.u_action = 'Loaner';
incident.caller_id = user;
incident.due_date = dateback;
incident.short_description = laptop + ' checked out to ' + user;
incident.assignment_group = 'SEC-SN-ITSERVICEDESK';
incident.assigned_to = tech;
incident.description = description;
incident.contact_type = 'in-person';
incident.state = 6;
incident.close_code = 'Known Solution';
incident.close_notes = "laptop configured and given to user";
var sysID = incident.insert();
g_form.addInfoMessage("Incident " + incident + " created. Please click on Save or Update to finish checking out this laptop.");
}
else
{
alert("You cannot check out a laptop unless all required fields are filled out. Make sure to set the status to 'Checked Out' when ready");
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-18-2013 10:11 AM
Well I have revised your ui action's script which is as follows. for Demo purpose, I have written a testing code on demo007 on incident form with a ui action name is create_problem and you can have a look at it.
Revised UI Action Code :
Action Name : create_incident // Make sure you write it in Action Name field present ui action's form
Client :true
onClick : ClientCode()
Script :
function ClientCode(){ // Added new lines
gsftSubmit(null, g_form.getFormElement(), 'create_incident');
}
if(typeof window == 'undefined'){ // Added new lines
createincident();
}
function createincident() {
var status = current.u_laptop_status;
var prep = current.u_prepared_by;
var user = current.u_checked_out_to;
var dateout = current.u_date_checked_out;
var dateback = current.u_date_due_back;
var reason = current.u_business_reason;
if (status == 'Checked Out' && prep != '' && to != '' && dateout != '' && dateback != '' && reason != '') // what is to? Try to code it in server side scripting
{
var length = 10;
var truncatedout = dateout.substring(0,length);
var truncatedback = dateback.substring(0,length);
var laptop = current.u_laptop_name;
var tech = current.u_prepared_by;
var description = "Checked Out To - " + user + "\nChecked Out - " + truncatedout + "\nDue Back - " + truncatedback + "\nReason - " + reason;
var incident = new GlideRecord('incident');
incident.priorty = '3';
incident.impact = '3';
incident.category = 'Computer';
incident.subcategory = 'Windows Platform - Hardware';
incident.u_action = 'Loaner';
incident.caller_id = user;
incident.due_date = dateback;
incident.short_description = laptop + ' checked out to ' + user;
incident.assignment_group = 'SEC-SN-ITSERVICEDESK'; // Rather, I would suggest pass the sys_id in inverted commas, since its a reference field i beleive.(Eg : "fed6788gh34khllltyghtt" instead of 'SEC-SN-ITSERVICEDESK'
incident.assigned_to = tech;
incident.description = description;
incident.contact_type = 'in-person'; // If contact_type is a choice type field then make sure 'in-person' is one of the database value already configured
incident.state = 6;
incident.close_code = 'Known Solution';
incident.close_notes = "laptop configured and given to user";
var sysID = incident.insert();
var mySysID = current.update();
gs.addInfoMessage("Incident " + incident.number + " created. Please click on Save or Update to finish checking out this laptop.");
action.setRedirectURL(incident);
action.setReturnURL(current);
}
else
{
alert("You cannot check out a laptop unless all required fields are filled out. Make sure to set the status to 'Checked Out' when ready");
current.setAbortAction(true);
}
}
Please refer my comments as well present in the code. Moreover, I would like to suggest you go through the link which can give you better understanding : http://www.servicenowguru.com/system-ui/ui-actions-system-ui/client-server-code-ui-action/
I hope this would help you.