"Valid to" and pre-populate text in short description while republishing article

Sriram28
Tera Contributor

Hello All,

 

When user click on re-publish ui action to republish any article.

 

I want below things to work.

1. "valid to" to be extended based on Knowledge base validity when republished

2. Pre-populate a text in description stating "the article is re-published"

 

Can someone help how to achieve above scenarios.

 

Thanks in advance

 

1 ACCEPTED SOLUTION

Sarthak Kashyap
Mega Sage

Hi @Sriram28 ,

 

I tried your problem in my PDI and it works for me please check below solution 

 

Navigate to UI Actions table and search for Republish and open kb_knowledge table button .

SarthakKashyap_0-1764571365016.png

and add below script, I modified it as per your requirment

 

var newRecord = new KBKnowledge().republish(current);
if (newRecord) {
    gs.addInfoMessage("Button Clicked");
    var validTo = newRecord.getValue("valid_to");
    gs.addInfoMessage(new KBKnowledge().getStateMessage(newRecord.getValue("workflow_state")));
    action.setRedirectURL(newRecord);

    var kbGR = new GlideRecord("kb_knowledge_base");
    if (kbGR.get(current.kb_knowledge_base)) {
        gs.addInfoMessage("InSide IF KB");

        var validityDays = kbGR.getValue("article_validity");
		// gs.addInfoMessage("validityDays = " + validityDays);
        if (validityDays) {
			gs.addInfoMessage("Inside IF Check = " + validityDays);
            var now = new GlideDateTime();
            now.addDays(validityDays);
            gs.addInfoMessage("Now = " + now);
            current.valid_to = now;
        }
    }

    var msg = "This article has been re-published on " + gs.nowDateTime() + ".\n\n";

    current.kb_description = msg + current.kb_description;
    current.update();


} else
    gs.addErrorMessage(gs.getMessage("You cannot republish this article. Please try again later."));

 

 

Result

My KB has validity as 20 days 

SarthakKashyap_1-1764571437085.png

 

So once I click the republish button it changes the date more then 20 days and also add description

SarthakKashyap_2-1764571482567.png

 

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,
Sarthak

 

 

View solution in original post

3 REPLIES 3

kaushal_snow
Giga Sage

@Sriram28 ,

 

You can customize the republish UI action for KB articles by extending the script so that when a user clicks Republish it automatically recalculates the Valid to date (for example adding the base validity period from the Knowledge Base header) and sets the article short_description (or a custom field) to include text like (This article has been re published) before saving.......

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Technical Consultant - ServiceNow Class of Legends 2025

Sarthak Kashyap
Mega Sage

Hi @Sriram28 ,

 

I tried your problem in my PDI and it works for me please check below solution 

 

Navigate to UI Actions table and search for Republish and open kb_knowledge table button .

SarthakKashyap_0-1764571365016.png

and add below script, I modified it as per your requirment

 

var newRecord = new KBKnowledge().republish(current);
if (newRecord) {
    gs.addInfoMessage("Button Clicked");
    var validTo = newRecord.getValue("valid_to");
    gs.addInfoMessage(new KBKnowledge().getStateMessage(newRecord.getValue("workflow_state")));
    action.setRedirectURL(newRecord);

    var kbGR = new GlideRecord("kb_knowledge_base");
    if (kbGR.get(current.kb_knowledge_base)) {
        gs.addInfoMessage("InSide IF KB");

        var validityDays = kbGR.getValue("article_validity");
		// gs.addInfoMessage("validityDays = " + validityDays);
        if (validityDays) {
			gs.addInfoMessage("Inside IF Check = " + validityDays);
            var now = new GlideDateTime();
            now.addDays(validityDays);
            gs.addInfoMessage("Now = " + now);
            current.valid_to = now;
        }
    }

    var msg = "This article has been re-published on " + gs.nowDateTime() + ".\n\n";

    current.kb_description = msg + current.kb_description;
    current.update();


} else
    gs.addErrorMessage(gs.getMessage("You cannot republish this article. Please try again later."));

 

 

Result

My KB has validity as 20 days 

SarthakKashyap_1-1764571437085.png

 

So once I click the republish button it changes the date more then 20 days and also add description

SarthakKashyap_2-1764571482567.png

 

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,
Sarthak

 

 

Sriram28
Tera Contributor

Hi @Sarthak Kashyap ,

 

Thank you so much for your response.