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