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) { } }