- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2018 10:46 AM
Hi guys, I have created a new UI action (code below) to create a Defect from the Inc form.
We have an Inc related list on the Defect form (as well as a custom field 'Defect' on the Inc form which is successfully being set by the new UI action).
I'm having a real blank moment but I just want to know the code needed to reference the existing INC in the Defect related list of incidents??
Furthermore what's the best way to prevent a Defect being created from an Inc, if this has already been done for that Inc (i.e. if 'Defect' field already has a value)?
I.e. we want to allow multiple incidents to be referenced to a defect (that will show on the related list) but only one defect per incident (via the custom field 'Defect').
Many thanks,
DS
UI action code (it's working bar the above requests):
function onSubmit_Defect() {
var response = confirm('Create a defect record?');
if (!response){
return false;
}
//Call the UI Action and skip the 'onclick' function
gsftSubmit(null, g_form.getFormElement(), 'createdefect'); //MUST call the 'Action name' set in this UI Action
}
if(typeof window == 'undefined')
create_defect();
function create_defect(){
var defect = new GlideRecord("rm_defect");
defect.business_service = current.business_service;
defect.u_service_category = current.category;
defect.cmdb_ci = current.cmdb_ci;
defect.short_description = current.short_description;
var sysID = defect.insert();
current.u_defect = sysID;
var mySysID = current.update();
gs.addInfoMessage("Defect " + defect.number + " created");
action.setRedirectURL(defect);
action.setReturnURL(current);
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2018 12:21 PM
Open the Defect form, navigate to Configure->Related List and select Defect -> Incident. All the related list are OOB, you just need to add that related list.
Also to not allow users to create another defect from the incident, just add a condition in ui action,
current.u_defect==''
Please mark this response as correct or helpful if it assisted you with your question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2018 12:21 PM
Open the Defect form, navigate to Configure->Related List and select Defect -> Incident. All the related list are OOB, you just need to add that related list.
Also to not allow users to create another defect from the incident, just add a condition in ui action,
current.u_defect==''
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-26-2018 04:34 AM
Hi Sanjiv, sorry but but to confirm I already have the related list added to the Defect form. I would like to know how as part of the UI action I can make the INC number (that the Defect is created from) automatically reference on the INC related list.
The Defect number is already being successfully set on the INC form (in the custom u_defect field) via this line in the code:
current.u_defect = sysID;
Thanks!
DS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-26-2018 05:14 AM
Btw the condition makes sense/works great so thanks for that.
I have also established that the related list I needed was Incident -> Defect (I had added Incident -> Parent). With this list added the INC is automatically showing in the list.
Thanks for your help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2018 05:16 PM
function onSubmit_Defect() {
var response = confirm('Create a defect record?');
if (!response){
return false;
}
//Call the UI Action and skip the 'onclick' function
gsftSubmit(null, g_form.getFormElement(), 'createdefect'); //MUST call the 'Action name' set in this UI Action
}
if(typeof window == 'undefined')
create_defect();
function create_defect(){
var defect = new GlideRecord("rm_defect");
defect.business_service = current.business_service;
defect.u_service_category = current.category;
defect.cmdb_ci = current.cmdb_ci;
defect.short_description = current.short_description;
var sysID = defect.insert();
current.u_defect = sysID;
var mySysID = current.update();
gs.addInfoMessage("Defect " + defect.number + " created");
action.setRedirectURL(defect);
action.setReturnURL(current);
}
One thing I noticed as soon as i look into your code is you are missing the initialize() statement once you glilderecorded the defect table
Incident related list will be there on the defect form only if you have a custom defect field referring incident and as you said you had it is fine
The best way to avoid duplication would be checking if your current incident record has already a value.And you can have a ui action visibility condition as said in the above comment
Kindly mark it helpful it the solution provided resolved your issue so that it will be considered as answered instead of being unanswered.
Have a great day
Thanks,
Kalyan