my code has a centralised class which is responsible for all DML operations. This class before inserting / updating / deleting a record is checking:

1, if the object is accessible for the current user

2, if the fields are available.

This works in the non managed package, but is broken when I'm trying to create a managed package.

For example, the code below throws the error ONLY when I'm trying to package the code (this is a check on a custom object's custom field).

if (Transaction__c.Status__c.getDescribe().isUpdateable() != true) {
    throw new SObjectAccessException('Payment360: You can\'t access this field.' );

Any ideas what can be the problem? My understanding is that the tests are running on behalf of sysadmin user (if not specified otherwise), so they should have access to all fields.

  • Are you adding the Profile Settings when creating the package? I know that the Sys Admin is supposed to have full access, but this might be the cause of your problem. As you are trying to create a managed package, you might as well try to create a profile that has access to everything and test this out. – Renato Oliveira May 22 '16 at 21:49
  • It's a bit of a cop out, but you could add a && !Test.isRunningTest() to the end of the condition to avoid the exception. I don't think System.runAs will help you here, as it won't affect field level permissions. – Daniel Ballinger May 23 '16 at 2:32
  • I have used this in my packaged code and it worked fine for me. Can you add exactly what error do you see? also screen shot of error will help us. – AtulRajguru9 May 23 '16 at 5:14
  • Thanks so for everybody -- System.runAs did not help (however, I'm using permission sets, not profiles, so I'll give a shot again). @DanielBallinger in my current solution is also avoiding this in test-context, but still a little bit worried if the security review team will be happy as well with this :p – Peter Tempfli May 23 '16 at 10:48

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.