- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2021 07:13 AM
Hello All,
On incident form i have created a new field called "Number of incidents caused by change" which is an integer.
This filed should auto populate with the count of incident caused by change
1. New Field which gets the count of incidents
2.incidents caused by change related list
3. Edit is the place where we can add incidents or remove incidents
The issue is created a business rule with the below code, it works only when new incidents are added, but when we remove the incidents the count is not getting updated.
BR code: for testing purpose i was using the info message to show on the form:
NOTE: Both the business rules work and update when new incidents are added but when removed from edit count not getting updated.
Script1 : written on the change form
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var TaskInc = new GlideRecord('incident');
TaskInc.addQuery('caused_by', current.sys_id);
//TaskInc.addEncodedQuery('caused_by!=NULL');
TaskInc.query();
var count = TaskInc.getRowCount();
gs.addInfoMessage("Total incidents caused by change before if condition --" + TaskInc.getRowCount());
//alert("change happened");
if (TaskInc.next()) {
if (count > 0) {
gs.addInfoMessage("Total incidents is greater than zero --" + TaskInc.getRowCount());
// current.u_number_of_incidents_caused_by_change = TaskInc.getRowCount();
// current.update();
} else {
gs.addInfoMessage("total incident count is Zero");
// current.u_number_of_incidents_caused_by_change = 0;
// current.update();
//gs.addInfoMessage(test)
}
}
})(current, previous);
Script 2: written on the incident form
(function executeRule(current, previous /*null when async*/ ) {
var task = new GlideRecord('incident');
task.addQuery('caused_by', current.caused_by);
task.query();
//gs.addInfoMessage("Incident count was --" + task.getRowCount());
var Chg = new GlideRecord('change_request');
Chg.addQuery('sys_id', current.caused_by);
Chg.query();
if (Chg.next()) {
Chg.u_number_of_incidents_caused_by_change = task.getRowCount();
Chg.update();
}
})(current, previous);
Solved! Go to Solution.
- Labels:
-
Change Management
-
Incident Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2021 04:51 PM
Hello Sree,
You can achieve this by an after business rule applied to incident table.
Script :
(function executeRule(current, previous /*null when async*/ ) {
var sys_id_ch = '';
var op = '';
if(gs.nil(current.caused_by)&&!gs.nil(previous.caused_by)){
sys_id_ch =previous.caused_by;
op = 'descrease';
}else if(!gs.nil(current.caused_by)&&gs.nil(previous.caused_by)){
sys_id_ch = current.caused_by;
op = 'increase';
}
var change_sys_id = current.caused_by;
var change_rec = new GlideRecord('change_request');
change_rec.addQuery('sys_id', sys_id_ch);
change_rec.query();
if (change_rec.next()) {
if(op == 'increase'){
change_rec.u_number_incident = change_rec.u_number_incident +1;
change_rec.update();
}
else if(op == 'descrease'){
change_rec.u_number_incident = change_rec.u_number_incident -1;
change_rec.update();
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2021 03:34 PM
I didn't notice that. Thank you for your feedback I really appreciate . That is my proposition. Tell me if you are ok with it .
function onLoad() {
var ajax = new GlideAjax('NumberIncidentCausedByChange');
ajax.addParam('sysparm_name', 'getNumberIncident');
ajax.addParam('sysparm_sys_id_causedBy', g_form.getUniqueValue());
ajax.getXML(displayNumberIncident);
function displayNumberIncident(response) {
var value = response.responseXML.documentElement.getAttribute('answer');
if(value != g_form.getValue('u_number_incident')){
g_form.setValue('u_number_incident', value);
g_form.save();
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2021 03:50 PM
If i add to save the form, the form is saving all the time, not able to make any changes to the form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2021 03:56 PM
No copy and paste the last script that I share with you . I think you added only g_fom.update() in the script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2021 10:20 AM
Hello Hajar,
This works only when we open the change form. if the change ticket is not opened then those changes are not updated. Is there a way to change script
"The script cannot be dependent on loading the Change form. We need "Number of Incidents Caused by Change" to update without opening the CHG".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2021 01:59 PM
Hello Sree, you can do it by running the script in the background.
See this picture to acces the background script :
After copy and past this script :
var change_records = new GlideRecord('change_request');
change_records.query();
while(change_records.next()){
var inc = new GlideRecord('incident');
inc.addQuery('caused_by',change_records.sys_id);
inc.query();
change_records.u_number_incident =inc.getRowCount();
change_records.update();
}
Click on Run script and let it the script runs.
As you can see, this screen shows that there no values in the Number incident column.
And after run it, the values is affected to each row :
Best Regards.
Hajar.