Prevent duplicate creation of Knowledge Article at the time of publishing if short description same

SabyasachiM8774
Tera Contributor

Hi , can someone please help with a script that would prevent duplicate creation of a Knowledge Article at the time of publishing if the short description is same. If any one use insert and insert stay button the copy of the  article would get created and will get saved as a draft, but if there is any existing article with same short description the user will not get it published, it should through an error message like in the script. 

 

(function executeRule(current, previous /*null when async*/) {
        var incidentShortDesc = current.getValue('short_description'); //considering short_desr to be the key , you can use any fieldwhich you wanted to check
        var kbArticleGr = new GlideRecord('kb_knowledge');
        kbArticleGr.addQuery('short_description', incidentShortDesc);
        kbArticleGr.query();
        
        if (kbArticleGr.hasNext()) {
            gs.addErrorMessage('A knowledge article with the same short description already exists.');
            current.setAbortAction(true); 
        }
 
})(current, previous);

 

1 ACCEPTED SOLUTION

Runjay Patel
Giga Sage

Hi @SabyasachiM8774 

 

You can use below code

var incidentShortDesc = current.getValue('short_description'); 

        var kbArticleGr = new GlideRecord('kb_knowledge');
		kbArticleGr.addEncodedQuery('sys_id!='+current.sys_id);
        kbArticleGr.addQuery('short_description', incidentShortDesc);
        kbArticleGr.query();
        
        if (kbArticleGr.hasNext()) {
            gs.addErrorMessage('A knowledge article with the same short description already exists.');
            current.setAbortAction(true); 
        }

RunjayPatel_0-1731600152666.png

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

 

View solution in original post

If you have basic knowledge in ServiceNow and face difficulties writing script in ServiceNow then this ServiceNow scripting tutorial is for you. In this tutorial i have explained from very basic to advance level of scripting. I have used real word use case to explain the scenarios. After watching
1 REPLY 1

Runjay Patel
Giga Sage

Hi @SabyasachiM8774 

 

You can use below code

var incidentShortDesc = current.getValue('short_description'); 

        var kbArticleGr = new GlideRecord('kb_knowledge');
		kbArticleGr.addEncodedQuery('sys_id!='+current.sys_id);
        kbArticleGr.addQuery('short_description', incidentShortDesc);
        kbArticleGr.query();
        
        if (kbArticleGr.hasNext()) {
            gs.addErrorMessage('A knowledge article with the same short description already exists.');
            current.setAbortAction(true); 
        }

RunjayPatel_0-1731600152666.png

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

 

If you have basic knowledge in ServiceNow and face difficulties writing script in ServiceNow then this ServiceNow scripting tutorial is for you. In this tutorial i have explained from very basic to advance level of scripting. I have used real word use case to explain the scenarios. After watching