1

I had this very simple code in my class:

  Interaction intToInsert = new Interaction();

  if (Interaction.sObjectType.getDescribe().isCreateable()) {
    insert intToInsert;
  }

When I run unit test against this class, the line of insert intToInsert is not run. Initially we thought it must be the permission issue so we tried to fix this. However, at last, we found out out by simply commenting out this line: if (Interaction.sObjectType.getDescribe().isCreateable()), the code can pass.

The insert is definitely executed well since in the unit test we checked the record is created and the value of the created record's fields are correct. We also checked the code coverage and everything looks perfect.

So since the insert can be executed well without any issue I would assume the permission is correct (actually we checked the permission set multiple times as well and it does grant the create access). And that line should have nothing to do with field access or triggers or anything else, why is that line failing?

Edit

Add some settings in unit test as requested: User setup:

User apiUser = TestUtil.createTestUser(
    new Map<String, Object>{
      'FirstName' => 'APIUserTestForInteraction',
      'ProfileId' => p.Id,
      'UserRoleId' => role.Id
    }
  );
  insert apiUser;

  // Assign Case related PS on API User
  List<String> psNames = new List<String>{
    'Chat_Read_Write',
    'Interaction_Read_Write',
    'API_Access_Twilio',
    'API_Only',
    'Enforce_mTLS'
  };
  List<PermissionSet> psList = [
    SELECT Id
    FROM PermissionSet
    WHERE Name IN :psNames
  ];
  List<PermissionSetAssignment> psaList = new List<PermissionSetAssignment>();
  for (PermissionSet ps : psList) {
    psaList.add(TestUtil.createPSAssignment(apiUser.id, ps.Id));
  }
  insert psaList;

Running test:

System.runAs(usr) {
  InteractionContextService.doPost(intContext);
}
5
  • Are permissions defined via perm set group or some other approach?
    – Phil W
    Jun 22 at 7:42
  • @PhilW I created a test user in the unit test. The permissions are granted via profile and permission sets
    – Lance Shi
    Jun 22 at 7:48
  • Could you edit the question to show the basic flow of your unit test, including the user setup and usages of Test.start/stopTest plus System.runAs? Thanks.
    – Phil W
    Jun 22 at 7:56
  • @PhilW updated as per requested
    – Lance Shi
    Jun 22 at 8:14
  • 1
    Not sure what the error is, but you shouldn't need to use System.runAs, as that only enforces sharing - not CRUD or FLS, which is what you're checking with the isCreateable() call. developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
    – Alba Rivas
    Jun 22 at 9:35

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.