14

I developed my code in my DE and created a custom profile named "XYZ". I used that profile in my Apex class and test methods.

While installing that managed beta package in my sandbox my test method failed because that profile is not visible.

Can I create a custom profile in the test method to ensure success?

6
  • 1
    You can create the profile first in sandbox and then map that profile with the packaged profile during installation.
    – doga
    Dec 6, 2012 at 6:54
  • your answer can solve my problem but my question is when i need to use the app in any ORG i need to create first profile in that ORG? or is there any way to create custom profile in test method. Dec 6, 2012 at 7:00
  • I think salesforce doesn't allow creation of profiles during installation. We can think of this as suppose target org is already having a profile with same name as your profile and if your profile overrides that profile then it might lead some of components not working properly.
    – doga
    Dec 6, 2012 at 8:12
  • Is your profile included in the package ? Is anything preventing you from using a standard profile ? Dec 6, 2012 at 8:32
  • 2
    Your test should not be running during install these days unless your at an older API level or using the IsTest(OnInstall=true) annotation. If your happy for this to be a regression test in your packaging org and not something that is needed to be retested in the subscriber org. You can either upgrade to the lastest API for your test (since the default is not to run on install these days) or remove the annotation if your using it. Dec 7, 2012 at 9:21

1 Answer 1

7

No you cannot create profiles in apex code, either through tests or otherwise, this is presumably a security limitation.

The second part of your problem is something I have also encountered. You cannot include a profile in a Managed Package. You can include 'Profile Settings' which can be applied to a subscribers existing profiles. But the name of the subscribers profile will not always match the name of the profile you are referencing in your unit test.

What I have done to eliminate this problem in the past is rather than referencing a profile in code, I use the Schema Describe information to determine the object or field level permissions that implicitly grant the user access. For example if a user has write permission on my Milestone Object, my code lets them perform a priviledged action on the Milestone

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .