0

I am trying to create a batch to send an email anytime the status on a Knowledge Article (Knowledge__kav) is changed.

The difficulty I have on this particular object (Knowledge__kav) is that anytime I update the status (Known_Error_Status__c) a draft is created with a new (Id) which means I struggle to capture the old status value of the article.

For example, let's say I have an Article with: 1st Article version

KnowledgeArticleId: kA09E0000005mCySAI

Id: ka09E0000005ywvQAA

Known_Error_Status__c: In Review

Now if I update the status of the article to In Progress, I will have a new version Id of the article: 2nd Article Version

KnowledgeArticleId: kA09E0000005mCySAI

Id: ka09E0000005yVkQAI

Known_Error_Status__c: In Progress

So when I publish my article, I have a new version Id published (ka09E0000005yVkQAI) and the previous version (1st Article Version: ka09E0000005ywvQAA) is deleted.

So how can I capture the status change to send the email?

I created a batch that is actually sending the email, but my batch sends the email only when the Article Known_Error_Status__c = 'Fixed'. Which is obviously not what I want. I would like to send the email anytime the article status is updated.

current batch

global class BatchArticleEmail implements Schedulable, Database.Batchable<SObject> {

global string query;

// To schedule batch 
global void execute(SchedulableContext sc) {
    Id batchProcessId = Database.executeBatch(this);
}

// Constructor to query articles
global BatchArticleEmail() {
    query = 'Select Id, KnowledgeArticleId FROM Knowledge__kav WHERE Known_Error_Status__c =\'Fixed\' AND PublishStatus=\'Online\'';
}

global Database.QueryLocator start(Database.BatchableContext BC){
     return Database.getQueryLocator(Query);
}

global void execute(Database.BatchableContext BC, List<Knowledge__kav> scope) {
    System.debug('#### Scope Size ' + scope.size());
    
    for (Knowledge__kav kav : scope) {
        EmailManager.sendEmailToKnownErrorSubscribers(scope); 
    } 
    
}

global void finish(Database.BatchableContext info) {
    
}

}
2

I think you need to enable Track Field History for the Knowledge object.

If "Track Field History" is not enabled for the Knowledge object, actions will not show on new Knowledge Article versions and also for previous versions even if 'field history' was enabled when that version was created. If it is enabled, actions will show to new versions and previous versions if 'field history' was enabled when that version was created.

Every time you made changes to the field, it'll create a new version to the Knowledge object and old one will not be deleted, so that might solve your Problem.

You can find about enable tracking here..Knowledge article versions history.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.