Splice method not working

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-15-2019 06:49 PM
// -- CODE --
//--copy articles from parent major incident to child incidents
try
{
var childIncIdsArr=[];
var childIncObj={};
var parentKBArtlArr=[];
var childKBArtArr=[];
var newArtArr=[];
var childKBTaskAddGr= new GlideRecord('m2m_kb_task');
//--get the list of articles attached to the parent incident
var m2mKBTaskParentGr = new GlideRecord('m2m_kb_task');
m2mKBTaskParentGr.addQuery('task='+current.sys_id+'');
m2mKBTaskParentGr.query();
while(m2mKBTaskParentGr.next())
{
parentKBArtlArr.push(m2mKBTaskParentGr.kb_knowledge+'');
}
//--get the list of child incident details
var childIncGr= new GlideRecord('incident');
childIncGr.addQuery('parent_incident='+current.sys_id+'');
childIncGr.query();
while(childIncGr.next())
{
childIncIdsArr.push(childIncGr.sys_id+'');
childIncObj[childIncGr.sys_id+'']={};
childIncObj[childIncGr.sys_id+'']['kb_articles']=parentKBArtlArr;
childIncObj[childIncGr.sys_id+'']['inc_state']=childIncGr.state+'';
/*
format used
{
'sys_id of the child incident':{
'kb_articles':[array of kb articles linked to the child incident],
'inc_state':'state of the incident'
}
}
*/
}
//--get the list of articles attached to the parent incident but not present in child incident
var m2mKBTaskChildGr = new GlideRecord('m2m_kb_task');
m2mKBTaskChildGr.addQuery('task.sys_idIN'+childIncIdsArr.join(','));
m2mKBTaskChildGr.query();
while(m2mKBTaskChildGr.next())
{
var childIncArtTempArr=childIncObj[m2mKBTaskChildGr.task+'']['kb_articles'];//array of articles for the child incident
var childArtIndx= childIncArtTempArr.indexOf(m2mKBTaskChildGr.kb_knowledge+'');//index of the article in the array of the child incidents articles
if(childArtIndx>-1)
childIncArtTempArr.splice(childArtIndx,1);//remove the article from child incident object, if the article is already linked
childIncObj[m2mKBTaskChildGr.task+'']['kb_articles']=childIncArtTempArr;
}
//--now insert the articles for the child incident
for(var childIncObjItr in childIncObj)
{
var tempIncObj= childIncObj[childIncObjItr];
var tempIncObjArtlArr=tempIncObj['kb_articles'];
//--dont inherit article if child incident already has articles and incident state is closed
if(tempIncObj['inc_state']=='7' && tempIncObjArtlArr.length>0)
continue;
for(var i=0; i<tempIncObjArtlArr.length;i++)
{
childKBTaskAddGr.newRecord();
childKBTaskAddGr.kb_knowledge=tempIncObjArtlArr[i]+'';//sys id of the kb article
childKBTaskAddGr.task= childIncObjItr+'';//sys id of the child incident
childKBTaskAddGr.insert();
}
}
}
catch(e)
{
gs.log("Error caught in 'Inf Inherit parent articles via incident' Business Rule. Error message: "+e);
}
})
Hi,
I am trying to copy kb articles linked to a parent incident to the child incidents whenever a child incident is linked.
The link between incident and kb articles is maintained in m2m_kb_task table and thus I have written a business rule on incident table to insert m2m_kb_task records.
I have written the below attached code where I am first fetching all the child incidents and then storing the information in below format (object with keys as sys id of the child incident containing another object with 2 elements viz. kb_articles which contains array of sys ids of the kb articles linked with the child incident, and 2nd element as state of child incident):
Step 1:
first storing the articles linked with parent incident inside an array named 'parentKbArtArr'
{
'sys_id of child incident 1': {
'kb_articles':'array of sys ids of kb articles already linked to the child incident', // default value as parentKbArtArr
'inc_state': 'state of the child incident'
}
and so on...
}
I am keeping the default value of the 'kb_articles' array for all the child incidents object as an array of articles linked with the parent incident i.e. parentKbArtArr.
Step 2:
Then I am querying m2m_kb_task table with the task.sys_idIN child incident sys ids, in order to remove the kb articles from the kb_articles array for a child incident, if there is already a link existing.
e.g. {
'inc01': ['parentkb1','parentkb2','parentkb3','parentkb4'],
'inc_state':'2'
}
where inc01 = sys id of child incident and parentkbs are sys ids of the kb articles linked to the parent incidents. now if parent kb2 and parentkb3 are already linked to inc01, I wanted the kb_articles array to contain only parentkb1 and parentkb4.
i.e.
{'inc01': ['parentkb1','parentkb4'],
'inc_state':'2'}
Step 3:
Finally, I will get my modified json and I will iterate it to insert m2m_kb_task records using the child inc sys ids and the array of articles
e.g. the json object might get converted to below
{
'inc01': ['parentkb1','parentkb4'],
'inc_state':'2',
'inc01': [''parentkb2','parentkb3','parentkb4'],
'inc_state':'1'
'inc01': ['parentkb1','parentkb2','parentkb3','parentkb4'],
'inc_state':'2'
'inc01': ['parentkb1','parentkb3','parentkb4'],
'inc_state':'1'
}
PROBLEM STATEMENT:
*****************************
I am getting stuck at the step 2 where I am using splice method to remove an element from the array.
The splice method is somehow modifying the complete array which is reference with all the child incident elements instead of the child incident element which is required.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-16-2019 07:43 AM
var obj = {
'inc01': ['parentkb1','parentkb2','parentkb3','parentkb4'],
'inc_state':'2'
};
var childIncArtTempArr=obj['inc01'];//array of articles for the child incident
console.log(childIncArtTempArr);
var childArtIndx= childIncArtTempArr.indexOf('parentkb3');//index of the article in the array of the child incidents articles
console.log(childArtIndx);
if(childArtIndx>-1)
childIncArtTempArr.splice(childArtIndx,1);//remove the article from child incident object, if the article is already linked
console.log(childIncArtTempArr);
//output on https://playcode.io/
(4) [
"parentkb1" ,
"parentkb2" ,
"parentkb3" ,
"parentkb4"
]
2
(3) [
"parentkb1" ,
"parentkb2" ,
"parentkb4"
]
Vinod Kumar Kachineni
Community Rising Star 2022

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-18-2019 02:42 PM
Thanks for your answer.
var obj={
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2019 08:19 AM
//Can you try
var key = "'" + m2mKBTaskChildGr.task,toString() + "'"; //wrapped in single quotes 'incident'
var childIncArtTempArr = childIncObj[key]['kb_articles']
Vinod Kumar Kachineni
Community Rising Star 2022

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-18-2019 02:35 PM
//can be run on https://playcode.io/ or jsfiddle.net
var obj={
'inc01':
{
'kb_articles': ['parentkb1','parentkb2','parentkb3','parentkb4'],
'inc_state':'2'
},
'inc02':{
'kb_articles': ['parentkb1','parentkb2','parentkb3','parentkb4'],
'inc_state':'2'
},
'inc03':{
'kb_articles': ['parentkb1','parentkb2','parentkb3','parentkb4'],
'inc_state':'2'
},
'inc04':{
'kb_articles': ['parentkb1','parentkb2','parentkb3','parentkb4'],
'inc_state':'2'
}
};
var tempObj= {
'inc01':['parentkb1','parentkb7'],
'inc02':['parentkb4','parentkb10'],
'inc04':['parentkb1','parentkb2']
};
for(var itr in tempObj)
{
if(obj[itr]['kb_articles'].indexOf(tempObj[itr][0]+'')>-1)
{
obj[itr]['kb_articles'].splice(obj[itr]['kb_articles'].indexOf(tempObj[itr][0]+''),1);
}
if(obj[itr]['kb_articles'].indexOf(tempObj[itr][1]+'')>-1)
{
obj[itr]['kb_articles'].splice(obj[itr]['kb_articles'].indexOf(tempObj[itr][1]+''),1);
}
var resObj={};
//console.log(obj[itr]['kb_articles'].indexOf(tempObj[itr]));
//if(obj[itr]['kb_articles'])
}
console.log(obj);
Please find above the actual problem converting into a complete javascript code. If run the code in 'https://playcode.io', or in background script, it works completely fine. However, if I the same code runs using the gliderecord, as pasted in my 1st code, it gives me empty arrays.