1

I have the below test class:

@isTest
public static void batchManagementDraftTest() {
        User u = TestDataFactory.createUserWithRoleLeader();
        User u2 = TestDataFactory.createAnotherUserWithRoleLeader();

        List<Knowledge__kav> queriedKAV;
        List<Knowledge__kav> kavList;

        System.runAs(u) {
            List<Employee__c> listEmployee = TestDataFactory.listEmployeeSingle();
            kavList = TestDataFactory.createKnowledgeInternalFAQ();

            queriedKAV = [
                    SELECT KnowledgeArticleId, createdBy.UserRole.DeveloperName, LastModifiedBy.UserRole.DeveloperName, RecordType.DeveloperName
                    FROM Knowledge__kav
                    WHERE id =: kavList[0].id AND LastModifiedBy.UserRole.DeveloperName != 'Super_User'
                    AND ((LastModifiedBy.UserRole.DeveloperName= 'Leader' AND RecordType.DeveloperName != 'CC')
                    OR LastModifiedBy.UserRole.DeveloperName != 'Leader')
            ];
        }

        System.runAs(u2) {
            update queriedKAV;
        }

        Test.startTest();
        BatchManagement bm = new BatchManagement();
        Database.executeBatch(bam);
        Test.stopTest();
    }

Inside the BatchManagement class, there is KbManagement.PublishingService.deleteDraftArticle(kav.KnowledgeArticleId);. Unfortunately, when this line is executed I am getting You can't perform this action. Be sure the action is valid for the current state of the article, and that you have permission to perform it. Which is strange because I am running as Admin and I have the UserPermissionsKnowledgeUser set to true.

I tried moving the below part

Test.startTest();
BatchManagement bm = new BatchManagement();
Database.executeBatch(bam);
Test.stopTest();

to inside of the System.runAs(u2) like this:

System.runAs(u2) {
    update queriedKAV;
    Test.startTest();
    BatchManagement bm = new BatchManagement();
    Database.executeBatch(bam);
    Test.stopTest();
}

Still it doesn't work. Any suggestions?

5
  • It might help to have just specifics on what the TestDataFactory does (does it create a draft?) and the parts before the deleteDraftArticle in the batch class. I'm guessing, but it's possible the error is because you're attempting to delete a draft but the article is not in a draft state? Dec 14, 2020 at 18:30
  • TestDataFactory simply creates a knowledge_kav in draft. As for the code before deleteDraftArticle there are just some bunch of if else statements that are used to populate some lists, but no modifications to the knowledge_kav untill you reach the deleteDraftArticle line.
    – nSv23
    Dec 14, 2020 at 21:11
  • what is the purpose of the update statement after the query? Or did you just copy and paste relevant parts? I don't believe this is a permissions issue - that error message can display for other valid reasons: trying to create a draft of an article when one exits, draft article is in an approval process, using the wrong Id in the knowledge method, etc. Dec 14, 2020 at 22:08
  • The thing I found was if I remove the users and directly call the TestDataFactory.createKnowledgeInternalFAQ() followed the executeBatch between the Test.startTest() and Test.stopTest() it works fine. But unforutnately, the query in Batch Start has conditions that required that a knowlegde be created by users with particular profile and lastmodifedBy by users with a particular value in a custom field etc. that forced me to use the system.runAs with various users
    – nSv23
    Dec 14, 2020 at 23:03
  • what is the purpose of the update statement after the query? -> I don't update/delete all the records in the start query, only certain ones, that's why the use of if elses.
    – nSv23
    Dec 14, 2020 at 23:07

0

Your Answer

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

Browse other questions tagged or ask your own question.