New Business rule on kb_knowledge. Trigger the rule on update and insert where the "Service Offering" Attribute is updated

Community Alums
Not applicable

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);

find_real_file.png

 

 

1 ACCEPTED SOLUTION

Mahendra RC
Mega Sage

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

View solution in original post

7 REPLIES 7

Could you please check with below code there was one issue in previous 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()){
		soRel.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.initialize();
		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

Community Alums
Not applicable

Hi @Mahendra ,

When I change the service offering with the KB that an existing Affected Products it would display the message notification on top and create a new ci but it is blank and the previous relationship CI is not deleted. Blank CIs are also created when I change the service offering for the KBs that doesn't have any Affected Product. Please see attached screenshot.

find_real_file.png

find_real_file.png

find_real_file.png

Community Alums
Not applicable

Hi @Mahendra 

It worked on my end now, thank you for your help! Turns out the field name for the cmdb_ci is incorrect so I just had to change it on what I have on my form which is below:

(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("u_service_offering"));
	soRel.query();
	if(soRel.next()){
	soRel.deleteRecord();
	gs.addInfoMessage("Previous record deleted");
	}
	
	gs.addInfoMessage(current.getDisplayValue('u_service_offering'));
	
	var newKbCiGR = new GlideRecord('m2m_kb_ci');
	newKbCiGR.addQuery('kb_knowledge',current.getUniqueValue());
	newKbCiGR.addQuery("cmdb_ci", current.getValue("u_service_offering"));
	newKbCiGR.query();
	if(!newKbCiGR.hasNext()){
		newKbCiGR.initialize();
		//newKbCiGR.cmdb_ci = current.getUniqueValue("cmdb_ci");
		newKbCiGR.cmdb_ci = current.getValue("u_service_offering");
		newKbCiGR.kb_knowledge = current.getUniqueValue();
		newKbCiGR.insert();
		gs.addInfoMessage(current.getValue("u_service_offering"));
	}
	
})(current, previous);