The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Create a fix script to remove the extra string from the Short description

Nandini Mishra
Tera Contributor

I need to remove the string starting with "IT Support:" from all the knowledge articles short descriptions. i.e if the short description is "IT Support: How to enable user account" then it should rename to "How to enable user account". Need to update the short descriptions of all the knowledge articles starting with "IT Support:"
Can someone help me how to write the script?

7 REPLIES 7

Tony Chatfield1
Kilo Patron

Hi, something like this should work

//string that you want to find/replace
var myString = 'IT Support:';

//first query the table 
var checkRecord = new GlideRecord('yourTargetTable');
//addQuery or encodedQuery if required.
checkRecord.query();

while(checkRecord.next()) {
//now test each records short description for existance of your string using indexOf
if(checkRecord.short_description.indexOf(myString) > -1) {

//replace the string with any empty string and trim any white space.
checkRecord.short_description = checkRecord.short_description.replace(myString, '').trim();

//Now update the record without triggering anything
checkRecord.setWorkflow(false);
checkRecord.update();

}
}

Hi Tony,

 

Thank you for the response, in addition i also have the sys_id's of the articles appended to the short description, how can i remove that too from the name?

eg: "IT Support:da1b51ab1b63cc90c001a8a4bd4bcb39 "How to enable user account" 

where "da1b51ab1b63cc90c001a8a4bd4bcb39" is the sys_id of the article

Hi Tony,

 

I am using the below script to update the records, since my string also contain the sys_id of the record after "IT Support: 763822295dgdbssjww63fcnyysr678 <Short_Description>

var myString = 'IT Support';
var checkRecord = new GlideRecord('kb_knowledge');
checkRecord.addEncodedQuery('number', "KB0643829");
checkRecord.query();
while(checkRecord.next()) {
if(checkRecord.short_description.indexOf(myString) > -1) {
checkRecord.short_description = checkRecord.short_description.replace(myString, '').trim();
var regexp = /[0-9a-f]{32}/;
var out = (checkRecord.short_description.match(regexp));
checkRecord.short_description = checkRecord.short_description.replace(out, '').trim();
checkRecord.setWorkflow(false);
checkRecord.update();
}
}

 

Basheer
Mega Sage

Hi @Nandini Mishra 

Try as below

var str = "IT Support";
var gr = new GlideRecord('kb_article');
gr.addEncodedQuery("short_descriptionSTARTSWITHIT Support")
gr.query();
while(gr.next()) {
if(gr.short_description.startsWith(str)) {
gr.short_description = gr.short_description.replace(myString,'').trim();
gr.setWorkflow(false);
gr.update();
}
}
Please hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

Cheers,
Mohammed Basheer Ahmed.