[SalesForce] approve article before publish

I am going through an strange issue while implementing a validation rule/trigger.
i want to check if article are in approved status (custom field) before it can be publish. i don't want to allow a user to publish an article which are not approved.

i tried this using validation rule on Knowledge__kav but this validation rule does not fire with the below rule criteria.

IF(AND( RecordType.DeveloperName == "Solution_Article", 
  ISPICKVAL(PublishStatus, "Online")) , true, false)

So, when article are publish, their PublishStatus is changed to 'Online', hence i tried to catch this in a trigger (below) but i am not able to catch the status change, as the PublishStatus is always in draft.

trigger KnowledgeTrigger on Knowledge__kav (before update) {

    if(Trigger.isUpdate && Trigger.isBefore){

        Set<ID> setKnwId = new Set<ID>();

        Id devRecordTypeId = Schema.SObjectType.Knowledge__kav.getRecordTypeInfosByName().get('Solution Article').getRecordTypeId();


        for(Knowledge__kav k1: trigger.old){
            system.debug('k old ' + k1);
        }

        for(Knowledge__kav k: trigger.new)          
        {
            system.debug('k ' + k);

            if(k.Id != null && k.PublishStatus == 'Online' && k.RecordTypeId == devRecordTypeId)
            {
                system.debug('test');

            }
        }
    }
}

Can you please help me out in this?

Best Answer

Knowledge Article object is very special object - you can not track changes of PublishStatus field in trigger/process builder. As workaround (not very good solution) -

  1. Remove "Publish" button from layout.

  2. Add a button using formula field. You can make this button prominent using any image. Place at very top-right in layout.

  3. This Formula field would redirect to a visualforce page with record id as parameter.

  4. On this page show error message if approval status is not approved.

  5. If approval status is approved then publish the record using PublishingService Class: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_knowledge_kbManagement.htm

    String articleId = 'Insert article ID';
    KbManagement.PublishingService.publishArticle(articleId, true);

Related Topic