- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-06-2022 08:01 AM
Hi All,
Good Day!
I just wanted to inquire as I have a requirement for business rule to insert new value if it doesn't exist yet on the table "m2m_kb_ci". The current script I have seems to have been preventing the update on the existing KB article when there is no relationship between the table m2m_kb_ci and existing record in kb_knwoledge. Can you please advise as to what I can improve on the script? I'm still new to this and doesn't usually do scripting. Please see the below requirement:
Create new Business Rule on kb_knowledge. Trigger the rule on update and insert where the "Service Offering" Attribute is updated
In script, First check to see if the New Value is already added as a relationship with the Current article within m2m_kb_ci table. If not, then create a new Relationship record here.
Then Check to see if the Previous value still has a relationship to the current record in m2m_kb_ci, then remove it if it exists.
Below is the current script I have.
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var soRel = new GlideRecord('m2m_kb_ci');
soRel.addQuery('kb_knowledge',current.sys_id);
soRel.query();
if(!soRel.hasnext()){
current.insert();
gs.addInfoMessage("record inserted");
}
//else if(soRel.isValidRecord(true)){
//previous.deleteRecord();
//}
})(current, previous);
Solved! Go to Solution.
- Labels:
-
Script Debugger
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-06-2022 08:35 AM
Hi,
Please check with below script:
(function executeRule(current, previous /*null when async*/) {
var soRel = new GlideRecord('m2m_kb_ci');
soRel.addQuery('kb_knowledge',current.getUniqueValue());
soRel.addQuery("cmdb_ci", previous.getValue("cmdb_ci"));
soRel.query();
if(soRel.next()){
soRl.deleteRecord();
gs.addInfoMessage("old record deleted");
}
var newKbCiGR = new GlideRecord('m2m_kb_ci');
newKbCiGR.addQuery('kb_knowledge',current.getUniqueValue());
newKbCiGR.addQuery("cmdb_ci", current.getValue("cmdb_ci"));
newKbCiGR.query();
if(!newKbCiGR.hasnext()){
newKbCiGR.cmdb_ci = current.getValue("cmdb_ci");
newKbCiGR.kb_knowledge = current.getUniqueValue();
newKbCiGR.insert();
gs.addInfoMessage("record inserted");
}
})(current, previous);
Please mark my respsone as helpful/correct, if it answer your question.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-06-2022 08:35 AM
Hi,
Please check with below script:
(function executeRule(current, previous /*null when async*/) {
var soRel = new GlideRecord('m2m_kb_ci');
soRel.addQuery('kb_knowledge',current.getUniqueValue());
soRel.addQuery("cmdb_ci", previous.getValue("cmdb_ci"));
soRel.query();
if(soRel.next()){
soRl.deleteRecord();
gs.addInfoMessage("old record deleted");
}
var newKbCiGR = new GlideRecord('m2m_kb_ci');
newKbCiGR.addQuery('kb_knowledge',current.getUniqueValue());
newKbCiGR.addQuery("cmdb_ci", current.getValue("cmdb_ci"));
newKbCiGR.query();
if(!newKbCiGR.hasnext()){
newKbCiGR.cmdb_ci = current.getValue("cmdb_ci");
newKbCiGR.kb_knowledge = current.getUniqueValue();
newKbCiGR.insert();
gs.addInfoMessage("record inserted");
}
})(current, previous);
Please mark my respsone as helpful/correct, if it answer your question.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-06-2022 05:56 PM
Hi
Thank you for the assistance! I tried to do this and both of the info message "Record inserted" and "Record deleted" were displayed every time I change the field service offering but no record was created if the current Service offering does not exist yet in the table m2m_kb_ci.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-06-2022 06:30 PM
Hello RM,
Could you please check with below code:
(function executeRule(current, previous /*null when async*/) {
var soRel = new GlideRecord('m2m_kb_ci');
soRel.addQuery('kb_knowledge',current.getUniqueValue());
soRel.addQuery("cmdb_ci", previous.getValue("cmdb_ci"));
soRel.query();
if(soRel.next()){
soRl.deleteRecord();
gs.addInfoMessage("old record deleted");
}
var newKbCiGR = new GlideRecord('m2m_kb_ci');
newKbCiGR.addQuery('kb_knowledge',current.getUniqueValue());
newKbCiGR.addQuery("cmdb_ci", current.getValue("cmdb_ci"));
newKbCiGR.query();
if(!newKbCiGR.hasNext()){
newKbCiGR.cmdb_ci = initialize();
newKbCiGR.cmdb_ci = current.getValue("cmdb_ci");
newKbCiGR.kb_knowledge = current.getUniqueValue();
newKbCiGR.insert();
gs.addInfoMessage("record inserted");
}
})(current, previous);
if this does not work. please share the screenshot of your script:
Please mark my respsone as helpful/correct, if it answer your question.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-06-2022 07:13 PM
Hi
It seems like it's not working completely now, no info messages displayed on top, no record created nor deleted the existing record. :'(
When I hit save after updating the Service Offering field it just saves the new record but nothing is displayed on top nor action taken for the m2m table m2m_kb_ci.