- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2020 01:04 AM
Hello All,
Can someone please help me ?
I have added a ui action to create outage in change.It will automatically populate short description and configuration item in the window.How its populating?
I want to populate planned start date in begin field and planned end date in end in the outage window.
createOutageM2M();
function createOutageM2M() {
current.update();
var url = new CreateOutageUtil().getNewOutageLink(current);
action.setRedirectURL(url);
action.setReturnURL(current);
}
Thanks
Saranya
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2020 12:44 PM
Here you go.
Script include:
Name: OutageDateValidation
Client callable: True
Script:
var OutageDateValidation = Class.create();
OutageDateValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateStartEndDate: function() {
var startDate = this.getParameter('sysparm_start_date');
var endDate = this.getParameter('sysparm_end_date');
var changeRequest = this.getParameter('sysparm_change_sys_id');
var grChange = new GlideRecord('change_request');
if (grChange.get(changeRequest)) {
if ((!JSUtil.nil(startDate) && (startDate < grChange.start_date.getDisplayValue() || startDate >= grChange.end_date.getDisplayValue())) || (!JSUtil.nil(endDate) && (endDate <= grChange.start_date.getDisplayValue() || endDate > grChange.end_date.getDisplayValue()))) {
return 1;
} else if (!JSUtil.nil(startDate) && !JSUtil.nil(endDate) && startDate >= endDate) {
return 2;
} else {
return true;
}
} else {
return true;
}
},
type: 'OutageDateValidation'
});
Client script 1:
Name: Validate Begin date
Table: Outage [cmdb_ci_outage]
Type: onChange
Field name: Begin
Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var changeSysID = g_form.getValue("task_number");
var endDate = g_form.getValue("end");
var ga = new GlideAjax('OutageDateValidation');
ga.addParam('sysparm_name', 'validateStartEndDate');
ga.addParam('sysparm_change_sys_id', changeSysID);
ga.addParam('sysparm_start_date', newValue);
ga.addParam('sysparm_end_date', endDate);
ga.getXML(callback);
}
function callback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer != "true") {
g_form.clearValue("begin");
if (answer == "1") {
g_form.showFieldMsg("begin", "Begin date must be between Planned start date and Planned end date on Change request", "error");
} else {
g_form.showFieldMsg("begin", "Begin date must be before End", "error");
}
}
}
Client script 2:
Name: Validate End date
Table: Outage [cmdb_ci_outage]
Type: onChange
Field name: End
Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var changeSysID = g_form.getValue("task_number");
var beginDate = g_form.getValue("begin");
var ga = new GlideAjax('OutageDateValidation');
ga.addParam('sysparm_name', 'validateStartEndDate');
ga.addParam('sysparm_change_sys_id', changeSysID);
ga.addParam('sysparm_start_date', beginDate);
ga.addParam('sysparm_end_date', newValue);
ga.getXML(callback);
}
function callback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer != "true") {
g_form.clearValue("end");
if (answer == "1") {
g_form.showFieldMsg("end", "End date must be between Planned start date and Planned end date on Change request", "error");
} else {
g_form.showFieldMsg("end", "End date must be after Begin", "error");
}
}
}
Hope this helps!
-Tanaji
Please mark answer correct/helpful based on impact.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2020 10:54 AM
Hello Saranya,
If you look at the create outage UI action, then the code will be something like this. Modify this as per your requirement and set only the values in the fields that you want and comment others.
createOutageM2M();
function createOutageM2M() {
current.update();
gs.addInfoMessage(gs.getMessage("You will need to navigate back to <a href='{0}'>{1}</a> upon completion", [current.getLink(), current.number + ""]));
var outage = new GlideRecord("cmdb_ci_outage");
outage.setValue("type", "planned");
outage.setValue("cmdb_ci", current.cmdb_ci + "");
outage.setValue("task_number", current.sys_id + "");
outage.setValue("begin", current.start_date + "");
outage.setValue("end", current.end_date + "");
var outageSysId = outage.insert();
var task_outage = new GlideRecord('task_outage');
task_outage.setValue("task", current.sys_id + "");
task_outage.setValue("outage", outageSysId);
task_outage.insert();
action.setRedirectURL(outage);
action.setReturnURL(current);
}
Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.
Regards,
Asif
2020 ServiceNow Community MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2020 08:17 AM
Hello Asif,
Thanks for the reply.
Can you please give one more help?
I have populated change request planned start date to begin and planned end date in end of outage.
User should only select within timeframe of change window.
How can I we do validation?
Thanks
Saranya

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2020 08:41 AM
Hi,
Fort you need to write onchange client script and do the validation. Chk the below link for sample code.
https://community.servicenow.com/community?id=community_article&sys_id=a26dac761b4e8010a59033f2cd4bcb12
Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.
Regards
Asif
2020 ServiceNow Community MVP

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2020 08:43 AM
Check this post out-
https://community.servicenow.com/community?id=community_question&sys_id=31e6cfa5db1cdbc01dcaf3231f96...
You need something similar. There should be two functions in the script include which will be called from two onChange client script (one on Begin and one on End).
These function will check if Task number field on Outage references a change request and if yes it will dot walk and compare the begin/end date on outage with Planned start and Planned end date. Return true/false to client scripts based on which you will add field messages.
-Tanaji
Please mark response helpful/correct based on the impact.